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:
@@ -26,7 +26,7 @@ struct SliderParams
|
||||
m_callback(0),
|
||||
m_paramValuePointer(targetValuePointer),
|
||||
m_userPointer(0),
|
||||
m_clampToNotches(true),
|
||||
m_clampToNotches(false),
|
||||
m_clampToIntegers(false),
|
||||
m_showValues(true)
|
||||
{
|
||||
|
||||
@@ -28,6 +28,8 @@ struct CommonRenderInterface
|
||||
virtual CommonCameraInterface* getActiveCamera()=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 renderSceneInternal(int renderMode=B3_DEFAULT_RENDERMODE){};
|
||||
|
||||
@@ -79,7 +79,6 @@ float shadowMapWorldSize=10;
|
||||
static InternalDataRenderer* sData2;
|
||||
|
||||
GLint lineWidthRange[2]={1,1};
|
||||
static b3Vector3 gLightPos=b3MakeVector3(-5,12,-4);
|
||||
|
||||
struct b3GraphicsInstance
|
||||
{
|
||||
@@ -156,6 +155,7 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData
|
||||
GLfloat m_projectionMatrix[16];
|
||||
GLfloat m_viewMatrix[16];
|
||||
|
||||
b3Vector3 m_lightPos;
|
||||
|
||||
GLuint m_defaultTexturehandle;
|
||||
b3AlignedObjectArray<InternalTextureHandle> m_textureHandles;
|
||||
@@ -172,6 +172,8 @@ struct InternalDataRenderer : public GLInstanceRendererInternalData
|
||||
m_shadowTexture(0),
|
||||
m_renderFrameBuffer(0)
|
||||
{
|
||||
m_lightPos=b3MakeVector3(-5,50,50);
|
||||
|
||||
//clear to zero to make it obvious if the matrix is used uninitialized
|
||||
for (int i=0;i<16;i++)
|
||||
{
|
||||
@@ -996,23 +998,26 @@ void GLInstancingRenderer::setActiveCamera(CommonCameraInterface* 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)
|
||||
{
|
||||
|
||||
b3Assert(glGetError() ==GL_NO_ERROR);
|
||||
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->setAspectRatio((float)m_screenWidth/(float)m_screenHeight);
|
||||
@@ -1554,9 +1559,10 @@ void GLInstancingRenderer::renderSceneInternal(int renderMode)
|
||||
//float upf[3];
|
||||
//m_data->m_activeCamera->getCameraUpVector(upf);
|
||||
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]);
|
||||
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);
|
||||
|
||||
GLfloat depthModelMatrix[4][4];
|
||||
@@ -1726,7 +1732,7 @@ b3Assert(glGetError() ==GL_NO_ERROR);
|
||||
glUniformMatrix4fv(ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]);
|
||||
glUniformMatrix4fv(ModelViewMatrix, 1, false, &m_data->m_viewMatrix[0]);
|
||||
|
||||
b3Vector3 gLightDir = gLightPos;
|
||||
b3Vector3 gLightDir = m_data->m_lightPos;
|
||||
gLightDir.normalize();
|
||||
glUniform3f(regularLightDirIn,gLightDir[0],gLightDir[1],gLightDir[2]);
|
||||
|
||||
@@ -1763,7 +1769,7 @@ b3Assert(glGetError() ==GL_NO_ERROR);
|
||||
float MVP[16];
|
||||
b3Matrix4x4Mul16(m_data->m_projectionMatrix,m_data->m_viewMatrix,MVP);
|
||||
glUniformMatrix4fv(useShadow_MVP, 1, false, &MVP[0]);
|
||||
b3Vector3 gLightDir = gLightPos;
|
||||
b3Vector3 gLightDir = m_data->m_lightPos;
|
||||
gLightDir.normalize();
|
||||
glUniform3f(useShadow_lightDirIn,gLightDir[0],gLightDir[1],gLightDir[2]);
|
||||
glUniformMatrix4fv(useShadow_DepthBiasModelViewMatrix, 1, false, &depthBiasMVP[0][0]);
|
||||
|
||||
@@ -117,7 +117,9 @@ public:
|
||||
virtual CommonCameraInterface* getActiveCamera();
|
||||
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 int getScreenWidth()
|
||||
{
|
||||
|
||||
@@ -39,7 +39,9 @@ void main(void)
|
||||
|
||||
|
||||
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;
|
||||
|
||||
cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient;
|
||||
|
||||
@@ -32,7 +32,8 @@ static const char* useShadowMapInstancingFragmentShader= \
|
||||
" \n"
|
||||
" \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"
|
||||
" \n"
|
||||
" cf = intensity*(vec3(1.0,1.0,1.0)-ambient)+ambient;\n"
|
||||
|
||||
@@ -78,6 +78,14 @@ void SimpleOpenGL2Renderer::setActiveCamera(CommonCameraInterface* cam)
|
||||
b3Assert(0);//not supported yet
|
||||
}
|
||||
|
||||
void SimpleOpenGL2Renderer::setLightPosition(const float lightPos[3])
|
||||
{
|
||||
}
|
||||
void SimpleOpenGL2Renderer::setLightPosition(const double lightPos[3])
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void SimpleOpenGL2Renderer::updateCamera(int upAxis)
|
||||
{
|
||||
float projection[16];
|
||||
|
||||
@@ -25,6 +25,10 @@ public:
|
||||
virtual CommonCameraInterface* getActiveCamera();
|
||||
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 removeAllInstances();
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#include "../CommonInterfaces/CommonParameterInterface.h"
|
||||
|
||||
|
||||
|
||||
|
||||
struct TinyRendererSetupInternalData
|
||||
{
|
||||
|
||||
@@ -42,6 +44,8 @@ struct TinyRendererSetupInternalData
|
||||
int m_textureHandle;
|
||||
int m_animateRenderer;
|
||||
|
||||
double m_lightPos[3];
|
||||
|
||||
TinyRendererSetupInternalData(int width, int height)
|
||||
:
|
||||
m_rgbColorBuffer(width,height,TGAImage::RGB),
|
||||
@@ -53,6 +57,10 @@ struct TinyRendererSetupInternalData
|
||||
m_textureHandle(0),
|
||||
m_animateRenderer(0)
|
||||
{
|
||||
m_lightPos[0] = -3;
|
||||
m_lightPos[1] = 3;
|
||||
m_lightPos[2] = 3;
|
||||
|
||||
m_depthBuffer.resize(m_width*m_height);
|
||||
m_shadowBuffer.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 void renderScene()
|
||||
{
|
||||
}
|
||||
virtual void renderScene();
|
||||
|
||||
void animateRenderer(int animateRendererIndex)
|
||||
{
|
||||
@@ -295,6 +301,28 @@ void TinyRendererSetup::initPhysics()
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
||||
|
||||
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++)
|
||||
{
|
||||
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_projectionMatrix[i][j] = projMat[i+4*j];
|
||||
|
||||
btVector3 lightDirWorld;
|
||||
switch (m_app->getUpAxis())
|
||||
{
|
||||
case 1:
|
||||
lightDirWorld = btVector3(-50.f,100,30);
|
||||
break;
|
||||
case 2:
|
||||
lightDirWorld = btVector3(-50.f,30,100);
|
||||
break;
|
||||
default:{}
|
||||
};
|
||||
btVector3 lightDirWorld = btVector3(m_internalData->m_lightPos[0],m_internalData->m_lightPos[1],m_internalData->m_lightPos[2]);
|
||||
|
||||
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_projectionMatrix[i][j] = projMat[i+4*j];
|
||||
|
||||
btVector3 lightDirWorld;
|
||||
switch (m_app->getUpAxis())
|
||||
{
|
||||
case 1:
|
||||
lightDirWorld = btVector3(-50.f,100,30);
|
||||
break;
|
||||
case 2:
|
||||
lightDirWorld = btVector3(-50.f,30,100);
|
||||
break;
|
||||
default:{}
|
||||
};
|
||||
btVector3 lightDirWorld = btVector3(m_internalData->m_lightPos[0],m_internalData->m_lightPos[1],m_internalData->m_lightPos[2]);
|
||||
|
||||
m_internalData->m_renderObjects[o]->m_lightDirWorld = lightDirWorld.normalized();
|
||||
|
||||
|
||||
@@ -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
|
||||
if (triangleIn[3][0]<0 && triangleIn[3][1] <0 && triangleIn[3][2] <0)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user