Beispiel #1
0
GLSprite::GLSprite(GLTexture* texture, Rectangle textureRectangle)
{
    initializeVariables();
    _texture          = texture;
    _textureRectangle = textureRectangle;
    generateVertices();
    generateTextureCoordinates();
}
void ofxRGBDRenderer::update(){
	
    
	if(!hasDepthImage){
     	ofLogError("ofxRGBDRenderer::update() -- no depth image");
        return;
    }

    if(!calibrationSetup && hasRGBImage){
     	ofLogError("ofxRGBDRenderer::update() -- no calibration for RGB Image");
        return;
    }

	bool debug = false;
	
	int w = 640;
	int h = 480;
	
	int start = ofGetElapsedTimeMillis();

	Point2d fov = depthCalibration.getUndistortedIntrinsics().getFov();
	
	float fx = tanf(ofDegToRad(fov.x) / 2) * 2;
	float fy = tanf(ofDegToRad(fov.y) / 2) * 2;
	
	Point2d principalPoint = depthCalibration.getUndistortedIntrinsics().getPrincipalPoint();
	cv::Size imageSize = depthCalibration.getUndistortedIntrinsics().getImageSize();
	

    if(!forceUndistortOff){
        depthCalibration.undistort( toCv(currentDepthImage), toCv(undistortedDepthImage), CV_INTER_NN);
    }
    else {
        undistortedDepthImage = currentDepthImage;
    }
	
    //start
	int imageIndex = 0;
	int vertexIndex = 0;
	float xReal,yReal;
    int indexPointer = 0;
    int vertexPointer = 0;
    hasVerts = false;
	unsigned short* ptr = undistortedDepthImage.getPixels();
	for(int y = 0; y < h; y += simplify) {
		for(int x = 0; x < w; x += simplify) {
            vertexPointer = y*w+x;
			unsigned short z = undistortedDepthImage.getPixels()[y*w+x];
			IndexMap& indx = indexMap[indexPointer];
			if(z != 0 && z < farClip){
				xReal = (((float)x - principalPoint.x) / imageSize.width) * z * fx;
				yReal = (((float)y - principalPoint.y) / imageSize.height) * z * fy;
                indx.vertexIndex = vertexPointer;
				indx.valid = true;
                simpleMesh.setVertex(vertexPointer, ofVec3f(xReal, yReal, z));
                hasVerts = true;
			}
			else {
				indx.valid = false;
			}
            indexPointer++;
		}
	}
    //end
    
    if(debug && !hasVerts) cout << "warning no verts with far clip " << farClip << endl; 
	if(debug) cout << "unprojection " << simpleMesh.getVertices().size() << " took " << ofGetElapsedTimeMillis() - start << endl;
	
	simpleMesh.clearIndices();
	set<ofIndexType> calculatedNormals;
	start = ofGetElapsedTimeMillis();
    if(calculateNormals){
		simpleMesh.getNormals().resize(simpleMesh.getVertices().size());
	}
    
	for(int i = 0; i < baseIndeces.size(); i+=3){
		if(indexMap[baseIndeces[i]].valid &&
		   indexMap[baseIndeces[i+1]].valid &&
		   indexMap[baseIndeces[i+2]].valid){
			
			ofVec3f a,b,c;
			a = simpleMesh.getVertices()[indexMap[baseIndeces[i]].vertexIndex]; 
			b = simpleMesh.getVertices()[indexMap[baseIndeces[i+1]].vertexIndex]; 
			c = simpleMesh.getVertices()[indexMap[baseIndeces[i+2]].vertexIndex]; 
			if(fabs(a.z - b.z) < edgeCull && fabs(a.z - c.z) < edgeCull){
				simpleMesh.addTriangle(indexMap[baseIndeces[i]].vertexIndex, 
									   indexMap[baseIndeces[i+1]].vertexIndex,
									   indexMap[baseIndeces[i+2]].vertexIndex);
				
				if(calculateNormals && calculatedNormals.find(indexMap[baseIndeces[i]].vertexIndex) == calculatedNormals.end()){
					//calculate normal
					simpleMesh.setNormal(indexMap[baseIndeces[i]].vertexIndex, (b-a).getCrossed(b-c).getNormalized());
					calculatedNormals.insert(indexMap[baseIndeces[i]].vertexIndex);
				}
			}
		}
	}
	
	if(debug) cout << "indexing  " << simpleMesh.getIndices().size() << " took " << ofGetElapsedTimeMillis() - start << endl;
    
    //normally this is done in the shader, but sometimes we want to have them on the CPU for doing special processing
	if(calculateTextureCoordinates){
        generateTextureCoordinates();
//		if(debug) cout << "gen tex coords took " << (ofGetElapsedTimeMillis() - start) << endl;
	}
}