From 6d6ef5d3dcc83ab2f2a2e54c4e05cce2e6449323 Mon Sep 17 00:00:00 2001 From: erwin coumans Date: Sun, 30 Jun 2013 23:19:14 -0700 Subject: [PATCH] fixed many memory leaks added working shadows, using basic shadow mapping --- Demos3/GpuDemos/gwenUserInterface.cpp | 17 + Demos3/GpuDemos/main_opengl3core.cpp | 168 ++++------ .../GpuDemos/raytrace/RaytracedShadowDemo.cpp | 1 + Demos3/GpuDemos/rigidbody/ConcaveScene.cpp | 4 +- Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp | 4 +- .../GpuDemos/rigidbody/GpuRigidBodyDemo.cpp | 7 +- .../rigidbody/GpuRigidBodyDemoInternalData.h | 4 +- Demos3/GpuDemos/shadows/ShadowMapDemo.cpp | 292 ++++++++++++++++-- Demos3/GpuDemos/shadows/ShadowMapDemo.h | 4 + .../shadows/ShadowMapDemoInternalData.h | 2 + btgui/OpenGLTrueTypeFont/fontstash.cpp | 2 + .../opengl_fontstashcallbacks.cpp | 1 + .../opengl_fontstashcallbacks.h | 1 + btgui/OpenGLWindow/GLInstancingRenderer.cpp | 163 ++++++---- btgui/OpenGLWindow/GLInstancingRenderer.h | 7 +- btgui/OpenGLWindow/GLPrimitiveRenderer.cpp | 1 + btgui/OpenGLWindow/GLPrimitiveRenderer.h | 1 + btgui/OpenGLWindow/GLRenderToTexture.cpp | 18 +- .../Shaders/createShadowMapInstancingVS.h | 5 +- .../Shaders/createshadowMapInstancingVS.glsl | 5 +- .../Shaders/useShadowMapInstancingPS.glsl | 24 +- .../Shaders/useShadowMapInstancingPS.h | 24 +- .../Shaders/useShadowMapInstancingVS.glsl | 14 +- .../Shaders/useShadowMapInstancingVS.h | 14 +- .../RigidBody/b3GpuBatchingPgsSolver.cpp | 9 +- .../RigidBody/b3GpuNarrowPhase.cpp | 12 + .../RigidBody/b3GpuNarrowPhaseInternalData.h | 1 + src/Bullet3OpenCL/RigidBody/b3Solver.cpp | 2 + 28 files changed, 581 insertions(+), 226 deletions(-) diff --git a/Demos3/GpuDemos/gwenUserInterface.cpp b/Demos3/GpuDemos/gwenUserInterface.cpp index e8e7ae2f9..9c98fdc60 100644 --- a/Demos3/GpuDemos/gwenUserInterface.cpp +++ b/Demos3/GpuDemos/gwenUserInterface.cpp @@ -53,7 +53,24 @@ GwenUserInterface::GwenUserInterface() GwenUserInterface::~GwenUserInterface() { + for (int i=0;im_handlers.size();i++) + { + delete m_data->m_handlers[i]; + } + + m_data->m_handlers.clear(); + + + delete m_data->pCanvas; + + GLPrimitiveRenderer* prim = m_data->m_primRenderer; + GwenOpenGL3CoreRenderer* coreRend = m_data->pRenderer; + delete m_data; + + delete prim; + delete coreRend; + } diff --git a/Demos3/GpuDemos/main_opengl3core.cpp b/Demos3/GpuDemos/main_opengl3core.cpp index 61b5b4dbc..ffa209c30 100644 --- a/Demos3/GpuDemos/main_opengl3core.cpp +++ b/Demos3/GpuDemos/main_opengl3core.cpp @@ -89,7 +89,7 @@ GpuDemo::CreateFunc* allDemos[]= // ConcaveSphereScene::MyCreateFunc, -// ShadowMapDemo::MyCreateFunc, + GpuBoxPlaneScene::MyCreateFunc, GpuConvexPlaneScene::MyCreateFunc, @@ -119,6 +119,9 @@ GpuDemo::CreateFunc* allDemos[]= PairBench::MyCreateFunc, GpuRaytraceScene::MyCreateFunc, + + ShadowMapDemo::MyCreateFunc, + //GpuRigidBodyDemo::MyCreateFunc, //BroadphaseBenchmark::CreateFunc, @@ -222,7 +225,16 @@ bool enableExperimentalCpuConcaveCollision=false; int droidRegular=0;//, droidItalic, droidBold, droidJapanese, dejavu; sth_stash* stash=0; +OpenGL2RenderCallbacks* renderCallbacks = 0; +void exitFont() +{ + sth_delete(stash); + stash=0; + + delete renderCallbacks; + renderCallbacks=0; +} sth_stash* initFont(GLPrimitiveRenderer* primRender) { GLint err; @@ -233,7 +245,7 @@ sth_stash* initFont(GLPrimitiveRenderer* primRender) float sx,sy,dx,dy,lh; GLuint texture; - OpenGL2RenderCallbacks* renderCallbacks = new OpenGL2RenderCallbacks(primRender); + renderCallbacks = new OpenGL2RenderCallbacks(primRender); stash = sth_create(512,512,renderCallbacks);//256,256);//,1024);//512,512); err = glGetError(); @@ -569,33 +581,31 @@ int main(int argc, char* argv[]) stash = initFont(&prim); - gui->init(g_OpenGLWidth,g_OpenGLHeight,stash,window->getRetinaScale()); - - printf("init fonts"); - - - gui->setToggleButtonCallback(MyButtonCallback); - - gui->registerToggleButton(MYPAUSE,"Pause"); - gui->registerToggleButton(MYPROFILE,"Profile"); - gui->registerToggleButton(MYRESET,"Reset"); - - - - - - - int numItems = sizeof(allDemos)/sizeof(ParticleDemo::CreateFunc*); - demoNames.clear(); - for (int i=0;igetName()); - delete demo; - } + gui->init(g_OpenGLWidth,g_OpenGLHeight,stash,window->getRetinaScale()); - gui->registerComboBox(MYCOMBOBOX1,numItems,&demoNames[0]); - gui->setComboBoxCallback(MyComboBoxCallback); + printf("init fonts"); + + + gui->setToggleButtonCallback(MyButtonCallback); + + gui->registerToggleButton(MYPAUSE,"Pause"); + gui->registerToggleButton(MYPROFILE,"Profile"); + gui->registerToggleButton(MYRESET,"Reset"); + + int numItems = sizeof(allDemos)/sizeof(ParticleDemo::CreateFunc*); + demoNames.clear(); + for (int i=0;igetName()); + delete demo; + } + + gui->registerComboBox(MYCOMBOBOX1,numItems,&demoNames[0]); + gui->setComboBoxCallback(MyComboBoxCallback); + } @@ -610,72 +620,9 @@ int main(int argc, char* argv[]) static bool once=true; - - - glClearColor(1,0,0,1); + glClearColor(0.3f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); - { - - window->startRendering(); - glFinish(); - - - - - float color[4] = {1,1,1,1}; - prim.drawRect(0,0,200,200,color); - float retinaScale = 1; - - float x = 10; - float y=220; - float dx=0; - if (1) - { - B3_PROFILE("font sth_draw_text"); - - glEnable(GL_BLEND); - GLint err = glGetError(); - assert(err==GL_NO_ERROR); - - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - err = glGetError(); - assert(err==GL_NO_ERROR); - - glDisable(GL_DEPTH_TEST); - err = glGetError(); - assert(err==GL_NO_ERROR); - - - glDisable(GL_CULL_FACE); - - sth_begin_draw(stash); - sth_flush_draw(stash); - sth_draw_text(stash, droidRegular,20.f, x, y, "Non-retina font rendering !@#$", &dx,g_OpenGLWidth,g_OpenGLHeight,0,1);//retinaScale); - if (retinaScale!=1.f) - sth_draw_text(stash, droidRegular,20.f*retinaScale, x, y+20, "Retina font rendering!@#$", &dx,g_OpenGLWidth,g_OpenGLHeight,0,retinaScale); - sth_flush_draw(stash); - - sth_end_draw(stash); - } - - gui->draw(g_OpenGLWidth,g_OpenGLHeight); - window->endRendering(); - glFinish(); - } - once=false; - -// OpenGL3CoreRenderer render; - - glClearColor(0,1,0,1); - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); - - window->endRendering(); - - glFinish(); - - - window->setWheelCallback(b3DefaultWheelCallback); @@ -690,7 +637,7 @@ int main(int argc, char* argv[]) int maxObjectCapacity=128*1024; maxObjectCapacity = b3Max(maxObjectCapacity,ci.arraySizeX*ci.arraySizeX*ci.arraySizeX+10); - + { ci.m_instancingRenderer = new GLInstancingRenderer(maxObjectCapacity);//render.getInstancingRenderer(); ci.m_window = window; ci.m_gui = gui; @@ -699,8 +646,11 @@ int main(int argc, char* argv[]) ci.m_instancingRenderer->InitShaders(); ci.m_primRenderer = &prim; // render.init(); + } - demo->initPhysics(ci); + { + demo->initPhysics(ci); + } @@ -793,7 +743,8 @@ int main(int argc, char* argv[]) } do { - + + GLint err = glGetError(); assert(err==GL_NO_ERROR); @@ -844,7 +795,6 @@ int main(int argc, char* argv[]) err = glGetError(); assert(err==GL_NO_ERROR); - glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);//|GL_STENCIL_BUFFER_BIT); glEnable(GL_DEPTH_TEST); @@ -896,7 +846,8 @@ int main(int argc, char* argv[]) { B3_PROFILE("gui->draw"); - gui->draw(g_OpenGLWidth,g_OpenGLHeight); + if (gui) + gui->draw(g_OpenGLWidth,g_OpenGLHeight); } err = glGetError(); assert(err==GL_NO_ERROR); @@ -948,6 +899,8 @@ int main(int argc, char* argv[]) demo->exitPhysics(); b3ProfileManager::CleanupMemory(); + delete ci.m_instancingRenderer; + delete demo; if (detailsFile) { @@ -966,13 +919,24 @@ int main(int argc, char* argv[]) } while (gReset); - gui->setComboBoxCallback(0); - delete gui; - gui=0; + if (gui) + gui->setComboBoxCallback(0); - window->closeWindow(); - delete window; - window = 0; + { + + + + delete gui; + gui=0; + + exitFont(); + + + window->closeWindow(); + delete window; + window = 0; + + } return 0; } diff --git a/Demos3/GpuDemos/raytrace/RaytracedShadowDemo.cpp b/Demos3/GpuDemos/raytrace/RaytracedShadowDemo.cpp index 913563eb3..f3590fbde 100644 --- a/Demos3/GpuDemos/raytrace/RaytracedShadowDemo.cpp +++ b/Demos3/GpuDemos/raytrace/RaytracedShadowDemo.cpp @@ -86,6 +86,7 @@ GpuRaytraceScene::~GpuRaytraceScene() glDeleteTextures(1,m_raytraceData->m_texId); delete[] m_raytraceData->m_texels; delete m_raytraceData->m_renderToTexture; + delete m_raytraceData->m_texId; delete m_raytraceData; } diff --git a/Demos3/GpuDemos/rigidbody/ConcaveScene.cpp b/Demos3/GpuDemos/rigidbody/ConcaveScene.cpp index abe0d8654..6d4b1957e 100644 --- a/Demos3/GpuDemos/rigidbody/ConcaveScene.cpp +++ b/Demos3/GpuDemos/rigidbody/ConcaveScene.cpp @@ -232,7 +232,9 @@ void ConcaveScene::createConcaveMesh(const ConstructionInfo& ci, const char* fil - + delete shape->m_indices; + delete shape->m_vertices; + delete shape; } } diff --git a/Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp b/Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp index c50dcd52a..a73a376ef 100644 --- a/Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp +++ b/Demos3/GpuDemos/rigidbody/GpuConvexScene.cpp @@ -48,7 +48,8 @@ void GpuConvexScene::setupScene(const ConstructionInfo& ci) char msg[1024]; int numInstances = index; sprintf(msg,"Num objects = %d",numInstances); - ci.m_gui->setStatusBarMessage(msg,true); + if (ci.m_gui) + ci.m_gui->setStatusBarMessage(msg,true); } void GpuConvexScene::destroyScene() @@ -168,6 +169,7 @@ int GpuConvexScene::createDynamicsObjects2(const ConstructionInfo& ci, const flo } } } + delete utilPtr; } return index; } diff --git a/Demos3/GpuDemos/rigidbody/GpuRigidBodyDemo.cpp b/Demos3/GpuDemos/rigidbody/GpuRigidBodyDemo.cpp index 5ffe9e049..408aaad1e 100644 --- a/Demos3/GpuDemos/rigidbody/GpuRigidBodyDemo.cpp +++ b/Demos3/GpuDemos/rigidbody/GpuRigidBodyDemo.cpp @@ -117,9 +117,9 @@ void GpuRigidBodyDemo::initPhysics(const ConstructionInfo& ci) b3GpuSapBroadphase* bp = new b3GpuSapBroadphase(m_clData->m_clContext,m_clData->m_clDevice,m_clData->m_clQueue); m_data->m_np = np; m_data->m_bp = bp; - b3DynamicBvhBroadphase* broadphaseDbvt = new b3DynamicBvhBroadphase(config.m_maxConvexBodies); + m_data->m_broadphaseDbvt = new b3DynamicBvhBroadphase(config.m_maxConvexBodies); - m_data->m_rigidBodyPipeline = new b3GpuRigidBodyPipeline(m_clData->m_clContext,m_clData->m_clDevice,m_clData->m_clQueue, np, bp,broadphaseDbvt,config); + m_data->m_rigidBodyPipeline = new b3GpuRigidBodyPipeline(m_clData->m_clContext,m_clData->m_clDevice,m_clData->m_clQueue, np, bp,m_data->m_broadphaseDbvt,config); setupScene(ci); @@ -139,8 +139,11 @@ void GpuRigidBodyDemo::initPhysics(const ConstructionInfo& ci) void GpuRigidBodyDemo::exitPhysics() { + destroyScene(); + delete m_data->m_instancePosOrnColor; delete m_data->m_rigidBodyPipeline; + delete m_data->m_broadphaseDbvt; m_window->setKeyboardCallback(oldCallback); diff --git a/Demos3/GpuDemos/rigidbody/GpuRigidBodyDemoInternalData.h b/Demos3/GpuDemos/rigidbody/GpuRigidBodyDemoInternalData.h index 189c2a5cf..a5b8943de 100644 --- a/Demos3/GpuDemos/rigidbody/GpuRigidBodyDemoInternalData.h +++ b/Demos3/GpuDemos/rigidbody/GpuRigidBodyDemoInternalData.h @@ -16,12 +16,14 @@ struct GpuRigidBodyDemoInternalData class b3GpuNarrowPhase* m_np; class b3GpuSapBroadphase* m_bp; + class b3DynamicBvhBroadphase* m_broadphaseDbvt; GpuRigidBodyDemoInternalData() :m_instancePosOrnColor(0), m_copyTransformsToVBOKernel(0), m_rigidBodyPipeline(0), m_np(0), - m_bp(0) + m_bp(0), + m_broadphaseDbvt(0) { } }; diff --git a/Demos3/GpuDemos/shadows/ShadowMapDemo.cpp b/Demos3/GpuDemos/shadows/ShadowMapDemo.cpp index 7f5dca440..48b12bd0e 100644 --- a/Demos3/GpuDemos/shadows/ShadowMapDemo.cpp +++ b/Demos3/GpuDemos/shadows/ShadowMapDemo.cpp @@ -1,7 +1,11 @@ #include "ShadowMapDemo.h" #include "ShadowMapDemoInternalData.h" #include "OpenGLWindow/GLInstancingRenderer.h" +#include "OpenglWindow/GLPrimitiveRenderer.h" + #include "OpenGLWindow/ShapeData.h" +#include "Bullet3Common/b3Quaternion.h" +#include ShadowMapDemo::ShadowMapDemo() { @@ -17,32 +21,52 @@ ShadowMapDemo::~ShadowMapDemo() void ShadowMapDemo::initPhysics(const ConstructionInfo& ci) { m_shadowData->m_instancingRenderer = ci.m_instancingRenderer; + m_shadowData->m_primitiveRenderer = ci.m_primRenderer; - int sphereShape = registerGraphicsSphereShape(ci,0.1,false); float pos[4]={0,3,0,0}; float orn[4]={0,0,0,1}; float color[4]={1,0,0,1}; float scaling[4]={1,1,1,1}; + if (1) + { + ///create a sphere + int sphereShape = registerGraphicsSphereShape(ci,0.1,false); + ci.m_instancingRenderer->registerGraphicsInstance(sphereShape,pos,orn,color,scaling); + } + if (1) + { + //create a cube + int strideInBytes = 9*sizeof(float); + int numVertices = sizeof(cube_vertices)/strideInBytes; + int numIndices = sizeof(cube_vertices)/sizeof(int); + //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); + int boxShape = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); + pos[1]=0.f; + scaling[0]=scaling[2]=50.f; + color[0]=1.f; + color[1]=1.f; + color[2]=1.f; + color[3]=1.f; + ci.m_instancingRenderer->registerGraphicsInstance(boxShape ,pos,orn,color,scaling); + } - ci.m_instancingRenderer->registerGraphicsInstance(sphereShape,pos,orn,color,scaling); - + b3Vector3 shift(0,0,0); + if (0) + { + b3Vector3 scaling(1,1,1); + const char* filename="data/room_thickwalls.obj"; + this->createConcaveMesh(ci,filename,shift,scaling); + } - int strideInBytes = 9*sizeof(float); - int numVertices = sizeof(cube_vertices)/strideInBytes; - int numIndices = sizeof(cube_vertices)/sizeof(int); - //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); - int boxShape = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); - pos[1]=0.f; - scaling[0]=scaling[2]=10.f; - color[0]=1.f; - color[1]=1.f; - color[2]=1.f; - color[3]=1.f; - ci.m_instancingRenderer->registerGraphicsInstance(boxShape ,pos,orn,color,scaling); + { + b3Vector3 camPos(0, 2,5); + m_shadowData->m_instancingRenderer->setCameraTargetPosition(camPos); + m_shadowData->m_instancingRenderer->setCameraPitch(0); + m_shadowData->m_instancingRenderer->setCameraYaw(0); - - m_shadowData->m_instancingRenderer->setCameraTargetPosition(pos); - m_shadowData->m_instancingRenderer->setCameraDistance(15); + m_shadowData->m_instancingRenderer->setCameraDistance(15); + } + //m_shadowData->m_instancingRenderer->setCameraYaw(55); ci.m_instancingRenderer->writeTransforms(); } @@ -50,13 +74,233 @@ void ShadowMapDemo::exitPhysics() { } -void ShadowMapDemo::renderScene() -{ - m_shadowData->m_instancingRenderer->renderScene(B3_CREATE_SHADOWMAP_RENDERMODE); - //m_shadowData->m_instancingRenderer->renderScene(); - m_shadowData->m_instancingRenderer->renderScene(B3_USE_SHADOWMAP_RENDERMODE); -} void ShadowMapDemo::clientMoveAndDisplay() { } + +#include"../../Wavefront/objLoader.h" +#include "OpenGLWindow/GLInstanceGraphicsShape.h" + +static GLInstanceGraphicsShape* createGraphicsShapeFromWavefrontObj(objLoader* obj) +{ + b3AlignedObjectArray* vertices = new b3AlignedObjectArray; + { +// int numVertices = obj->vertexCount; + // int numIndices = 0; + b3AlignedObjectArray* indicesPtr = new b3AlignedObjectArray; + /* + for (int v=0;vvertexCount;v++) + { + vtx.xyzw[0] = obj->vertexList[v]->e[0]; + vtx.xyzw[1] = obj->vertexList[v]->e[1]; + vtx.xyzw[2] = obj->vertexList[v]->e[2]; + b3Vector3 n(vtx.xyzw[0],vtx.xyzw[1],vtx.xyzw[2]); + if (n.length2()>B3_EPSILON) + { + n.normalize(); + vtx.normal[0] = n[0]; + vtx.normal[1] = n[1]; + vtx.normal[2] = n[2]; + + } else + { + vtx.normal[0] = 0; //todo + vtx.normal[1] = 1; + vtx.normal[2] = 0; + } + vtx.uv[0] = 0.5f;vtx.uv[1] = 0.5f; //todo + vertices->push_back(vtx); + } + */ + + for (int f=0;ffaceCount;f++) + { + obj_face* face = obj->faceList[f]; + //b3Vector3 normal(face.m_plane[0],face.m_plane[1],face.m_plane[2]); + if (face->vertex_count>=3) + { + b3Vector3 normal(0,1,0); + int vtxBaseIndex = vertices->size(); + + if (face->vertex_count<=4) + { + indicesPtr->push_back(vtxBaseIndex); + indicesPtr->push_back(vtxBaseIndex+1); + indicesPtr->push_back(vtxBaseIndex+2); + + GLInstanceVertex vtx0; + vtx0.xyzw[0] = obj->vertexList[face->vertex_index[0]]->e[0]; + vtx0.xyzw[1] = obj->vertexList[face->vertex_index[0]]->e[1]; + vtx0.xyzw[2] = obj->vertexList[face->vertex_index[0]]->e[2]; + vtx0.xyzw[3] = 0.f;//obj->vertexList[face->vertex_index[0]]->e[2]; + + vtx0.uv[0] = 0.5f;//obj->textureList[face->vertex_index[0]]->e[0]; + vtx0.uv[1] = 0.5f;//obj->textureList[face->vertex_index[0]]->e[1]; + + GLInstanceVertex vtx1; + vtx1.xyzw[0] = obj->vertexList[face->vertex_index[1]]->e[0]; + vtx1.xyzw[1] = obj->vertexList[face->vertex_index[1]]->e[1]; + vtx1.xyzw[2] = obj->vertexList[face->vertex_index[1]]->e[2]; + vtx1.xyzw[3]= 0.f; + vtx1.uv[0] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[0]; + vtx1.uv[1] = 0.5f;//obj->textureList[face->vertex_index[1]]->e[1]; + + GLInstanceVertex vtx2; + vtx2.xyzw[0] = obj->vertexList[face->vertex_index[2]]->e[0]; + vtx2.xyzw[1] = obj->vertexList[face->vertex_index[2]]->e[1]; + vtx2.xyzw[2] = obj->vertexList[face->vertex_index[2]]->e[2]; + vtx2.xyzw[3] = 0.f; + vtx2.uv[0] = 0.5f;obj->textureList[face->vertex_index[2]]->e[0]; + vtx2.uv[1] = 0.5f;obj->textureList[face->vertex_index[2]]->e[1]; + + + b3Vector3 v0(vtx0.xyzw[0],vtx0.xyzw[1],vtx0.xyzw[2]); + b3Vector3 v1(vtx1.xyzw[0],vtx1.xyzw[1],vtx1.xyzw[2]); + b3Vector3 v2(vtx2.xyzw[0],vtx2.xyzw[1],vtx2.xyzw[2]); + + normal = (v1-v0).cross(v2-v0); + normal.normalize(); + vtx0.normal[0] = normal[0]; + vtx0.normal[1] = normal[1]; + vtx0.normal[2] = normal[2]; + vtx1.normal[0] = normal[0]; + vtx1.normal[1] = normal[1]; + vtx1.normal[2] = normal[2]; + vtx2.normal[0] = normal[0]; + vtx2.normal[1] = normal[1]; + vtx2.normal[2] = normal[2]; + vertices->push_back(vtx0); + vertices->push_back(vtx1); + vertices->push_back(vtx2); + } + if (face->vertex_count==4) + { + + indicesPtr->push_back(vtxBaseIndex); + indicesPtr->push_back(vtxBaseIndex+1); + indicesPtr->push_back(vtxBaseIndex+2); + indicesPtr->push_back(vtxBaseIndex+3); +// + GLInstanceVertex vtx3; + vtx3.xyzw[0] = obj->vertexList[face->vertex_index[3]]->e[0]; + vtx3.xyzw[1] = obj->vertexList[face->vertex_index[3]]->e[1]; + vtx3.xyzw[2] = obj->vertexList[face->vertex_index[3]]->e[2]; + vtx3.uv[0] = 0.5; + vtx3.uv[1] = 0.5; + + vtx3.normal[0] = normal[0]; + vtx3.normal[1] = normal[1]; + vtx3.normal[2] = normal[2]; + + vertices->push_back(vtx3); + + } + } + } + + + GLInstanceGraphicsShape* gfxShape = new GLInstanceGraphicsShape; + gfxShape->m_vertices = vertices; + gfxShape->m_numvertices = vertices->size(); + gfxShape->m_indices = indicesPtr; + gfxShape->m_numIndices = indicesPtr->size(); + for (int i=0;i<4;i++) + gfxShape->m_scaling[i] = 1;//bake the scaling into the vertices + return gfxShape; + } +} + + +void ShadowMapDemo::createConcaveMesh(const ConstructionInfo& ci, const char* fileName, const b3Vector3& shift, const b3Vector3& scaling) +{ + objLoader* objData = new objLoader(); + + FILE* f = 0; + + char relativeFileName[1024]; + { + const char* prefix[]={"./","../","../../","../../../","../../../../"}; + int numPrefixes = sizeof(prefix)/sizeof(char*); + + for (int i=0;iload(relativeFileName); + int index=10; + + { + GLInstanceGraphicsShape* shape = createGraphicsShapeFromWavefrontObj(objData); + + + b3AlignedObjectArray verts; + for (int i=0;im_numvertices;i++) + { + for (int j=0;j<3;j++) + shape->m_vertices->at(i).xyzw[j] += shift[j]; + + b3Vector3 vtx(shape->m_vertices->at(i).xyzw[0], + shape->m_vertices->at(i).xyzw[1], + shape->m_vertices->at(i).xyzw[2]); + verts.push_back(vtx*scaling); + } + + + { + int strideInBytes = 9*sizeof(float); + int numVertices = sizeof(cube_vertices)/strideInBytes; + int numIndices = sizeof(cube_indices)/sizeof(int); + //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); + //int shapeId = ci.m_instancingRenderer->registerShape(&cube_vertices[0],numVertices,cube_indices,numIndices); + + + int shapeId = ci.m_instancingRenderer->registerShape(&shape->m_vertices->at(0).xyzw[0], shape->m_numvertices, &shape->m_indices->at(0), shape->m_numIndices); + b3Quaternion orn(0,0,0,1); + + b3Vector4 color(0.3,0.3,1,1.f);//0.5);//1.f + + + { + float mass = 0.f; + b3Vector3 position(0,0,0); + int id = ci.m_instancingRenderer->registerGraphicsInstance(shapeId,position,orn,color,scaling); + index++; + } + } + } + delete objData; +} + + + + +void ShadowMapDemo::renderScene() +{ + float color[4]={1,1,1,1}; + m_shadowData->m_instancingRenderer->renderScene(); + + m_shadowData->m_instancingRenderer->enableShadowMap(); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE ); +// glTexParameteri( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE ); + m_shadowData->m_primitiveRenderer->drawTexturedRect(10,10,90,90,color,0,0,1,1,true); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); + + +} diff --git a/Demos3/GpuDemos/shadows/ShadowMapDemo.h b/Demos3/GpuDemos/shadows/ShadowMapDemo.h index 3f692e004..891005625 100644 --- a/Demos3/GpuDemos/shadows/ShadowMapDemo.h +++ b/Demos3/GpuDemos/shadows/ShadowMapDemo.h @@ -2,6 +2,7 @@ #define SHADOW_MAP_DEMO_H #include "../GpuDemo.h" +#include "Bullet3Common/b3Vector3.h" class ShadowMapDemo : public GpuDemo { @@ -23,6 +24,9 @@ public: virtual void renderScene(); + void createConcaveMesh(const ConstructionInfo& ci, const char* fileName, const b3Vector3& shift, const b3Vector3& scaling); + + virtual void clientMoveAndDisplay(); static GpuDemo* MyCreateFunc() diff --git a/Demos3/GpuDemos/shadows/ShadowMapDemoInternalData.h b/Demos3/GpuDemos/shadows/ShadowMapDemoInternalData.h index d8c9df62f..d019bcb3b 100644 --- a/Demos3/GpuDemos/shadows/ShadowMapDemoInternalData.h +++ b/Demos3/GpuDemos/shadows/ShadowMapDemoInternalData.h @@ -5,6 +5,8 @@ struct ShadowMapDemoInternalData { class GLInstancingRenderer* m_instancingRenderer; + class GLPrimitiveRenderer* m_primitiveRenderer; + }; #endif //SHADOW_MAP_INTERNAL_DATA_H diff --git a/btgui/OpenGLTrueTypeFont/fontstash.cpp b/btgui/OpenGLTrueTypeFont/fontstash.cpp index a42e2eac4..02b552b8b 100644 --- a/btgui/OpenGLTrueTypeFont/fontstash.cpp +++ b/btgui/OpenGLTrueTypeFont/fontstash.cpp @@ -789,6 +789,8 @@ void sth_delete(struct sth_stash* stash) tex = stash->textures; while(tex != NULL) { curtex = tex; + delete tex->m_texels; + tex->m_texels=0; tex = tex->next; stash->m_renderCallbacks->updateTexture(curtex,0,0,0); free(curtex); diff --git a/btgui/OpenGLTrueTypeFont/opengl_fontstashcallbacks.cpp b/btgui/OpenGLTrueTypeFont/opengl_fontstashcallbacks.cpp index 2d034af34..e1554bf08 100644 --- a/btgui/OpenGLTrueTypeFont/opengl_fontstashcallbacks.cpp +++ b/btgui/OpenGLTrueTypeFont/opengl_fontstashcallbacks.cpp @@ -180,6 +180,7 @@ void InternalOpenGL2RenderCallbacks::updateTexture(sth_texture* texture, sth_gly glDeleteTextures(1, id); //delete id; + delete texture->m_userData; texture->m_userData = 0; } diff --git a/btgui/OpenGLTrueTypeFont/opengl_fontstashcallbacks.h b/btgui/OpenGLTrueTypeFont/opengl_fontstashcallbacks.h index fe1714aa8..285a3df97 100644 --- a/btgui/OpenGLTrueTypeFont/opengl_fontstashcallbacks.h +++ b/btgui/OpenGLTrueTypeFont/opengl_fontstashcallbacks.h @@ -7,6 +7,7 @@ class GLPrimitiveRenderer; struct InternalOpenGL2RenderCallbacks : public RenderCallbacks { + virtual PrimInternalData* getData()=0; virtual ~InternalOpenGL2RenderCallbacks(); diff --git a/btgui/OpenGLWindow/GLInstancingRenderer.cpp b/btgui/OpenGLWindow/GLInstancingRenderer.cpp index e846f6670..63273be62 100644 --- a/btgui/OpenGLWindow/GLInstancingRenderer.cpp +++ b/btgui/OpenGLWindow/GLInstancingRenderer.cpp @@ -14,6 +14,10 @@ subject to the following restrictions: //Originally written by Erwin Coumans +///todo: make this configurable in the gui +bool useShadowMap=true; +float shadowMapWidth=4096;//8192, 2048 +float shadowMapHeight=4096; #include "OpenGLInclude.h" @@ -248,14 +252,14 @@ static GLuint instancingShaderPointSprite; // The point spr static bool done = false; static GLint useShadow_ModelViewMatrix=0; +static GLint useShadow_MVP=0; + static GLint useShadow_ProjectionMatrix=0; static GLint useShadow_DepthBiasModelViewMatrix=0; static GLint useShadow_uniform_texture_diffuse = 0; static GLint useShadow_shadowMap = 0; -static GLint createShadow_ModelViewMatrix=0; -static GLint createShadow_ProjectionMatrix=0; -static GLint createShadow_uniform_texture_diffuse = 0; +static GLint createShadow_depthMVP=0; static GLint ModelViewMatrix=0; static GLint ProjectionMatrix=0; @@ -290,6 +294,10 @@ GLInstancingRenderer::GLInstancingRenderer(int maxNumObjectCapacity, int maxShap GLInstancingRenderer::~GLInstancingRenderer() { + delete m_data->m_shadowMap; + glDeleteTextures(1,&m_data->m_shadowTexture); + glDeleteTextures(1,&m_data->m_defaultTexturehandle); + for (int i=0;im_index_vbo) @@ -300,7 +308,10 @@ GLInstancingRenderer::~GLInstancingRenderer() { glDeleteVertexArrays(1,&m_graphicsInstances[i]->m_cube_vao); } + delete m_graphicsInstances[i]; } + m_graphicsInstances.clear(); + sData2=0; if (m_data) @@ -635,8 +646,9 @@ void GLInstancingRenderer::InitShaders() glLinkProgram(useShadowMapInstancingShader); glUseProgram(useShadowMapInstancingShader); useShadow_ModelViewMatrix = glGetUniformLocation(useShadowMapInstancingShader, "ModelViewMatrix"); + useShadow_MVP = glGetUniformLocation(useShadowMapInstancingShader, "MVP"); useShadow_ProjectionMatrix = glGetUniformLocation(useShadowMapInstancingShader, "ProjectionMatrix"); - useShadow_DepthBiasModelViewMatrix = glGetUniformLocation(useShadowMapInstancingShader, " DepthBiasModelViewProjectionMatrix"); + useShadow_DepthBiasModelViewMatrix = glGetUniformLocation(useShadowMapInstancingShader, "DepthBiasModelViewProjectionMatrix"); useShadow_uniform_texture_diffuse = glGetUniformLocation(useShadowMapInstancingShader, "Diffuse"); useShadow_shadowMap = glGetUniformLocation(useShadowMapInstancingShader,"shadowMap"); @@ -644,9 +656,7 @@ void GLInstancingRenderer::InitShaders() createShadowMapInstancingShader = gltLoadShaderPair(createShadowMapInstancingVertexShader,createShadowMapInstancingFragmentShader); glLinkProgram(createShadowMapInstancingShader); glUseProgram(createShadowMapInstancingShader); - createShadow_ModelViewMatrix = glGetUniformLocation(createShadowMapInstancingShader, "ModelViewMatrix"); - createShadow_ProjectionMatrix = glGetUniformLocation(createShadowMapInstancingShader, "ProjectionMatrix"); - createShadow_uniform_texture_diffuse = glGetUniformLocation(createShadowMapInstancingShader, "Diffuse"); + createShadow_depthMVP = glGetUniformLocation(createShadowMapInstancingShader, "depthMVP"); glUseProgram(0); @@ -705,7 +715,7 @@ void GLInstancingRenderer::init() err = glGetError(); b3Assert(err==GL_NO_ERROR); - glClearColor(float(0.),float(0.),float(0.4),float(0)); +// glClearColor(float(0.),float(0.),float(0.4),float(0)); err = glGetError(); b3Assert(err==GL_NO_ERROR); @@ -859,6 +869,14 @@ void b3Matrix4x4Mul(GLfloat aIn[4][4], GLfloat bIn[4][4], GLfloat result[4][4]) result[j][i] = aIn[0][i] * bIn[j][0] + aIn[1][i] * bIn[j][1] + aIn[2][i] * bIn[j][2] + aIn[3][i] * bIn[j][3]; } +void b3Matrix4x4Mul16(GLfloat aIn[16], GLfloat bIn[16], GLfloat result[16]) +{ + for (int j=0;j<4;j++) + for (int i=0;i<4;i++) + result[j*4+i] = aIn[0*4+i] * bIn[j*4+0] + aIn[1*4+i] * bIn[j*4+1] + aIn[2*4+i] * bIn[j*4+2] + aIn[3*4+i] * bIn[j*4+3]; +} + + void b3CreateDiagonalMatrix(GLfloat value, GLfloat result[4][4]) { for (int i=0;i<4;i++) @@ -971,35 +989,19 @@ void GLInstancingRenderer::updateCamera() aspect = m_screenWidth / (b3Scalar)m_screenHeight; extents.setValue(aspect * 1.0f, 1.0f,0); - - - if (m_ortho) + + + if (m_screenWidth > m_screenHeight) { - // reset matrix - - - extents *= m_data->m_cameraDistance; - //b3Vector3 lower = m_data->m_cameraTargetPosition - extents; - //b3Vector3 upper = m_data->m_cameraTargetPosition + extents; - //gluOrtho2D(lower.x, upper.x, lower.y, upper.y); - //glTranslatef(100,210,0); + b3CreateFrustum(-aspect * m_frustumZNear, aspect * m_frustumZNear, -m_frustumZNear, m_frustumZNear, m_frustumZNear, m_frustumZFar,projectionMatrix); } else { -// glFrustum (-aspect, aspect, -1.0, 1.0, 1.0, 10000.0); + b3CreateFrustum(-aspect * m_frustumZNear, aspect * m_frustumZNear, -m_frustumZNear, m_frustumZNear, m_frustumZNear, m_frustumZFar,projectionMatrix); } - - - if (m_screenWidth > m_screenHeight) - { - b3CreateFrustum(-aspect * m_frustumZNear, aspect * m_frustumZNear, -m_frustumZNear, m_frustumZNear, m_frustumZNear, m_frustumZFar,projectionMatrix); - } else - { - b3CreateFrustum(-aspect * m_frustumZNear, aspect * m_frustumZNear, -m_frustumZNear, m_frustumZNear, m_frustumZNear, m_frustumZFar,projectionMatrix); - } - b3CreateLookAt(m_data->m_cameraPosition,m_data->m_cameraTargetPosition,m_data->m_cameraUp,modelviewMatrix); + b3CreateLookAt(m_data->m_cameraPosition,m_data->m_cameraTargetPosition,m_data->m_cameraUp,modelviewMatrix); - + } @@ -1121,7 +1123,7 @@ void writeTextureToPng(int textureWidth, int textureHeight, const char* fileName float val = orgPixels[(j*textureWidth+i)]; if (val!=1.f) { - //printf("val[i,j]=%f\n", i,j,val); + //printf("val[%d,%d]=%f\n", i,j,val); } pixels[(j*textureWidth+i)*numComponents]=orgPixels[(j*textureWidth+i)]*255.f; pixels[(j*textureWidth+i)*numComponents+1]=0.f;//255.f; @@ -1168,9 +1170,35 @@ void writeTextureToPng(int textureWidth, int textureHeight, const char* fileName } -void GLInstancingRenderer::renderScene(int renderMode) +void GLInstancingRenderer::renderScene() { + if (useShadowMap) + { + float color[4]={1,1,1,1}; + renderSceneInternal(B3_CREATE_SHADOWMAP_RENDERMODE); + glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); + renderSceneInternal(B3_USE_SHADOWMAP_RENDERMODE); + + } else + { + renderSceneInternal(); + } + +} + +void GLInstancingRenderer::renderSceneInternal(int renderMode) +{ + +// glEnable(GL_DEPTH_TEST); + + // Accept fragment if it closer to the camera than the former one + //glDepthFunc(GL_LESS); + + // Cull triangles which normal is not towards the camera + //glEnable(GL_CULL_FACE); + + B3_PROFILE("GLInstancingRenderer::RenderScene"); @@ -1192,29 +1220,46 @@ void GLInstancingRenderer::renderScene(int renderMode) { if (!m_data->m_shadowMap) { + glActiveTexture(GL_TEXTURE0); + glGenTextures(1,&m_data->m_shadowTexture); glBindTexture(GL_TEXTURE_2D,m_data->m_shadowTexture); //glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT16,m_screenWidth,m_screenHeight,0,GL_DEPTH_COMPONENT,GL_FLOAT,0); - glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT32,m_screenWidth,m_screenHeight,0,GL_DEPTH_COMPONENT,GL_FLOAT,0); + //glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT32,m_screenWidth,m_screenHeight,0,GL_DEPTH_COMPONENT,GL_FLOAT,0); + glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT16, shadowMapWidth, shadowMapHeight, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + + float l_ClampColor[] = {1.0, 1.0, 1.0, 1.0}; + glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, l_ClampColor); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); m_data->m_shadowMap=new GLRenderToTexture(); - m_data->m_shadowMap->init(m_screenWidth,m_screenHeight,m_data->m_shadowTexture,RENDERTEXTURE_DEPTH); + m_data->m_shadowMap->init(shadowMapWidth, shadowMapHeight,m_data->m_shadowTexture,RENDERTEXTURE_DEPTH); } m_data->m_shadowMap->enable(); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glViewport(0,0,shadowMapWidth,shadowMapHeight); + //glClearColor(1,1,1,1); + glClear(GL_DEPTH_BUFFER_BIT); + //glClearColor(0.3,0.3,0.3,1); + +// m_data->m_shadowMap->disable(); + // return; + // glEnable(GL_CULL_FACE); +// glCullFace(GL_BACK); // Cull back-facing triangles -> draw only front-facing triangles + GLint err = glGetError(); b3Assert(err==GL_NO_ERROR); - } - - b3Vector3 lightPos(0.5f,2,2);//20,15,10);//-13,6,2);// = b3Vector3(0.5f,2,2); - b3CreateOrtho(-10,10,-10,10,-10,200,depthProjectionMatrix);//-14,14,-14,14,1,200, depthProjectionMatrix); + } + static b3Vector3 lightPos(-5.f,200,-40);//20,15,10);//-13,6,2);// = b3Vector3(0.5f,2,2); +// lightPos.y+=0.1f; + b3CreateOrtho(-100,100,-100,100,1,300,depthProjectionMatrix);//-14,14,-14,14,1,200, depthProjectionMatrix); float depthViewMatrix[4][4]; b3Vector3 center(0,0,0); b3Vector3 up(0,1,0); @@ -1240,8 +1285,8 @@ void GLInstancingRenderer::renderScene(int renderMode) b3Matrix4x4Mul(biasMatrix,depthMVP,depthBiasMVP); - float m_frustumZNear=1; - float m_frustumZFar=10000.f; + //float m_frustumZNear=0.1; + //float m_frustumZFar=100.f; @@ -1316,7 +1361,7 @@ void GLInstancingRenderer::renderScene(int renderMode) b3GraphicsInstance* gfxObj = m_graphicsInstances[i]; if (gfxObj->m_numGraphicsInstances) { - + glActiveTexture(GL_TEXTURE0); GLuint curBindTexture = 0; if (gfxObj->m_texturehandle) curBindTexture = gfxObj->m_texturehandle; @@ -1421,9 +1466,7 @@ void GLInstancingRenderer::renderScene(int renderMode) case B3_CREATE_SHADOWMAP_RENDERMODE: { glUseProgram(createShadowMapInstancingShader); - glUniformMatrix4fv(createShadow_ProjectionMatrix, 1, false, &depthProjectionMatrix[0][0]); - glUniformMatrix4fv(createShadow_ModelViewMatrix, 1, false, &depthModelViewMatrix[0][0]); - glUniform1i(createShadow_uniform_texture_diffuse, 0); + glUniformMatrix4fv(createShadow_depthMVP, 1, false, &depthMVP[0][0]); glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, (void*)indexOffset, gfxObj->m_numGraphicsInstances); break; } @@ -1433,20 +1476,18 @@ void GLInstancingRenderer::renderScene(int renderMode) glUseProgram(useShadowMapInstancingShader); glUniformMatrix4fv(useShadow_ProjectionMatrix, 1, false, &projectionMatrix[0]); glUniformMatrix4fv(useShadow_ModelViewMatrix, 1, false, &modelviewMatrix[0]); + float MVP[16]; + b3Matrix4x4Mul16(projectionMatrix,modelviewMatrix,MVP); + glUniformMatrix4fv(useShadow_MVP, 1, false, &MVP[0]); glUniformMatrix4fv(useShadow_DepthBiasModelViewMatrix, 1, false, &depthBiasMVP[0][0]); - - - glUniform1i(useShadow_uniform_texture_diffuse, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, m_data->m_shadowTexture); glUniform1i(useShadow_shadowMap,1); - glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, (void*)indexOffset, gfxObj->m_numGraphicsInstances); break; } default: { - // b3Assert(0); } }; @@ -1461,10 +1502,9 @@ void GLInstancingRenderer::renderScene(int renderMode) if (renderMode==B3_CREATE_SHADOWMAP_RENDERMODE) { - //writeTextureToPng(m_screenWidth,m_screenHeight,"shadowmap.png",4); + // writeTextureToPng(shadowMapWidth,shadowMapHeight,"shadowmap.png",4); m_data->m_shadowMap->disable(); - - + glViewport(0,0,m_screenWidth,m_screenHeight); } err = glGetError(); @@ -1488,3 +1528,12 @@ void GLInstancingRenderer::renderScene(int renderMode) void GLInstancingRenderer::CleanupShaders() { } + +void GLInstancingRenderer::enableShadowMap() +{ + glActiveTexture(GL_TEXTURE0); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, m_data->m_shadowTexture); + //glBindTexture(GL_TEXTURE_2D, m_data->m_defaultTexturehandle); + +} diff --git a/btgui/OpenGLWindow/GLInstancingRenderer.h b/btgui/OpenGLWindow/GLInstancingRenderer.h index 04ac19019..adb9786a9 100644 --- a/btgui/OpenGLWindow/GLInstancingRenderer.h +++ b/btgui/OpenGLWindow/GLInstancingRenderer.h @@ -52,6 +52,8 @@ class GLInstancingRenderer int m_screenWidth; int m_screenHeight; + void renderSceneInternal(int renderMode=B3_DEFAULT_RENDERMODE); + public: GLInstancingRenderer(int m_maxObjectCapacity, int maxShapeCapacityInBytes = 56*1024*1024); @@ -59,8 +61,9 @@ public: void init(); + void renderScene(); + void InitShaders(); - void renderScene(int renderMode=B3_DEFAULT_RENDERMODE); void CleanupShaders(); void updateShape(int shapeIndex, const float* vertices); @@ -117,6 +120,8 @@ public: { return m_maxNumObjectCapacity; } + void enableShadowMap(); + }; #endif //GL_INSTANCING_RENDERER_H diff --git a/btgui/OpenGLWindow/GLPrimitiveRenderer.cpp b/btgui/OpenGLWindow/GLPrimitiveRenderer.cpp index e1feff344..fbc2c4b3a 100644 --- a/btgui/OpenGLWindow/GLPrimitiveRenderer.cpp +++ b/btgui/OpenGLWindow/GLPrimitiveRenderer.cpp @@ -207,6 +207,7 @@ GLPrimitiveRenderer::~GLPrimitiveRenderer() glUseProgram(0); glBindTexture(GL_TEXTURE_2D,0); glDeleteProgram(m_data->m_shaderProg); + delete m_data; } void GLPrimitiveRenderer::drawLine() diff --git a/btgui/OpenGLWindow/GLPrimitiveRenderer.h b/btgui/OpenGLWindow/GLPrimitiveRenderer.h index 1aef1d06b..fd83601ad 100644 --- a/btgui/OpenGLWindow/GLPrimitiveRenderer.h +++ b/btgui/OpenGLWindow/GLPrimitiveRenderer.h @@ -26,6 +26,7 @@ public: { return m_data; } + }; #endif//_GL_PRIMITIVE_RENDERER_H diff --git a/btgui/OpenGLWindow/GLRenderToTexture.cpp b/btgui/OpenGLWindow/GLRenderToTexture.cpp index acbf4c3e9..332967e5e 100644 --- a/btgui/OpenGLWindow/GLRenderToTexture.cpp +++ b/btgui/OpenGLWindow/GLRenderToTexture.cpp @@ -15,7 +15,16 @@ void GLRenderToTexture::init(int width, int height, GLuint textureId, int render glGenFramebuffers(1, &m_framebufferName); glBindFramebuffer(GL_FRAMEBUFFER, m_framebufferName); - switch (m_renderTextureType) + + + // The depth buffer +// glGenRenderbuffers(1, &m_depthrenderbuffer); + +// glBindRenderbuffer(GL_RENDERBUFFER, m_depthrenderbuffer); +// glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); +// glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthrenderbuffer); + + switch (m_renderTextureType) { case RENDERTEXTURE_COLOR: { @@ -32,14 +41,7 @@ void GLRenderToTexture::init(int width, int height, GLuint textureId, int render b3Assert(0); } }; - - // The depth buffer - glGenRenderbuffers(1, &m_depthrenderbuffer); - - glBindRenderbuffer(GL_RENDERBUFFER, m_depthrenderbuffer); - glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthrenderbuffer); glBindFramebuffer( GL_FRAMEBUFFER, 0 ); diff --git a/btgui/OpenGLWindow/Shaders/createShadowMapInstancingVS.h b/btgui/OpenGLWindow/Shaders/createShadowMapInstancingVS.h index de4f3c49e..a241b07da 100644 --- a/btgui/OpenGLWindow/Shaders/createShadowMapInstancingVS.h +++ b/btgui/OpenGLWindow/Shaders/createShadowMapInstancingVS.h @@ -13,8 +13,7 @@ static const char* createShadowMapInstancingVertexShader= \ "layout (location = 6) in vec3 instance_scale;\n" "\n" "\n" -"uniform mat4 ModelViewMatrix;\n" -"uniform mat4 ProjectionMatrix;\n" +"uniform mat4 depthMVP;\n" "\n" "\n" "vec4 quatMul ( in vec4 q1, in vec4 q2 )\n" @@ -52,7 +51,7 @@ static const char* createShadowMapInstancingVertexShader= \ "{\n" " vec4 q = instance_quaternion;\n" " vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n" -" vec4 vertexPos = ProjectionMatrix * ModelViewMatrix * vec4( (instance_position+localcoord).xyz,1);\n" +" vec4 vertexPos = depthMVP * vec4( (instance_position+localcoord).xyz,1);\n" " gl_Position = vertexPos;\n" "}\n" "\n" diff --git a/btgui/OpenGLWindow/Shaders/createshadowMapInstancingVS.glsl b/btgui/OpenGLWindow/Shaders/createshadowMapInstancingVS.glsl index c8eb57fe7..86bbd6e8f 100644 --- a/btgui/OpenGLWindow/Shaders/createshadowMapInstancingVS.glsl +++ b/btgui/OpenGLWindow/Shaders/createshadowMapInstancingVS.glsl @@ -11,8 +11,7 @@ layout (location = 5) in vec4 instance_color; layout (location = 6) in vec3 instance_scale; -uniform mat4 ModelViewMatrix; -uniform mat4 ProjectionMatrix; +uniform mat4 depthMVP; vec4 quatMul ( in vec4 q1, in vec4 q2 ) @@ -50,7 +49,7 @@ void main(void) { vec4 q = instance_quaternion; vec4 localcoord = quatRotate3( position.xyz*instance_scale,q); - vec4 vertexPos = ProjectionMatrix * ModelViewMatrix * vec4( (instance_position+localcoord).xyz,1); + vec4 vertexPos = depthMVP * vec4( (instance_position+localcoord).xyz,1); gl_Position = vertexPos; } diff --git a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl index b213bdac5..3e92fba96 100644 --- a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl +++ b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl @@ -26,15 +26,31 @@ void main(void) vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);//fragment.color; vec3 ct,cf; float intensity,at,af; - intensity = max(dot(lightDir,normalize(normal)),0); - cf = intensity*vec3(1.0,1.0,1.0)+ambient; + + intensity = clamp( dot( normalize(normal),lightDir ), 0,1 ); + + cf = ambient; af = 1.0; ct = texel.rgb; at = texel.a; - float bias = 0.005f; + //float bias = 0.005f; + + float bias = 0.005*tan(acos(intensity)); + bias = clamp(bias, 0,0.01); + + float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z-bias)/ShadowCoord.w)); - color = vec4(ct * cf * visibility, at * af); + intensity*=2; + if (intensity>1) + intensity=1.f; + + visibility *= intensity; + + if (visibility<0.6) + visibility=0.6f; + + color = vec4(ct * visibility, 1.f);//at * af); } diff --git a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h index ba1e5ebab..594ab3c04 100644 --- a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h +++ b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h @@ -28,17 +28,33 @@ static const char* useShadowMapInstancingFragmentShader= \ " vec4 texel = fragment.color*texture(Diffuse,vert.texcoord);//fragment.color;\n" " vec3 ct,cf;\n" " float intensity,at,af;\n" -" intensity = max(dot(lightDir,normalize(normal)),0);\n" -" cf = intensity*vec3(1.0,1.0,1.0)+ambient;\n" +" \n" +" intensity = clamp( dot( normalize(normal),lightDir ), 0,1 );\n" +" \n" +" cf = ambient;\n" " af = 1.0;\n" " \n" " ct = texel.rgb;\n" " at = texel.a;\n" " \n" -" float bias = 0.005f;\n" +" //float bias = 0.005f;\n" +" \n" +" float bias = 0.005*tan(acos(intensity));\n" +" bias = clamp(bias, 0,0.01);\n" +"\n" +"\n" " float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z-bias)/ShadowCoord.w));\n" " \n" -" color = vec4(ct * cf * visibility, at * af); \n" +" intensity*=2;\n" +" if (intensity>1)\n" +" intensity=1.f;\n" +" \n" +" visibility *= intensity;\n" +" \n" +" if (visibility<0.6)\n" +" visibility=0.6f;\n" +" \n" +" color = vec4(ct * visibility, 1.f);//at * af); \n" "}\n" "\n" ; diff --git a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl index 3717b52b5..7d175fb57 100644 --- a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl +++ b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.glsl @@ -1,5 +1,5 @@ -#version 330 core -//precision highp float; +#version 330 +precision highp float; layout (location = 0) in vec4 position; @@ -14,6 +14,7 @@ layout (location = 6) in vec3 instance_scale; uniform mat4 ModelViewMatrix; uniform mat4 ProjectionMatrix; uniform mat4 DepthBiasModelViewProjectionMatrix; +uniform mat4 MVP; out vec4 ShadowCoord; @@ -66,16 +67,15 @@ void main(void) ambient = vec3(0.3,.3,0.3); - vec4 local_normal = (quatRotate3( vertexnormal,q)); - vec3 light_pos = vec3(-0.3,0.1,0.1); - normal = local_normal.xyz;//normalize(ModelViewMatrix * local_normal).xyz; + vec4 worldNormal = (quatRotate3( vertexnormal,q)); + vec3 light_pos = vec3(-5.f,100,-40); + normal = normalize(worldNormal).xyz; lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz)); -// lightDir = normalize(vec3(gl_LightSource[0].position)); vec4 axis = vec4(1,1,1,0); vec4 localcoord = quatRotate3( position.xyz*instance_scale,q); - vec4 vertexPos = ProjectionMatrix * ModelViewMatrix * vec4((instance_position+localcoord).xyz,1); + vec4 vertexPos = MVP* vec4((instance_position+localcoord).xyz,1); gl_Position = vertexPos; ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1); diff --git a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h index 6ae70b98f..1ba4d903d 100644 --- a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h +++ b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingVS.h @@ -1,7 +1,7 @@ //this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project static const char* useShadowMapInstancingVertexShader= \ -"#version 330 core\n" -"//precision highp float;\n" +"#version 330 \n" +"precision highp float;\n" "\n" "\n" "layout (location = 0) in vec4 position;\n" @@ -16,6 +16,7 @@ static const char* useShadowMapInstancingVertexShader= \ "uniform mat4 ModelViewMatrix;\n" "uniform mat4 ProjectionMatrix;\n" "uniform mat4 DepthBiasModelViewProjectionMatrix;\n" +"uniform mat4 MVP;\n" "\n" "out vec4 ShadowCoord;\n" "\n" @@ -68,16 +69,15 @@ static const char* useShadowMapInstancingVertexShader= \ " ambient = vec3(0.3,.3,0.3);\n" " \n" " \n" -" vec4 local_normal = (quatRotate3( vertexnormal,q));\n" -" vec3 light_pos = vec3(-0.3,0.1,0.1);\n" -" normal = local_normal.xyz;//normalize(ModelViewMatrix * local_normal).xyz;\n" +" vec4 worldNormal = (quatRotate3( vertexnormal,q));\n" +" vec3 light_pos = vec3(-5.f,100,-40);\n" +" normal = normalize(worldNormal).xyz;\n" "\n" " lightDir = normalize(light_pos);//gl_LightSource[0].position.xyz));\n" -"// lightDir = normalize(vec3(gl_LightSource[0].position));\n" " \n" " vec4 axis = vec4(1,1,1,0);\n" " vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);\n" -" vec4 vertexPos = ProjectionMatrix * ModelViewMatrix * vec4((instance_position+localcoord).xyz,1);\n" +" vec4 vertexPos = MVP* vec4((instance_position+localcoord).xyz,1);\n" "\n" " gl_Position = vertexPos;\n" " ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);\n" diff --git a/src/Bullet3OpenCL/RigidBody/b3GpuBatchingPgsSolver.cpp b/src/Bullet3OpenCL/RigidBody/b3GpuBatchingPgsSolver.cpp index 9fdb4c3a6..5d4720206 100644 --- a/src/Bullet3OpenCL/RigidBody/b3GpuBatchingPgsSolver.cpp +++ b/src/Bullet3OpenCL/RigidBody/b3GpuBatchingPgsSolver.cpp @@ -193,13 +193,20 @@ b3GpuBatchingPgsSolver::b3GpuBatchingPgsSolver(cl_context ctx,cl_device_id devic b3GpuBatchingPgsSolver::~b3GpuBatchingPgsSolver() { + delete m_data->m_bodyBufferGPU; + delete m_data->m_inertiaBufferGPU; + delete m_data->m_pBufContactOutGPU; + + delete m_data->m_contactCGPU; + delete m_data->m_numConstraints; + delete m_data->m_offsets; delete m_data->m_sortDataBuffer; delete m_data->m_contactBuffer; delete m_data->m_sort32; delete m_data->m_scan; delete m_data->m_search; - + delete m_data->m_solverGPU; clReleaseKernel(m_data->m_batchingKernel); clReleaseKernel(m_data->m_batchingKernelNew); diff --git a/src/Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.cpp b/src/Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.cpp index 5132c4d99..e5a08376e 100644 --- a/src/Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.cpp +++ b/src/Bullet3OpenCL/RigidBody/b3GpuNarrowPhase.cpp @@ -135,6 +135,16 @@ b3GpuNarrowPhase::~b3GpuNarrowPhase() delete m_data->m_bvhInfoGPU; + for (int i=0;im_bvhData.size();i++) + { + delete m_data->m_bvhData[i]; + } + for (int i=0;im_meshInterfaces.size();i++) + { + delete m_data->m_meshInterfaces[i]; + } + m_data->m_meshInterfaces.clear(); + m_data->m_bvhData.clear(); delete m_data->m_treeNodesGPU; delete m_data->m_subTreesGPU; @@ -334,6 +344,7 @@ int b3GpuNarrowPhase::registerConvexHullShape(const float* vertices, int stride } int collidableIndex = registerConvexHullShape(utilPtr); + delete utilPtr; return collidableIndex; } @@ -505,6 +516,7 @@ int b3GpuNarrowPhase::registerConcaveMesh(b3AlignedObjectArray* vert bool useQuantizedAabbCompression = true; b3TriangleIndexVertexArray* meshInterface=new b3TriangleIndexVertexArray(); + m_data->m_meshInterfaces.push_back(meshInterface); b3IndexedMesh mesh; mesh.m_numTriangles = indices->size()/3; mesh.m_numVertices = vertices->size(); diff --git a/src/Bullet3OpenCL/RigidBody/b3GpuNarrowPhaseInternalData.h b/src/Bullet3OpenCL/RigidBody/b3GpuNarrowPhaseInternalData.h index 8c7aa807d..bdcf8405d 100644 --- a/src/Bullet3OpenCL/RigidBody/b3GpuNarrowPhaseInternalData.h +++ b/src/Bullet3OpenCL/RigidBody/b3GpuNarrowPhaseInternalData.h @@ -75,6 +75,7 @@ struct b3GpuNarrowPhaseInternalData b3AlignedObjectArray* m_localShapeAABBCPU; b3AlignedObjectArray m_bvhData; + b3AlignedObjectArray m_meshInterfaces; b3AlignedObjectArray m_treeNodesCPU; b3AlignedObjectArray m_subTreesCPU; diff --git a/src/Bullet3OpenCL/RigidBody/b3Solver.cpp b/src/Bullet3OpenCL/RigidBody/b3Solver.cpp index b5b8680ef..2ccb0ad3d 100644 --- a/src/Bullet3OpenCL/RigidBody/b3Solver.cpp +++ b/src/Bullet3OpenCL/RigidBody/b3Solver.cpp @@ -176,6 +176,8 @@ b3Solver::b3Solver(cl_context ctx, cl_device_id device, cl_command_queue queue, b3Solver::~b3Solver() { + delete m_offsets; + delete m_numConstraints; delete m_sortDataBuffer; delete m_contactBuffer2;