From eda1ff77db79910ed84c2b4372527eabeb3df53f Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Wed, 15 Oct 2014 18:05:11 -0700 Subject: [PATCH] add alpha blendering option (disabled by default) using w component of color add low-level support function demo (test) --- Demos3/AllBullet2Demos/BulletDemoEntries.h | 7 ++ .../CollisionDetection/SupportFuncDemo.h | 112 ++++++++++++++++++ btgui/OpenGLWindow/CommonGraphicsApp.h | 2 +- btgui/OpenGLWindow/CommonRenderInterface.h | 1 + btgui/OpenGLWindow/GLInstancingRenderer.cpp | 24 +++- btgui/OpenGLWindow/GLInstancingRenderer.h | 7 +- .../Shaders/useShadowMapInstancingPS.glsl | 2 +- .../Shaders/useShadowMapInstancingPS.h | 2 +- 8 files changed, 147 insertions(+), 10 deletions(-) create mode 100644 Demos3/bullet2/CollisionDetection/SupportFuncDemo.h diff --git a/Demos3/AllBullet2Demos/BulletDemoEntries.h b/Demos3/AllBullet2Demos/BulletDemoEntries.h index 6895703b0..9bd0c06b2 100644 --- a/Demos3/AllBullet2Demos/BulletDemoEntries.h +++ b/Demos3/AllBullet2Demos/BulletDemoEntries.h @@ -20,6 +20,7 @@ #include "../ImportSTLDemo/ImportSTLSetup.h" #include "../../Demos/SerializeDemo/SerializeSetup.h" #include "../bullet2/MultiBodyDemo/TestJointTorqueSetup.h" +#include "../bullet2/CollisionDetection/SupportFuncDemo.h" static BulletDemoInterface* TestJointTorqueCreateFunc(CommonGraphicsApp* app) { @@ -72,6 +73,8 @@ static BulletDemoInterface* MyImportSTLCreateFunc(CommonGraphicsApp* app) } + + struct BulletDemoEntry { int m_menuLevel; @@ -83,9 +86,13 @@ struct BulletDemoEntry static BulletDemoEntry allDemos[]= { + {0,"LowLevel",0}, + {1,"SupportFunc", &MySupportFuncDemo::CreateFunc}, + //{"emptydemo",EmptyBulletDemo::MyCreateFunc}, {0,"API Demos", 0}, + {1,"BasicDemo",BasicDemo::MyCreateFunc}, { 1, "CcdDemo", MyCcdPhysicsDemoCreateFunc }, { 1, "Kinematic", MyKinematicObjectCreateFunc }, diff --git a/Demos3/bullet2/CollisionDetection/SupportFuncDemo.h b/Demos3/bullet2/CollisionDetection/SupportFuncDemo.h new file mode 100644 index 000000000..6724be321 --- /dev/null +++ b/Demos3/bullet2/CollisionDetection/SupportFuncDemo.h @@ -0,0 +1,112 @@ +#ifndef SUPPORT_FUNC_DEMO_H +#define SUPPORT_FUNC_DEMO_H + +#include "Bullet3AppSupport/BulletDemoInterface.h" +#include "OpenGLWindow/CommonGraphicsApp.h" +#include "BulletCollision/CollisionShapes/btSphereShape.h" + +class MySupportFuncDemo : public BulletDemoInterface +{ + CommonGraphicsApp* m_app; + btSphereShape* m_sphere; + float m_x; + float m_y; + + +public: + + MySupportFuncDemo(CommonGraphicsApp* app) + :m_app(app), + m_x(0), + m_y(0) + { + m_sphere = new btSphereShape(1); + { + int boxId = m_app->registerCubeShape(10,0.1,10); + btVector3 pos(0,-2,0); + btQuaternion orn(0,0,0,1); + btVector4 color(0.3,0.3,0.3,1); + + btVector3 scaling(1,1,1); + m_app->m_renderer->registerGraphicsInstance(boxId,pos,orn,color,scaling); + } + + { + int sphereId = m_app->registerGraphicsSphereShape(1,false,0,0); + btVector3 pos(0,0,0); + btQuaternion orn(0,0,0,1); + btVector4 color(0,1,0,0.6); + + btVector3 scaling(1,1,1); + m_app->m_renderer->registerGraphicsInstance(sphereId,pos,orn,color,scaling); + } + m_app->m_renderer->writeTransforms(); + m_app->m_renderer->enableBlend(true); + } + virtual ~MySupportFuncDemo() + { + m_app->m_renderer->enableBlend(false); + delete m_sphere; + } + static BulletDemoInterface* CreateFunc(CommonGraphicsApp* app) + { + return new MySupportFuncDemo(app); + } + + virtual void initPhysics() + { + } + virtual void exitPhysics() + { + + } + virtual void stepSimulation(float deltaTime) + { + m_x+=0.01f; + m_y+=0.02f; + + } + virtual void renderScene() + { + m_app->m_renderer->renderScene(); + + } + virtual void physicsDebugDraw() + { + int width=3; + btVector3 from(0,0,0); + btVector3 to(10.*btSin(m_x),10.*btCos(m_x),10.*btSin(m_y)); + + + { + btVector3 color(1,0,0); + m_app->m_renderer->drawLine(from,to,color,width); + } + btVector3 dir(to-from); + btVector3 sup = m_sphere->btConvexInternalShape::localGetSupportingVertex(dir); + btVector3 orth0,orth1; + btPlaneSpace1(dir,orth0,orth1); + btVector3 color(0,0,1); + orth0.normalize(); + orth1.normalize(); + + m_app->m_renderer->drawLine(sup,sup+orth0*0.4,color,3); + m_app->m_renderer->drawLine(sup,sup+orth1*0.4,color,3); + + } + virtual bool mouseMoveCallback(float x,float y) + { + + } + virtual bool mouseButtonCallback(int button, int state, float x, float y) + { + + } + virtual bool keyboardCallback(int key, int state) + { + + } + +}; +#endif //SUPPORT_FUNC_DEMO + diff --git a/btgui/OpenGLWindow/CommonGraphicsApp.h b/btgui/OpenGLWindow/CommonGraphicsApp.h index ced0ac262..953fdeca7 100644 --- a/btgui/OpenGLWindow/CommonGraphicsApp.h +++ b/btgui/OpenGLWindow/CommonGraphicsApp.h @@ -44,7 +44,7 @@ struct CommonGraphicsApp virtual void drawText( const char* txt, int posX, int posY) = 0; virtual int registerCubeShape(float halfExtentsX,float halfExtentsY, float halfExtentsZ)=0; - virtual int registerGraphicsSphereShape(float radius, bool usePointSprites, int largeSphereThreshold, int mediumSphereThreshold)=0; + virtual int registerGraphicsSphereShape(float radius, bool usePointSprites=true, int largeSphereThreshold=100, int mediumSphereThreshold=10)=0; }; diff --git a/btgui/OpenGLWindow/CommonRenderInterface.h b/btgui/OpenGLWindow/CommonRenderInterface.h index 5b4254ea4..f1bd591a2 100644 --- a/btgui/OpenGLWindow/CommonRenderInterface.h +++ b/btgui/OpenGLWindow/CommonRenderInterface.h @@ -45,6 +45,7 @@ struct CommonRenderInterface virtual void writeSingleInstanceTransformToCPU(const float* position, const float* orientation, int srcIndex)=0; virtual void writeSingleInstanceTransformToCPU(const double* position, const double* orientation, int srcIndex)=0; virtual void writeTransforms()=0; + virtual void enableBlend(bool blend)=0; }; #endif//COMMON_RENDER_INTERFACE_H diff --git a/btgui/OpenGLWindow/GLInstancingRenderer.cpp b/btgui/OpenGLWindow/GLInstancingRenderer.cpp index 24eec1669..0e5d4d571 100644 --- a/btgui/OpenGLWindow/GLInstancingRenderer.cpp +++ b/btgui/OpenGLWindow/GLInstancingRenderer.cpp @@ -380,7 +380,8 @@ GLInstancingRenderer::GLInstancingRenderer(int maxNumObjectCapacity, int maxShap m_textureinitialized(false), m_screenWidth(0), m_screenHeight(0), - m_upAxis(1) + m_upAxis(1), + m_enableBlend(false) { m_data = new InternalDataRenderer; @@ -1593,14 +1594,14 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode) // m_data->m_shadowMap->disable(); // return; - // glEnable(GL_CULL_FACE); -// glCullFace(GL_BACK); // Cull back-facing triangles -> draw only front-facing triangles + glEnable(GL_CULL_FACE); + glCullFace(GL_BACK); // Cull back-facing triangles -> draw only front-facing triangles b3Assert(glGetError() ==GL_NO_ERROR); } else { - glDisable(GL_CULL_FACE); - //glCullFace(GL_BACK); + //glDisable(GL_CULL_FACE); + glCullFace(GL_BACK); } @@ -1794,6 +1795,12 @@ b3Assert(glGetError() ==GL_NO_ERROR); case B3_USE_SHADOWMAP_RENDERMODE: { + if (m_enableBlend) + { + glEnable (GL_BLEND); + glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + } + glUseProgram(useShadowMapInstancingShader); glUniformMatrix4fv(useShadow_ProjectionMatrix, 1, false, &projectionMatrix[0]); glUniformMatrix4fv(useShadow_ModelViewMatrix, 1, false, &modelviewMatrix[0]); @@ -1808,7 +1815,12 @@ b3Assert(glGetError() ==GL_NO_ERROR); glBindTexture(GL_TEXTURE_2D, m_data->m_shadowTexture); glUniform1i(useShadow_shadowMap,1); glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances); - break; + if (m_enableBlend) + { + glDisable (GL_BLEND); + } + + break; } default: { diff --git a/btgui/OpenGLWindow/GLInstancingRenderer.h b/btgui/OpenGLWindow/GLInstancingRenderer.h index cd55995d8..d4ffd9360 100644 --- a/btgui/OpenGLWindow/GLInstancingRenderer.h +++ b/btgui/OpenGLWindow/GLInstancingRenderer.h @@ -43,7 +43,8 @@ class GLInstancingRenderer : public CommonRenderInterface int m_screenHeight; int m_upAxis; - + bool m_enableBlend; + void renderSceneInternal(int renderMode=B3_DEFAULT_RENDERMODE); @@ -156,6 +157,10 @@ public: return m_maxNumObjectCapacity; } void enableShadowMap(); + virtual void enableBlend(bool blend) + { + m_enableBlend = blend; + } }; diff --git a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl index e4fc56d02..c4913d702 100644 --- a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl +++ b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.glsl @@ -52,5 +52,5 @@ void main(void) if (visibility<0.6) visibility=0.6f; - color = vec4(ct * visibility, 1.f);//at * af); + color = vec4(ct * visibility, fragment.color.w); } diff --git a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h index ab36f9313..d43ec5dcb 100644 --- a/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h +++ b/btgui/OpenGLWindow/Shaders/useShadowMapInstancingPS.h @@ -44,6 +44,6 @@ static const char* useShadowMapInstancingFragmentShader= \ " if (visibility<0.6)\n" " visibility=0.6f;\n" " \n" -" color = vec4(ct * visibility, 1.f);//at * af); \n" +" color = vec4(ct * visibility, fragment.color.w);\n" "}\n" ;