Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct PixelInfo {
- bool hasVertex;
- int meshIndex;
- };
- const XnDepthPixel* distancePixels = depth.getXnDepthGenerator().GetDepthMap(); // stores array of distances
- const XnRGB24Pixel* colorPixels = image.getXnImageGenerator().GetRGB24ImageMap(); // stores array of colors
- static XnPoint3D screenPos[kw * kh]; // stores projected coordinates
- static XnPoint3D worldPos[kw * kh]; // stores world coordinates
- static int mapIndices[kw * kh]; // maps meshIndex to mapIndex
- static PixelInfo pixelInfo[kw * kh]; // maps mapIndex to meshIndex (and whether or not there is a vertex there)
- memset(pixelInfo, 0, sizeof(pixelInfo));// zero out pixelInfo
- // add all screen positions to array (only those within near/far threshold)
- int numVerts=0;
- for(int j = 0; j < kh; j += pointStep) {
- for(int i = 0; i < kw; i += pointStep) {
- int mapIndex = j * kw + i;
- float z = distancePixels[mapIndex];
- if(z >= nearThreshold && z <= farThreshold) {
- screenPos[numVerts].X = i;
- screenPos[numVerts].Y = j;
- screenPos[numVerts].Z = z;
- mapIndices[numVerts] = mapIndex;
- pixelInfo[mapIndex].hasVertex = true;
- pixelInfo[mapIndex].meshIndex = numVerts;
- numVerts++;
- }
- }
- }
- // convert from projected coordinates to world coordinates
- depth.getXnDepthGenerator().ConvertProjectiveToRealWorld(numVerts, screenPos, worldPos);
- mesh.clear();
- mesh.setMode(OF_TRIANGLES_MODE);
- // add vertices to mesh
- for(int i = 0; i < numVerts; i ++) {
- int mapIndex = mapIndices[i];
- mesh.addVertex(ofVec3f((doFlipX ? -1 : 1) * worldPos[i].X, (doFlipY ? -1 : 1) * worldPos[i].Y, (doFlipZ ? -1 : 1) * worldPos[i].Z));
- if(pointColor.a > 0.5) {
- mesh.addColor(pointColor);
- } else {
- mesh.addColor(ofColor(colorPixels[mapIndex].nRed/255.0f, colorPixels[mapIndex].nGreen/255.0f, colorPixels[mapIndex].nBlue/255.0f, 1.0));
- }
- }
- ofVec3f *v = mesh.getVerticesPointer();
- // TODO: add borders
- for(int j=pointStep; j<kh-pointStep; j+=pointStep) {
- for(int i=pointStep; i<kw-pointStep; i+=pointStep) {
- int mapIndex1 = j * kw + i;
- int mapIndex2 = mapIndex1 + pointStep;
- int mapIndex3 = mapIndex1 + kw * pointStep;
- int mapIndex4 = mapIndex3 + pointStep;
- PixelInfo &p1 = pixelInfo[mapIndex1];
- PixelInfo &p2 = pixelInfo[mapIndex2];
- PixelInfo &p3 = pixelInfo[mapIndex3];
- PixelInfo &p4 = pixelInfo[mapIndex4];
- if(p1.hasVertex && p2.hasVertex && p3.hasVertex && p4.hasVertex) {
- int meshIndex1 = p1.meshIndex;
- int meshIndex2 = p2.meshIndex;
- int meshIndex3 = p3.meshIndex;
- int meshIndex4 = p4.meshIndex;
- // float z1 = v[index].z;
- // float z2 = v[index + pointStep].z;
- // float z3 = v[index + kw * pointStep].z;
- // float z4 = v[index + pointStep + kw * pointStep].z;
- // if(fabsf(z1-z2)<faceZThreshold && fabsf(z1-z3)<faceZThreshold && fabsf(z1-z4)<faceZThreshold && fabsf(z2-z3)<faceZThreshold && fabsf(z2-z4)<faceZThreshold && fabsf(z3-z4)<faceZThreshold) {
- // face 1
- mesh.addTriangle(meshIndex1, meshIndex2, meshIndex3);
- mesh.addTriangle(meshIndex3, meshIndex2, meshIndex4);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement