Advertisement
memo

openni -> ofmesh

Apr 28th, 2011
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.20 KB | None | 0 0
  1. struct PixelInfo {
  2.     bool hasVertex;
  3.     int meshIndex;
  4. };      
  5.  
  6.         const XnDepthPixel* distancePixels = depth.getXnDepthGenerator().GetDepthMap();     // stores array of distances
  7.         const XnRGB24Pixel* colorPixels = image.getXnImageGenerator().GetRGB24ImageMap();   // stores array of colors
  8.        
  9.         static XnPoint3D screenPos[kw * kh];    // stores projected coordinates
  10.         static XnPoint3D worldPos[kw * kh];     // stores world coordinates
  11.         static int mapIndices[kw * kh];         // maps meshIndex to mapIndex
  12.         static PixelInfo pixelInfo[kw * kh];    // maps mapIndex to meshIndex (and whether or not there is a vertex there)
  13.        
  14.         memset(pixelInfo, 0, sizeof(pixelInfo));// zero out pixelInfo
  15.        
  16.         // add all screen positions to array (only those within near/far threshold)
  17.         int numVerts=0;
  18.         for(int j = 0; j < kh; j += pointStep) {
  19.             for(int i = 0; i < kw; i += pointStep) {
  20.                
  21.                 int mapIndex = j * kw + i;
  22.                 float z = distancePixels[mapIndex];
  23.                
  24.                 if(z >= nearThreshold && z <= farThreshold) {
  25.                     screenPos[numVerts].X = i;
  26.                     screenPos[numVerts].Y = j;
  27.                     screenPos[numVerts].Z = z;
  28.                    
  29.                     mapIndices[numVerts] = mapIndex;
  30.                    
  31.                     pixelInfo[mapIndex].hasVertex = true;
  32.                     pixelInfo[mapIndex].meshIndex = numVerts;
  33.                    
  34.                    
  35.                     numVerts++;
  36.                 }
  37.             }
  38.         }
  39.        
  40.         // convert from projected coordinates to world coordinates
  41.         depth.getXnDepthGenerator().ConvertProjectiveToRealWorld(numVerts, screenPos, worldPos);
  42.        
  43.        
  44.         mesh.clear();
  45.         mesh.setMode(OF_TRIANGLES_MODE);
  46.        
  47.         // add vertices to mesh
  48.         for(int i = 0; i < numVerts; i ++) {
  49.             int mapIndex = mapIndices[i];
  50.             mesh.addVertex(ofVec3f((doFlipX ? -1 : 1) * worldPos[i].X, (doFlipY ? -1 : 1) * worldPos[i].Y, (doFlipZ ? -1 : 1) * worldPos[i].Z));
  51.             if(pointColor.a > 0.5) {
  52.                 mesh.addColor(pointColor);
  53.             } else {
  54.                 mesh.addColor(ofColor(colorPixels[mapIndex].nRed/255.0f, colorPixels[mapIndex].nGreen/255.0f, colorPixels[mapIndex].nBlue/255.0f, 1.0));
  55.             }
  56.         }
  57.        
  58.        
  59.        
  60.        
  61.         ofVec3f *v = mesh.getVerticesPointer();
  62.        
  63.         // TODO: add borders
  64.         for(int j=pointStep; j<kh-pointStep; j+=pointStep) {
  65.             for(int i=pointStep; i<kw-pointStep; i+=pointStep) {
  66.                 int mapIndex1 = j * kw + i;
  67.                 int mapIndex2 = mapIndex1 + pointStep;
  68.                 int mapIndex3 = mapIndex1 + kw * pointStep;
  69.                 int mapIndex4 = mapIndex3 + pointStep;
  70.                
  71.                 PixelInfo &p1 = pixelInfo[mapIndex1];
  72.                 PixelInfo &p2 = pixelInfo[mapIndex2];
  73.                 PixelInfo &p3 = pixelInfo[mapIndex3];
  74.                 PixelInfo &p4 = pixelInfo[mapIndex4];
  75.                
  76.                 if(p1.hasVertex && p2.hasVertex && p3.hasVertex && p4.hasVertex) {
  77.                    
  78.                     int meshIndex1 = p1.meshIndex;
  79.                     int meshIndex2 = p2.meshIndex;
  80.                     int meshIndex3 = p3.meshIndex;
  81.                     int meshIndex4 = p4.meshIndex;
  82.                     //            float z1 = v[index].z;
  83.                     //            float z2 = v[index + pointStep].z;
  84.                     //            float z3 = v[index + kw * pointStep].z;
  85.                     //            float z4 = v[index + pointStep + kw * pointStep].z;
  86.                    
  87.                     //            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) {
  88.                     // face 1
  89.                     mesh.addTriangle(meshIndex1, meshIndex2, meshIndex3);
  90.                     mesh.addTriangle(meshIndex3, meshIndex2, meshIndex4);
  91.                 }
  92.             }
  93.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement