PyBullet: add planar reflection example,

See examples/pybullet/examples/addPlanarReflection.py
This commit is contained in:
erwincoumans
2018-04-11 01:03:36 -07:00
parent 39edcf699c
commit ffc808784b
11 changed files with 155 additions and 15 deletions

View File

@@ -361,7 +361,8 @@ GLInstancingRenderer::GLInstancingRenderer(int maxNumObjectCapacity, int maxShap
m_textureinitialized(false),
m_screenWidth(0),
m_screenHeight(0),
m_upAxis(1)
m_upAxis(1),
m_planeReflectionShapeIndex(-1)
{
m_data = new InternalDataRenderer;
@@ -1597,13 +1598,44 @@ void GLInstancingRenderer::renderScene()
{
renderSceneInternal(B3_CREATE_SHADOWMAP_RENDERMODE);
//glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//renderSceneInternal(B3_USE_SHADOWMAP_RENDERMODE_REFLECTION);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
if (m_planeReflectionShapeIndex>=0)
{
/* Don't update color or depth. */
glDisable(GL_DEPTH_TEST);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
/* Draw 1 into the stencil buffer. */
glEnable(GL_STENCIL_TEST);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glStencilFunc(GL_ALWAYS, 1, 0xffffffff);
/* Now render floor; floor pixels just get their stencil set to 1. */
renderSceneInternal(B3_USE_SHADOWMAP_RENDERMODE_REFLECTION_PLANE);
/* Re-enable update of color and depth. */
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glEnable(GL_DEPTH_TEST);
/* Now, only render where stencil is set to 1. */
glStencilFunc(GL_EQUAL, 1, 0xffffffff); /* draw if ==1 */
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
//draw the reflection objects
renderSceneInternal(B3_USE_SHADOWMAP_RENDERMODE_REFLECTION);
glDisable(GL_STENCIL_TEST);
}
renderSceneInternal(B3_USE_SHADOWMAP_RENDERMODE);
}
else if (m_data->m_useProjectiveTexture)
{
renderSceneInternal(B3_USE_PROJECTIVE_TEXTURE_RENDERMODE);
}
else
@@ -2040,6 +2072,13 @@ void GLInstancingRenderer::renderSceneInternal(int orgRenderMode)
{
int renderMode=orgRenderMode;
bool reflectionPass = false;
bool reflectionPlanePass = false;
if (orgRenderMode==B3_USE_SHADOWMAP_RENDERMODE_REFLECTION_PLANE)
{
reflectionPlanePass = true;
renderMode = B3_USE_SHADOWMAP_RENDERMODE;
}
if (orgRenderMode==B3_USE_SHADOWMAP_RENDERMODE_REFLECTION)
{
reflectionPass = true;
@@ -2085,8 +2124,8 @@ void GLInstancingRenderer::renderSceneInternal(int orgRenderMode)
//GLfloat depthModelViewMatrix2[4][4];
// For projective texture mapping
float textureProjectionMatrix[4][4];
GLfloat textureModelViewMatrix[4][4];
//float textureProjectionMatrix[4][4];
//GLfloat textureModelViewMatrix[4][4];
// Compute the MVP matrix from the light's point of view
if (renderMode==B3_CREATE_SHADOWMAP_RENDERMODE)
@@ -2254,6 +2293,7 @@ b3Assert(glGetError() ==GL_NO_ERROR);
for (int obj=0;obj<m_graphicsInstances.size();obj++)
{
b3GraphicsInstance* gfxObj = m_graphicsInstances[obj];
if (gfxObj->m_numGraphicsInstances)
{
SortableTransparentInstance inst;
@@ -2300,9 +2340,13 @@ b3Assert(glGetError() ==GL_NO_ERROR);
{
for (int i=0;i<transparentInstances.size();i++)
{
int shapeIndex = transparentInstances[i].m_shapeIndex;
//during a reflectionPlanePass, only draw the plane, nothing else
if ((shapeIndex!=m_planeReflectionShapeIndex) && reflectionPlanePass)
continue;
b3GraphicsInstance* gfxObj = m_graphicsInstances[shapeIndex];
//only draw stuff (opaque/transparent) if it is the right pass
@@ -2340,7 +2384,7 @@ b3Assert(glGetError() ==GL_NO_ERROR);
curBindTexture = m_data->m_defaultTexturehandle;
}
//disable lazy evaluation, it just leads to bugs
//disable lazy evaluation, it just leads to bugs
//if (lastBindTexture != curBindTexture)
{
glBindTexture(GL_TEXTURE_2D,curBindTexture);
@@ -2503,6 +2547,8 @@ b3Assert(glGetError() ==GL_NO_ERROR);
float MVP[16];
if (reflectionPass)
{
//todo: create an API to select this reflection matrix, to allow
//reflection planes different from Z-axis up through (0,0,0)
float tmp[16];
float reflectionMatrix[16] = {1,0,0,0,
0,1,0,0,
@@ -2669,6 +2715,11 @@ void GLInstancingRenderer::CleanupShaders()
{
}
void GLInstancingRenderer::setPlaneReflectionShapeIndex(int index)
{
m_planeReflectionShapeIndex = index;
}
void GLInstancingRenderer::enableShadowMap()
{
glActiveTexture(GL_TEXTURE0);

View File

@@ -39,6 +39,8 @@ class GLInstancingRenderer : public CommonRenderInterface
int m_screenHeight;
int m_upAxis;
int m_planeReflectionShapeIndex;
int registerGraphicsInstanceInternal(int shapeIndex, const float* position, const float* quaternion, const float* color, const float* scaling);
@@ -151,6 +153,8 @@ public:
virtual int getTotalNumInstances() const;
virtual void enableShadowMap();
virtual void setPlaneReflectionShapeIndex(int index);
virtual void clearZBuffer();