Add pybullet transparent.py example, transparency with global per-object sort in GLInstancingRenderer

This commit is contained in:
erwincoumans 2017-06-24 19:38:31 -07:00
parent a651cb9ab4
commit d5fe67cf57
5 changed files with 324 additions and 219 deletions

View File

@ -0,0 +1,32 @@
<?xml version="0.0" ?>
<robot name="urdf_robot">
<link name="baseLink">
<contact>
<rolling_friction value="0.03"/>
<spinning_friction value="0.03"/>
</contact>
<inertial>
<origin rpy="0 0 0" xyz="0 0 0"/>
<mass value="10.0"/>
<inertia ixx="1" ixy="0" ixz="0" iyy="1" iyz="0" izz="1"/>
</inertial>
<visual>
<origin rpy="0 0 0" xyz="0 0 0"/>
<geometry>
<mesh filename="textured_sphere_smooth.obj" scale="0.5 0.5 0.5"/>
</geometry>
<material name="red_transparent">
<color rgba="1 0 0 0.5"/>
<specular rgb="11 1 1"/>
</material>
</visual>
<collision>
<origin rpy="0 0 0" xyz="0 0 0"/>
<geometry>
<sphere radius="0.5"/>
</geometry>
</collision>
</link>
</robot>

View File

@ -79,7 +79,6 @@ public:
gTotalPoints = 0; gTotalPoints = 0;
m_app->setUpAxis(1); m_app->setUpAxis(1);
m_app->m_renderer->enableBlend(true);
switch (m_tutorialIndex) switch (m_tutorialIndex)
{ {
@ -250,7 +249,6 @@ public:
m_timeSeriesCanvas0 = 0; m_timeSeriesCanvas0 = 0;
m_app->m_renderer->enableBlend(false);
} }

View File

@ -2046,9 +2046,9 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode)
b3Vector3 center = b3MakeVector3(0,0,0); b3Vector3 center = b3MakeVector3(0,0,0);
//float upf[3]; //float upf[3];
//m_data->m_activeCamera->getCameraUpVector(upf); //m_data->m_activeCamera->getCameraUpVector(upf);
b3Vector3 up, fwd; b3Vector3 up, lightFwd;
b3Vector3 lightDir = m_data->m_lightPos.normalized(); b3Vector3 lightDir = m_data->m_lightPos.normalized();
b3PlaneSpace1(lightDir,up,fwd); b3PlaneSpace1(lightDir,up,lightFwd);
// b3Vector3 up = b3MakeVector3(upf[0],upf[1],upf[2]); // b3Vector3 up = b3MakeVector3(upf[0],upf[1],upf[2]);
b3CreateLookAt(m_data->m_lightPos,center,up,&depthViewMatrix[0][0]); b3CreateLookAt(m_data->m_lightPos,center,up,&depthViewMatrix[0][0]);
//b3CreateLookAt(lightPos,m_data->m_cameraTargetPosition,b3Vector3(0,1,0),(float*)depthModelViewMatrix2); //b3CreateLookAt(lightPos,m_data->m_cameraTargetPosition,b3Vector3(0,1,0),(float*)depthModelViewMatrix2);
@ -2108,207 +2108,194 @@ b3Assert(glGetError() ==GL_NO_ERROR);
{ {
totalNumInstances+=m_graphicsInstances[i]->m_numGraphicsInstances; totalNumInstances+=m_graphicsInstances[i]->m_numGraphicsInstances;
} }
int curOffset = 0;
//GLuint lastBindTexture = 0;
for (int i=0;i<m_graphicsInstances.size();i++) b3AlignedObjectArray<SortableTransparentInstance> transparentInstances;
{ {
int curOffset = 0;
//GLuint lastBindTexture = 0;
b3GraphicsInstance* gfxObj = m_graphicsInstances[i]; transparentInstances.reserve(totalNumInstances);
if (gfxObj->m_numGraphicsInstances)
for (int obj=0;obj<m_graphicsInstances.size();obj++)
{ {
glActiveTexture(GL_TEXTURE0); b3GraphicsInstance* gfxObj = m_graphicsInstances[obj];
GLuint curBindTexture = 0; if (gfxObj->m_numGraphicsInstances)
if (gfxObj->m_texturehandle)
curBindTexture = gfxObj->m_texturehandle;
else
curBindTexture = m_data->m_defaultTexturehandle;
//disable lazy evaluation, it just leads to bugs
//if (lastBindTexture != curBindTexture)
{ {
glBindTexture(GL_TEXTURE_2D,curBindTexture); SortableTransparentInstance inst;
}
//lastBindTexture = curBindTexture; inst.m_shapeIndex = obj;
b3Assert(glGetError() ==GL_NO_ERROR);
// int myOffset = gfxObj->m_instanceOffset*4*sizeof(float); if ((gfxObj->m_flags&eGfxTransparency)==0)
int POSITION_BUFFER_SIZE = (totalNumInstances*sizeof(float)*4);
int ORIENTATION_BUFFER_SIZE = (totalNumInstances*sizeof(float)*4);
int COLOR_BUFFER_SIZE = (totalNumInstances*sizeof(float)*4);
// int SCALE_BUFFER_SIZE = (totalNumInstances*sizeof(float)*3);
glBindVertexArray(gfxObj->m_cube_vao);
int vertexStride = 9*sizeof(float);
PointerCaster vertex;
vertex.m_baseIndex = gfxObj->m_vertexArrayOffset*vertexStride;
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 9*sizeof(float), vertex.m_pointer);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(curOffset*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes));
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(curOffset*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE));
PointerCaster uv;
uv.m_baseIndex = 7*sizeof(float)+vertex.m_baseIndex;
PointerCaster normal;
normal.m_baseIndex = 4*sizeof(float)+vertex.m_baseIndex;
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 9*sizeof(float), uv.m_pointer);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 9*sizeof(float), normal.m_pointer);
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(curOffset*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE));
glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(curOffset*3*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE+COLOR_BUFFER_SIZE));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
glEnableVertexAttribArray(5);
glEnableVertexAttribArray(6);
glVertexAttribDivisor(0, 0);
glVertexAttribDivisor(1, 1);
glVertexAttribDivisor(2, 1);
glVertexAttribDivisor(3, 0);
glVertexAttribDivisor(4, 0);
glVertexAttribDivisor(5, 1);
glVertexAttribDivisor(6, 1);
int indexCount = gfxObj->m_numIndices;
GLvoid* indexOffset = 0;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gfxObj->m_index_vbo);
{
B3_PROFILE("glDrawElementsInstanced");
if (gfxObj->m_primitiveType==B3_GL_POINTS)
{ {
glUseProgram(instancingShaderPointSprite); inst.m_instanceId = curOffset;
glUniformMatrix4fv(ProjectionMatrixPointSprite, 1, false, &m_data->m_projectionMatrix[0]); inst.m_centerPosition.setValue(m_data->m_instance_positions_ptr[inst.m_instanceId*4+0],
glUniformMatrix4fv(ModelViewMatrixPointSprite, 1, false, &m_data->m_viewMatrix[0]); m_data->m_instance_positions_ptr[inst.m_instanceId*4+1],
glUniform1f(screenWidthPointSprite,float(m_screenWidth)); m_data->m_instance_positions_ptr[inst.m_instanceId*4+2]);
inst.m_centerPosition *= -1;//reverse sort opaque instances
//glUniform1i(uniform_texture_diffusePointSprite, 0); transparentInstances.push_back(inst);
b3Assert(glGetError() ==GL_NO_ERROR);
glPointSize(20);
#ifndef __APPLE__
glEnable(GL_POINT_SPRITE_ARB);
// glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
#endif
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
glDrawElementsInstanced(GL_POINTS, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances);
} else } else
{ {
switch (renderMode) for (int i=0;i<gfxObj->m_numGraphicsInstances;i++)
{ {
inst.m_instanceId = curOffset+i;
case B3_DEFAULT_RENDERMODE: inst.m_centerPosition.setValue(m_data->m_instance_positions_ptr[inst.m_instanceId*4+0],
{
glUseProgram(instancingShader);
glUniformMatrix4fv(ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]);
glUniformMatrix4fv(ModelViewMatrix, 1, false, &m_data->m_viewMatrix[0]);
b3Vector3 gLightDir = m_data->m_lightPos;
gLightDir.normalize();
glUniform3f(regularLightDirIn,gLightDir[0],gLightDir[1],gLightDir[2]);
glUniform1i(uniform_texture_diffuse, 0);
glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances);
break;
}
case B3_CREATE_SHADOWMAP_RENDERMODE:
{
/*printf("createShadowMapInstancingShader=%d\n",createShadowMapInstancingShader);
printf("createShadow_depthMVP=%d\n",createShadow_depthMVP);
printf("indexOffset=%d\n",indexOffset);
printf("gfxObj->m_numGraphicsInstances=%d\n",gfxObj->m_numGraphicsInstances);
printf("indexCount=%d\n",indexCount);
*/
glUseProgram(createShadowMapInstancingShader);
glUniformMatrix4fv(createShadow_depthMVP, 1, false, &depthMVP[0][0]);
glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances);
break;
}
case B3_USE_SHADOWMAP_RENDERMODE:
{
if ( gfxObj->m_flags&eGfxTransparency)
{
glDepthMask(false);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glUseProgram(useShadowMapInstancingShader);
glUniformMatrix4fv(useShadow_ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]);
glUniformMatrix4fv(useShadow_ModelViewMatrix, 1, false, &m_data->m_viewMatrix[0]);
glUniformMatrix4fv(useShadow_ViewMatrixInverse, 1, false, &m_data->m_viewMatrixInverse[0]);
glUniformMatrix4fv(useShadow_ModelViewMatrix, 1, false, &m_data->m_viewMatrix[0]);
glUniform3f(useShadow_lightSpecularIntensity, m_data->m_lightSpecularIntensity[0],m_data->m_lightSpecularIntensity[1],m_data->m_lightSpecularIntensity[2]);
glUniform3f(useShadow_materialSpecularColor, gfxObj->m_materialSpecularColor[0],gfxObj->m_materialSpecularColor[1],gfxObj->m_materialSpecularColor[2]);
float MVP[16];
b3Matrix4x4Mul16(m_data->m_projectionMatrix,m_data->m_viewMatrix,MVP);
glUniformMatrix4fv(useShadow_MVP, 1, false, &MVP[0]);
//gLightDir.normalize();
glUniform3f(useShadow_lightPosIn,m_data->m_lightPos[0],m_data->m_lightPos[1],m_data->m_lightPos[2]);
float camPos[3];
m_data->m_activeCamera->getCameraPosition(camPos);
glUniform3f(useShadow_cameraPositionIn,camPos[0],camPos[1],camPos[2]);
glUniform1f(useShadow_materialShininessIn,gfxObj->m_materialShinyNess);
glUniformMatrix4fv(useShadow_DepthBiasModelViewMatrix, 1, false, &depthBiasMVP[0][0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_data->m_shadowTexture);
glUniform1i(useShadow_shadowMap,1);
//sort transparent objects
//gfxObj->m_instanceOffset
if ( gfxObj->m_flags&eGfxTransparency)
{
b3AlignedObjectArray<SortableTransparentInstance> transparentInstances;
transparentInstances.reserve(gfxObj->m_numGraphicsInstances);
for (int i=0;i<gfxObj->m_numGraphicsInstances;i++)
{
SortableTransparentInstance inst;
inst.m_shapeIndex = -1;
inst.m_instanceId = curOffset+i;
inst.m_centerPosition.setValue(m_data->m_instance_positions_ptr[inst.m_instanceId*4+0],
m_data->m_instance_positions_ptr[inst.m_instanceId*4+1], m_data->m_instance_positions_ptr[inst.m_instanceId*4+1],
m_data->m_instance_positions_ptr[inst.m_instanceId*4+2]); m_data->m_instance_positions_ptr[inst.m_instanceId*4+2]);
transparentInstances.push_back(inst); transparentInstances.push_back(inst);
}
}
curOffset+=gfxObj->m_numGraphicsInstances;
}
}
TransparentDistanceSortPredicate sorter;
float fwd[3];
m_data->m_activeCamera->getCameraForwardVector(fwd);
sorter.m_camForwardVec.setValue(fwd[0],fwd[1],fwd[2]);
transparentInstances.quickSort(sorter);
}
}
TransparentDistanceSortPredicate sorter; //two passes: first for opaque instances, second for transparent ones.
float fwd[3]; for (int pass = 0; pass<2;pass++)
m_data->m_activeCamera->getCameraForwardVector(fwd); {
sorter.m_camForwardVec.setValue(fwd[0],fwd[1],fwd[2]); for (int i=0;i<transparentInstances.size();i++)
transparentInstances.quickSort(sorter); {
int shapeIndex = transparentInstances[i].m_shapeIndex;
b3GraphicsInstance* gfxObj = m_graphicsInstances[shapeIndex];
//only draw stuff (opaque/transparent) if it is the right pass
int drawThisPass = (pass==0) == ((gfxObj->m_flags&eGfxTransparency)==0);
//transparent objects don't cast shadows (to simplify things)
if (gfxObj->m_flags&eGfxTransparency)
{
if (renderMode==B3_CREATE_SHADOWMAP_RENDERMODE)
drawThisPass = 0;
}
if (drawThisPass && gfxObj->m_numGraphicsInstances)
{
glActiveTexture(GL_TEXTURE0);
GLuint curBindTexture = 0;
if (gfxObj->m_texturehandle)
curBindTexture = gfxObj->m_texturehandle;
else
curBindTexture = m_data->m_defaultTexturehandle;
//disable lazy evaluation, it just leads to bugs
//if (lastBindTexture != curBindTexture)
{
glBindTexture(GL_TEXTURE_2D,curBindTexture);
}
//lastBindTexture = curBindTexture;
b3Assert(glGetError() ==GL_NO_ERROR);
// int myOffset = gfxObj->m_instanceOffset*4*sizeof(float);
int POSITION_BUFFER_SIZE = (totalNumInstances*sizeof(float)*4);
int ORIENTATION_BUFFER_SIZE = (totalNumInstances*sizeof(float)*4);
int COLOR_BUFFER_SIZE = (totalNumInstances*sizeof(float)*4);
// int SCALE_BUFFER_SIZE = (totalNumInstances*sizeof(float)*3);
glBindVertexArray(gfxObj->m_cube_vao);
for (int i=0;i<transparentInstances.size();i++) int vertexStride = 9*sizeof(float);
PointerCaster vertex;
vertex.m_baseIndex = gfxObj->m_vertexArrayOffset*vertexStride;
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 9*sizeof(float), vertex.m_pointer);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(transparentInstances[i].m_instanceId*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes));
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(transparentInstances[i].m_instanceId*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE));
PointerCaster uv;
uv.m_baseIndex = 7*sizeof(float)+vertex.m_baseIndex;
PointerCaster normal;
normal.m_baseIndex = 4*sizeof(float)+vertex.m_baseIndex;
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 9*sizeof(float), uv.m_pointer);
glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, 9*sizeof(float), normal.m_pointer);
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(transparentInstances[i].m_instanceId*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE));
glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)(transparentInstances[i].m_instanceId*3*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE+COLOR_BUFFER_SIZE));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
glEnableVertexAttribArray(5);
glEnableVertexAttribArray(6);
glVertexAttribDivisor(0, 0);
glVertexAttribDivisor(1, 1);
glVertexAttribDivisor(2, 1);
glVertexAttribDivisor(3, 0);
glVertexAttribDivisor(4, 0);
glVertexAttribDivisor(5, 1);
glVertexAttribDivisor(6, 1);
int indexCount = gfxObj->m_numIndices;
GLvoid* indexOffset = 0;
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gfxObj->m_index_vbo);
{
B3_PROFILE("glDrawElementsInstanced");
if (gfxObj->m_primitiveType==B3_GL_POINTS)
{
glUseProgram(instancingShaderPointSprite);
glUniformMatrix4fv(ProjectionMatrixPointSprite, 1, false, &m_data->m_projectionMatrix[0]);
glUniformMatrix4fv(ModelViewMatrixPointSprite, 1, false, &m_data->m_viewMatrix[0]);
glUniform1f(screenWidthPointSprite,float(m_screenWidth));
//glUniform1i(uniform_texture_diffusePointSprite, 0);
b3Assert(glGetError() ==GL_NO_ERROR);
glPointSize(20);
#ifndef __APPLE__
glEnable(GL_POINT_SPRITE_ARB);
// glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE);
#endif
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
glDrawElementsInstanced(GL_POINTS, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances);
} else
{
switch (renderMode)
{
case B3_DEFAULT_RENDERMODE:
{
if ( gfxObj->m_flags&eGfxTransparency)
{ {
glDepthMask(false);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glUseProgram(instancingShader);
glUniformMatrix4fv(ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]);
glUniformMatrix4fv(ModelViewMatrix, 1, false, &m_data->m_viewMatrix[0]);
b3Vector3 gLightDir = m_data->m_lightPos;
gLightDir.normalize();
glUniform3f(regularLightDirIn,gLightDir[0],gLightDir[1],gLightDir[2]);
glUniform1i(uniform_texture_diffuse, 0);
if ( gfxObj->m_flags&eGfxTransparency)
{
int instanceId = transparentInstances[i].m_instanceId; int instanceId = transparentInstances[i].m_instanceId;
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes)); glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes));
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE)); glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE));
@ -2316,36 +2303,104 @@ b3Assert(glGetError() ==GL_NO_ERROR);
glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*3*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE+COLOR_BUFFER_SIZE)); glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*3*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE+COLOR_BUFFER_SIZE));
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
} else
{
glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances);
} }
} else
{
glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances);
}
if ( gfxObj->m_flags&eGfxTransparency) if ( gfxObj->m_flags&eGfxTransparency)
{ {
glDisable (GL_BLEND); glDisable (GL_BLEND);
glDepthMask(true); glDepthMask(true);
} }
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,0); break;
}
case B3_CREATE_SHADOWMAP_RENDERMODE:
{
glUseProgram(createShadowMapInstancingShader);
glUniformMatrix4fv(createShadow_depthMVP, 1, false, &depthMVP[0][0]);
glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances);
break;
}
glActiveTexture(GL_TEXTURE0); case B3_USE_SHADOWMAP_RENDERMODE:
glBindTexture(GL_TEXTURE_2D,0); {
break; if ( gfxObj->m_flags&eGfxTransparency)
} {
default: glDepthMask(false);
{ glEnable (GL_BLEND);
// b3Assert(0); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
} }
};
glUseProgram(useShadowMapInstancingShader);
glUniformMatrix4fv(useShadow_ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]);
glUniformMatrix4fv(useShadow_ModelViewMatrix, 1, false, &m_data->m_viewMatrix[0]);
glUniformMatrix4fv(useShadow_ViewMatrixInverse, 1, false, &m_data->m_viewMatrixInverse[0]);
glUniformMatrix4fv(useShadow_ModelViewMatrix, 1, false, &m_data->m_viewMatrix[0]);
glUniform3f(useShadow_lightSpecularIntensity, m_data->m_lightSpecularIntensity[0],m_data->m_lightSpecularIntensity[1],m_data->m_lightSpecularIntensity[2]);
glUniform3f(useShadow_materialSpecularColor, gfxObj->m_materialSpecularColor[0],gfxObj->m_materialSpecularColor[1],gfxObj->m_materialSpecularColor[2]);
float MVP[16];
b3Matrix4x4Mul16(m_data->m_projectionMatrix,m_data->m_viewMatrix,MVP);
glUniformMatrix4fv(useShadow_MVP, 1, false, &MVP[0]);
//gLightDir.normalize();
glUniform3f(useShadow_lightPosIn,m_data->m_lightPos[0],m_data->m_lightPos[1],m_data->m_lightPos[2]);
float camPos[3];
m_data->m_activeCamera->getCameraPosition(camPos);
glUniform3f(useShadow_cameraPositionIn,camPos[0],camPos[1],camPos[2]);
glUniform1f(useShadow_materialShininessIn,gfxObj->m_materialShinyNess);
glUniformMatrix4fv(useShadow_DepthBiasModelViewMatrix, 1, false, &depthBiasMVP[0][0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_data->m_shadowTexture);
glUniform1i(useShadow_shadowMap,1);
//sort transparent objects
//gfxObj->m_instanceOffset
if ( gfxObj->m_flags&eGfxTransparency)
{
int instanceId = transparentInstances[i].m_instanceId;
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes));
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE));
glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*4*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE));
glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid *)((instanceId)*3*sizeof(float)+m_data->m_maxShapeCapacityInBytes+POSITION_BUFFER_SIZE+ORIENTATION_BUFFER_SIZE+COLOR_BUFFER_SIZE));
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
} else
{
glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, indexOffset, gfxObj->m_numGraphicsInstances);
}
if ( gfxObj->m_flags&eGfxTransparency)
{
glDisable (GL_BLEND);
glDepthMask(true);
}
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,0);
break;
}
default:
{
// b3Assert(0);
}
};
}
//glDrawElementsInstanced(GL_LINE_LOOP, indexCount, GL_UNSIGNED_INT, (void*)indexOffset, gfxObj->m_numGraphicsInstances);
} }
//glDrawElementsInstanced(GL_LINE_LOOP, indexCount, GL_UNSIGNED_INT, (void*)indexOffset, gfxObj->m_numGraphicsInstances);
} }
} }
curOffset+= gfxObj->m_numGraphicsInstances;
} }
{ {

View File

@ -425,21 +425,23 @@ public:
} }
// int boxId = m_app->registerCubeShape(1,1,1,textureIndex); // int boxId = m_app->registerCubeShape(1,1,1,textureIndex);
int boxId = m_app->registerGraphicsUnitSphereShape(SPHERE_LOD_HIGH, textureIndex); int sphereTransparent = m_app->registerGraphicsUnitSphereShape(SPHERE_LOD_HIGH, textureIndex);
int sphereOpaque= m_app->registerGraphicsUnitSphereShape(SPHERE_LOD_HIGH, textureIndex);
b3Vector3 scaling = b3MakeVector3(SPHERE_RADIUS,SPHERE_RADIUS,SPHERE_RADIUS); b3Vector3 scaling = b3MakeVector3(SPHERE_RADIUS,SPHERE_RADIUS,SPHERE_RADIUS);
for (int i=0;i<m_bodies.size();i++) for (int i=0;i<m_bodies.size();i++)
{ {
b3Vector4 color = b3MakeVector4(1,1,1,0.6); int gfxShape = sphereOpaque;
b3Vector4 color = b3MakeVector4(.1,.1,1,1);
if (i%2) if (i%2)
{ {
color.setValue(1,.1,.1,0.8); color.setValue(1,.1,.1,0.1);
gfxShape = sphereTransparent;
} }
m_bodies[i]->m_collisionShape.m_sphere.m_radius = SPHERE_RADIUS; m_bodies[i]->m_collisionShape.m_sphere.m_radius = SPHERE_RADIUS;
m_bodies[i]->m_collisionShape.m_type = LW_SPHERE_TYPE; m_bodies[i]->m_collisionShape.m_type = LW_SPHERE_TYPE;
m_bodies[i]->m_graphicsIndex = m_app->m_renderer->registerGraphicsInstance(boxId,m_bodies[i]->m_worldPose.m_position, m_bodies[i]->m_worldPose.m_orientation,color,scaling); m_bodies[i]->m_graphicsIndex = m_app->m_renderer->registerGraphicsInstance(gfxShape,m_bodies[i]->m_worldPose.m_position, m_bodies[i]->m_worldPose.m_orientation,color,scaling);
m_app->m_renderer->writeSingleInstanceTransformToCPU(m_bodies[i]->m_worldPose.m_position, m_bodies[i]->m_worldPose.m_orientation, m_bodies[i]->m_graphicsIndex); m_app->m_renderer->writeSingleInstanceTransformToCPU(m_bodies[i]->m_worldPose.m_position, m_bodies[i]->m_worldPose.m_orientation, m_bodies[i]->m_graphicsIndex);
} }
} }

View File

@ -0,0 +1,18 @@
import pybullet as p
import time
p.connect(p.GUI)
p.loadURDF("plane.urdf")
sphereUid = p.loadURDF("sphere_transparent.urdf",[0,0,2])
redSlider = p.addUserDebugParameter("red",0,1,1)
greenSlider = p.addUserDebugParameter("green",0,1,0)
blueSlider = p.addUserDebugParameter("blue",0,1,0)
alphaSlider = p.addUserDebugParameter("alpha",0,1,0.5)
while (1):
red = p.readUserDebugParameter(redSlider)
green = p.readUserDebugParameter(greenSlider)
blue = p.readUserDebugParameter(blueSlider)
alpha = p.readUserDebugParameter(alphaSlider)
p.changeVisualShape(sphereUid,-1,rgbaColor=[red,green,blue,alpha])
time.sleep(0.01)