Guest User

Valve BSP & Bullet Physics

a guest
Feb 12th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. //Map converter part
  2. logger.logDebug("Scanning tree...");
  3. std::vector<int> leafIds;
  4. exploreTree(nodes, models[0].node, leafIds); //Model 0 is the "world" model, without entities
  5. logger.logDebug("Found %i leaves", leafIds.size());
  6.  
  7. std::vector<unsigned short> okBrushes;
  8. unsigned int oldPos = bb.getCursorPos();
  9. unsigned int pObjs = 0;
  10. bb << pObjs;
  11.  
  12. for(int i = 0; i < leafIds.size(); i++) {
  13.     int lbCnt = leafs[leafIds[i]].leafBrushCnt;
  14.  
  15.     for(int j = 0; j < lbCnt; j++) {
  16.         int lbId = leafs[leafIds[i]].leafBrushId + j;
  17.         unsigned short bId = lbrushes[lbId];
  18.         Brush br = brushes[bId];
  19.  
  20.         if((br.contents & BRUSH_FLAG_SOLID) > 0 && br.sideCount > 0) { //BRUSH_FLAG_SOLID = 1
  21.             bool hasBeenDone = false;
  22.             for(int k = 0; k < okBrushes.size(); k++) {
  23.                 if(okBrushes[k] == bId) {
  24.                     hasBeenDone = true;
  25.                     break;
  26.                 }
  27.             }
  28.  
  29.             if(hasBeenDone)
  30.                 continue;
  31.  
  32.             bb << (unsigned short) br.sideCount;
  33.             pObjs++;
  34.  
  35.             for(int k = 0; k < br.sideCount; k++) {
  36.                 Plane pl = planes[bsides[br.sideStart + k].planeId];
  37.  
  38.                 bb << pl.normal;
  39.                 bb << (float) -pl.distance;
  40.             }
  41.  
  42.             okBrushes.push_back(bId);
  43.         }
  44.     }
  45. }
  46.  
  47. bb.setCursorPos(oldPos, ByteBuffer::CR_Begin);
  48. bb << pObjs;
  49. bb.setCursorPos(0, ByteBuffer::CR_End);
  50.  
  51. logger.logDebug("Leaves OK; found %i solid brushes!", (int) pObjs);
  52.  
  53. //Map loader part
  54. reader.readData<unsigned int>(&poCnt);
  55.  
  56. for(int i = 0; i < poCnt; i++) {
  57.     btAlignedObjectArray<btVector3> poNormals;
  58.     unsigned short nrmCnt;
  59.     reader.readData<unsigned short>(&nrmCnt);
  60.  
  61.     for(int j = 0; j < nrmCnt; j++) {
  62.         float nx, ny, nz, nw;
  63.         reader.readData<float>(&nx);
  64.         reader.readData<float>(&ny);
  65.         reader.readData<float>(&nz);
  66.         reader.readData<float>(&nw);
  67.  
  68.         btVector3 nrm;
  69.         nrm.setValue(nx, ny, nz);
  70.         nrm[3] = nw;
  71.         poNormals.push_back(nrm);
  72.     }
  73.  
  74.     btAlignedObjectArray<btVector3> poVertices;
  75.     btGeometryUtil::getVerticesFromPlaneEquations(poNormals, poVertices);
  76.  
  77.     for(int j = 0; j < poVertices.size(); j++) {
  78.         poVertices[j] *= LEVEL_SCALE; //LEVEL_SCALE is .25f; for rendering I scale the matrix using the same value
  79.  
  80.         //swap
  81.         float vz = poVertices[j].z();
  82.         poVertices[j].setZ(-poVertices[j].y());
  83.         poVertices[j].setY(vz);
  84.     }
  85.  
  86.     btConvexHullShape *shape = new btConvexHullShape(&poVertices[0].x(), poVertices.size());
  87.     btRigidBody *body = new btRigidBody(.0f, new btDefaultMotionState, shape);
  88.  
  89.     m_shapes.push_back(shape);
  90.     m_bodies.push_back(body);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment