#include "TinyRenderer.h" #include #include "LinearMath/btHashMap.h" #include "BulletCollision/CollisionDispatch/btCollisionObject.h" #include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h" #include "BulletCollision/CollisionShapes/btCollisionShape.h" class Bullet2TinyRenderer { btAlignedObjectArray m_swRenderObjects; btHashMap m_colObject2gfxIndex; btHashMap m_colShape2gfxIndex; int m_swWidth; int m_swHeight; TGAImage m_rgbColorBuffer; b3AlignedObjectArray m_depthBuffer; int m_textureHandle; unsigned char* m_image; public: Bullet2TinyRenderer (int swWidth, int swHeight): m_swWidth(swWidth), m_swHeight(swHeight), m_rgbColorBuffer(swWidth,swHeight,TGAImage::RGB) { m_depthBuffer.resize(swWidth*swHeight); m_image=new unsigned char[m_swWidth*m_swHeight*4]; } virtual ~Bullet2TinyRenderer() { for (int i=0;igetCollisionShape()]; //if (colIndex>=0 && shapeIndex>=0) //{ // m_col2gfxIndex.insert(colIndex,shapeIndex); //} } /* virtual int registerGraphicsShape(const float* vertices, int numvertices, const int* indices, int numIndices) { if (shapeIndex>=0) { TinyRenderObjectData* swObj = new TinyRenderObjectData(m_swWidth,m_swHeight,m_rgbColorBuffer,m_depthBuffer); swObj->registerMeshShape(vertices,numvertices,indices,numIndices); //swObj->createCube(1,1,1); m_swRenderObjects.insert(shapeIndex,swObj); } return shapeIndex; } */ virtual void render(const btDiscreteDynamicsWorld* rbWorld, float viewMat[16]) { //clear the color buffer TGAColor clearColor; clearColor.bgra[0] = 255; clearColor.bgra[1] = 255; clearColor.bgra[2] = 255; clearColor.bgra[3] = 255; clearBuffers(clearColor); ATTRIBUTE_ALIGNED16(float modelMat[16]); for (int i=0;igetNumCollisionObjects();i++) { btCollisionObject* colObj = rbWorld->getCollisionObjectArray()[i]; int* colObjIndexPtr = m_colObject2gfxIndex[colObj]; if (colObjIndexPtr) { int colObjIndex = *colObjIndexPtr; TinyRenderObjectData* renderObj = m_swRenderObjects[colObjIndex]; //sync the object transform const btTransform& tr = colObj->getWorldTransform(); tr.getOpenGLMatrix(modelMat); for (int i=0;i<4;i++) { for (int j=0;j<4;j++) { renderObj->m_modelMatrix[i][j] = modelMat[i+4*j]; renderObj->m_viewMatrix[i][j] = viewMat[i+4*j]; } } TinyRenderer::renderObject(*renderObj); } } for(int y=0;y10) { counter=0; getFrameBuffer().write_tga_file("/Users/erwincoumans/develop/bullet3/framebuf.tga",true); } } };