tinyrenderer: disable triangle backface culling (doesn't work well, cull triangles that should be visible)

GLInstancingRenderer: allow to set the light position
This commit is contained in:
Erwin Coumans
2017-04-23 07:35:13 -07:00
parent e8c1602232
commit 4759e5a590
10 changed files with 99 additions and 55 deletions

View File

@@ -26,7 +26,7 @@ struct SliderParams
m_callback(0), m_callback(0),
m_paramValuePointer(targetValuePointer), m_paramValuePointer(targetValuePointer),
m_userPointer(0), m_userPointer(0),
m_clampToNotches(true), m_clampToNotches(false),
m_clampToIntegers(false), m_clampToIntegers(false),
m_showValues(true) m_showValues(true)
{ {

View File

@@ -28,6 +28,8 @@ struct CommonRenderInterface
virtual CommonCameraInterface* getActiveCamera()=0; virtual CommonCameraInterface* getActiveCamera()=0;
virtual void setActiveCamera(CommonCameraInterface* cam)=0; virtual void setActiveCamera(CommonCameraInterface* cam)=0;
virtual void setLightPosition(const float lightPos[3]) = 0;
virtual void setLightPosition(const double lightPos[3]) = 0;
virtual void renderScene()=0; virtual void renderScene()=0;
virtual void renderSceneInternal(int renderMode=B3_DEFAULT_RENDERMODE){}; virtual void renderSceneInternal(int renderMode=B3_DEFAULT_RENDERMODE){};

View File

@@ -79,7 +79,6 @@ float shadowMapWorldSize=10;
static InternalDataRenderer* sData2; static InternalDataRenderer* sData2;
GLint lineWidthRange[2]={1,1}; GLint lineWidthRange[2]={1,1};
static b3Vector3 gLightPos=b3MakeVector3(-5,12,-4);
struct b3GraphicsInstance struct b3GraphicsInstance
{ {
@@ -156,6 +155,7 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData
GLfloat m_projectionMatrix[16]; GLfloat m_projectionMatrix[16];
GLfloat m_viewMatrix[16]; GLfloat m_viewMatrix[16];
b3Vector3 m_lightPos;
GLuint m_defaultTexturehandle; GLuint m_defaultTexturehandle;
b3AlignedObjectArray<InternalTextureHandle> m_textureHandles; b3AlignedObjectArray<InternalTextureHandle> m_textureHandles;
@@ -172,6 +172,8 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData
m_shadowTexture(0), m_shadowTexture(0),
m_renderFrameBuffer(0) m_renderFrameBuffer(0)
{ {
m_lightPos=b3MakeVector3(-5,50,50);
//clear to zero to make it obvious if the matrix is used uninitialized //clear to zero to make it obvious if the matrix is used uninitialized
for (int i=0;i<16;i++) for (int i=0;i<16;i++)
{ {
@@ -996,23 +998,26 @@ void GLInstancingRenderer::setActiveCamera(CommonCameraInterface* cam)
m_data->m_activeCamera = cam; m_data->m_activeCamera = cam;
} }
void GLInstancingRenderer::setLightPosition(const float lightPos[3])
{
m_data->m_lightPos[0] = lightPos[0];
m_data->m_lightPos[1] = lightPos[1];
m_data->m_lightPos[2] = lightPos[2];
}
void GLInstancingRenderer::setLightPosition(const double lightPos[3])
{
m_data->m_lightPos[0] = lightPos[0];
m_data->m_lightPos[1] = lightPos[1];
m_data->m_lightPos[2] = lightPos[2];
}
void GLInstancingRenderer::updateCamera(int upAxis) void GLInstancingRenderer::updateCamera(int upAxis)
{ {
b3Assert(glGetError() ==GL_NO_ERROR); b3Assert(glGetError() ==GL_NO_ERROR);
m_upAxis = upAxis; m_upAxis = upAxis;
switch (upAxis)
{
case 1:
gLightPos = b3MakeVector3(-50.f,100,30);
break;
case 2:
gLightPos = b3MakeVector3(-50.f,30,100);
break;
default:
b3Assert(0);
};
m_data->m_activeCamera->setCameraUpAxis(upAxis); m_data->m_activeCamera->setCameraUpAxis(upAxis);
m_data->m_activeCamera->setAspectRatio((float)m_screenWidth/(float)m_screenHeight); m_data->m_activeCamera->setAspectRatio((float)m_screenWidth/(float)m_screenHeight);
@@ -1554,9 +1559,10 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode)
//float upf[3]; //float upf[3];
//m_data->m_activeCamera->getCameraUpVector(upf); //m_data->m_activeCamera->getCameraUpVector(upf);
b3Vector3 up, fwd; b3Vector3 up, fwd;
b3PlaneSpace1(gLightPos,up,fwd); b3Vector3 lightDir = m_data->m_lightPos.normalized();
b3PlaneSpace1(lightDir,up,fwd);
// b3Vector3 up = b3MakeVector3(upf[0],upf[1],upf[2]); // b3Vector3 up = b3MakeVector3(upf[0],upf[1],upf[2]);
b3CreateLookAt(gLightPos,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);
GLfloat depthModelMatrix[4][4]; GLfloat depthModelMatrix[4][4];
@@ -1726,7 +1732,7 @@ b3Assert(glGetError() ==GL_NO_ERROR);
glUniformMatrix4fv(ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]); glUniformMatrix4fv(ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]);
glUniformMatrix4fv(ModelViewMatrix, 1, false, &m_data->m_viewMatrix[0]); glUniformMatrix4fv(ModelViewMatrix, 1, false, &m_data->m_viewMatrix[0]);
b3Vector3 gLightDir = gLightPos; b3Vector3 gLightDir = m_data->m_lightPos;
gLightDir.normalize(); gLightDir.normalize();
glUniform3f(regularLightDirIn,gLightDir[0],gLightDir[1],gLightDir[2]); glUniform3f(regularLightDirIn,gLightDir[0],gLightDir[1],gLightDir[2]);
@@ -1763,7 +1769,7 @@ b3Assert(glGetError() ==GL_NO_ERROR);
float MVP[16]; float MVP[16];
b3Matrix4x4Mul16(m_data->m_projectionMatrix,m_data->m_viewMatrix,MVP); b3Matrix4x4Mul16(m_data->m_projectionMatrix,m_data->m_viewMatrix,MVP);
glUniformMatrix4fv(useShadow_MVP, 1, false, &MVP[0]); glUniformMatrix4fv(useShadow_MVP, 1, false, &MVP[0]);
b3Vector3 gLightDir = gLightPos; b3Vector3 gLightDir = m_data->m_lightPos;
gLightDir.normalize(); gLightDir.normalize();
glUniform3f(useShadow_lightDirIn,gLightDir[0],gLightDir[1],gLightDir[2]); glUniform3f(useShadow_lightDirIn,gLightDir[0],gLightDir[1],gLightDir[2]);
glUniformMatrix4fv(useShadow_DepthBiasModelViewMatrix, 1, false, &depthBiasMVP[0][0]); glUniformMatrix4fv(useShadow_DepthBiasModelViewMatrix, 1, false, &depthBiasMVP[0][0]);

View File

@@ -117,6 +117,8 @@ public:
virtual CommonCameraInterface* getActiveCamera(); virtual CommonCameraInterface* getActiveCamera();
virtual void setActiveCamera(CommonCameraInterface* cam); virtual void setActiveCamera(CommonCameraInterface* cam);
virtual void setLightPosition(const float lightPos[3]);
virtual void setLightPosition(const double lightPos[3]);
virtual void resize(int width, int height); virtual void resize(int width, int height);
virtual int getScreenWidth() virtual int getScreenWidth()

View File

@@ -39,6 +39,8 @@ void main(void)
float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z)/ShadowCoord.w)); float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z)/ShadowCoord.w));
if (intensity<0.5)
visibility = 0;
intensity = 0.7*intensity + 0.3*intensity*visibility; intensity = 0.7*intensity + 0.3*intensity*visibility;

View File

@@ -32,7 +32,8 @@ static const char* useShadowMapInstancingFragmentShader= \
" \n" " \n"
" \n" " \n"
" float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z)/ShadowCoord.w));\n" " float visibility = texture(shadowMap, vec3(ShadowCoord.xy,(ShadowCoord.z)/ShadowCoord.w));\n"
" \n" " if (intensity<0.5)\n"
" visibility = 0;\n"
" intensity = 0.7*intensity + 0.3*intensity*visibility;\n" " intensity = 0.7*intensity + 0.3*intensity*visibility;\n"
" \n" " \n"
" cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient;\n" " cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient;\n"

View File

@@ -78,6 +78,14 @@ void SimpleOpenGL2Renderer::setActiveCamera(CommonCameraInterface* cam)
b3Assert(0);//not supported yet b3Assert(0);//not supported yet
} }
void SimpleOpenGL2Renderer::setLightPosition(const float lightPos[3])
{
}
void SimpleOpenGL2Renderer::setLightPosition(const double lightPos[3])
{
}
void SimpleOpenGL2Renderer::updateCamera(int upAxis) void SimpleOpenGL2Renderer::updateCamera(int upAxis)
{ {
float projection[16]; float projection[16];

View File

@@ -25,6 +25,10 @@ public:
virtual CommonCameraInterface* getActiveCamera(); virtual CommonCameraInterface* getActiveCamera();
virtual void setActiveCamera(CommonCameraInterface* cam); virtual void setActiveCamera(CommonCameraInterface* cam);
virtual void setLightPosition(const float lightPos[3]);
virtual void setLightPosition(const double lightPos[3]);
virtual void resize(int width, int height); virtual void resize(int width, int height);
virtual void removeAllInstances(); virtual void removeAllInstances();

View File

@@ -18,6 +18,8 @@
#include "../CommonInterfaces/CommonParameterInterface.h" #include "../CommonInterfaces/CommonParameterInterface.h"
struct TinyRendererSetupInternalData struct TinyRendererSetupInternalData
{ {
@@ -42,6 +44,8 @@ struct TinyRendererSetupInternalData
int m_textureHandle; int m_textureHandle;
int m_animateRenderer; int m_animateRenderer;
double m_lightPos[3];
TinyRendererSetupInternalData(int width, int height) TinyRendererSetupInternalData(int width, int height)
: :
m_rgbColorBuffer(width,height,TGAImage::RGB), m_rgbColorBuffer(width,height,TGAImage::RGB),
@@ -53,6 +57,10 @@ struct TinyRendererSetupInternalData
m_textureHandle(0), m_textureHandle(0),
m_animateRenderer(0) m_animateRenderer(0)
{ {
m_lightPos[0] = -3;
m_lightPos[1] = 3;
m_lightPos[2] = 3;
m_depthBuffer.resize(m_width*m_height); m_depthBuffer.resize(m_width*m_height);
m_shadowBuffer.resize(m_width*m_height); m_shadowBuffer.resize(m_width*m_height);
// m_segmentationMaskBuffer.resize(m_width*m_height); // m_segmentationMaskBuffer.resize(m_width*m_height);
@@ -115,9 +123,7 @@ struct TinyRendererSetup : public CommonExampleInterface
virtual bool keyboardCallback(int key, int state); virtual bool keyboardCallback(int key, int state);
virtual void renderScene() virtual void renderScene();
{
}
void animateRenderer(int animateRendererIndex) void animateRenderer(int animateRendererIndex)
{ {
@@ -295,6 +301,28 @@ void TinyRendererSetup::initPhysics()
m_guiHelper->getParameterInterface()->registerComboBox( comboParams); m_guiHelper->getParameterInterface()->registerComboBox( comboParams);
} }
{
SliderParams slider("LightPosX",&m_internalData->m_lightPos[0]);
slider.m_minVal=-10;
slider.m_maxVal=10;
if (m_guiHelper->getParameterInterface())
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
{
SliderParams slider("LightPosY",&m_internalData->m_lightPos[1]);
slider.m_minVal=-10;
slider.m_maxVal=10;
if (m_guiHelper->getParameterInterface())
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
{
SliderParams slider("LightPosZ",&m_internalData->m_lightPos[2]);
slider.m_minVal=-10;
slider.m_maxVal=10;
if (m_guiHelper->getParameterInterface())
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
} }
@@ -303,14 +331,33 @@ void TinyRendererSetup::exitPhysics()
} }
void TinyRendererSetup::stepSimulation(float deltaTime) void TinyRendererSetup::stepSimulation(float deltaTime)
{ {
m_internalData->updateTransforms(); m_internalData->updateTransforms();
}
void TinyRendererSetup::renderScene()
{
m_internalData->updateTransforms();
btVector4 from(m_internalData->m_lightPos[0],m_internalData->m_lightPos[1],m_internalData->m_lightPos[2],1);
btVector4 toX(m_internalData->m_lightPos[0]+0.1,m_internalData->m_lightPos[1],m_internalData->m_lightPos[2],1);
btVector4 toY(m_internalData->m_lightPos[0],m_internalData->m_lightPos[1]+0.1,m_internalData->m_lightPos[2],1);
btVector4 toZ(m_internalData->m_lightPos[0],m_internalData->m_lightPos[1],m_internalData->m_lightPos[2]+0.1,1);
btVector4 colorX(1,0,0,1);
btVector4 colorY(0,1,0,1);
btVector4 colorZ(0,0,1,1);
int width=2;
m_guiHelper->getRenderInterface()->drawLine( from,toX,colorX,width);
m_guiHelper->getRenderInterface()->drawLine( from,toY,colorY,width);
m_guiHelper->getRenderInterface()->drawLine( from,toZ,colorZ,width);
if (!m_useSoftware) if (!m_useSoftware)
{ {
btVector3 lightPos(m_internalData->m_lightPos[0],m_internalData->m_lightPos[1],m_internalData->m_lightPos[2]);
m_guiHelper->getRenderInterface()->setLightPosition(lightPos);
for (int i=0;i<m_internalData->m_transforms.size();i++) for (int i=0;i<m_internalData->m_transforms.size();i++)
{ {
m_guiHelper->getRenderInterface()->writeSingleInstanceTransformToCPU(m_internalData->m_transforms[i].getOrigin(),m_internalData->m_transforms[i].getRotation(),i); m_guiHelper->getRenderInterface()->writeSingleInstanceTransformToCPU(m_internalData->m_transforms[i].getOrigin(),m_internalData->m_transforms[i].getRotation(),i);
@@ -358,17 +405,7 @@ void TinyRendererSetup::stepSimulation(float deltaTime)
m_internalData->m_renderObjects[o]->m_viewMatrix[i][j] = viewMat[i+4*j]; m_internalData->m_renderObjects[o]->m_viewMatrix[i][j] = viewMat[i+4*j];
m_internalData->m_renderObjects[o]->m_projectionMatrix[i][j] = projMat[i+4*j]; m_internalData->m_renderObjects[o]->m_projectionMatrix[i][j] = projMat[i+4*j];
btVector3 lightDirWorld; btVector3 lightDirWorld = btVector3(m_internalData->m_lightPos[0],m_internalData->m_lightPos[1],m_internalData->m_lightPos[2]);
switch (m_app->getUpAxis())
{
case 1:
lightDirWorld = btVector3(-50.f,100,30);
break;
case 2:
lightDirWorld = btVector3(-50.f,30,100);
break;
default:{}
};
m_internalData->m_renderObjects[o]->m_lightDirWorld = lightDirWorld.normalized(); m_internalData->m_renderObjects[o]->m_lightDirWorld = lightDirWorld.normalized();
@@ -399,17 +436,7 @@ void TinyRendererSetup::stepSimulation(float deltaTime)
m_internalData->m_renderObjects[o]->m_viewMatrix[i][j] = viewMat[i+4*j]; m_internalData->m_renderObjects[o]->m_viewMatrix[i][j] = viewMat[i+4*j];
m_internalData->m_renderObjects[o]->m_projectionMatrix[i][j] = projMat[i+4*j]; m_internalData->m_renderObjects[o]->m_projectionMatrix[i][j] = projMat[i+4*j];
btVector3 lightDirWorld; btVector3 lightDirWorld = btVector3(m_internalData->m_lightPos[0],m_internalData->m_lightPos[1],m_internalData->m_lightPos[2]);
switch (m_app->getUpAxis())
{
case 1:
lightDirWorld = btVector3(-50.f,100,30);
break;
case 2:
lightDirWorld = btVector3(-50.f,30,100);
break;
default:{}
};
m_internalData->m_renderObjects[o]->m_lightDirWorld = lightDirWorld.normalized(); m_internalData->m_renderObjects[o]->m_lightDirWorld = lightDirWorld.normalized();

View File

@@ -426,14 +426,6 @@ static bool clipTriangleAgainstNearplane(const mat<4,3,float>& triangleIn, b3Ali
{ {
float orientation = (triangleIn[0][1] - triangleIn[0][0]) * (triangleIn[1][2] - triangleIn[1][0])
- (triangleIn[1][1] - triangleIn[1][0]) * (triangleIn[0][2] - triangleIn[0][0]);
if (orientation < 0.0)
{
return true;
}
//discard triangle if all vertices are behind near-plane //discard triangle if all vertices are behind near-plane
if (triangleIn[3][0]<0 && triangleIn[3][1] <0 && triangleIn[3][2] <0) if (triangleIn[3][0]<0 && triangleIn[3][1] <0 && triangleIn[3][2] <0)
{ {