Render multiple objects with shadow.

This commit is contained in:
yunfeibai
2016-11-27 16:53:15 -08:00
parent 583dc1cac7
commit 0bc4e0a4c3
12 changed files with 19039 additions and 10 deletions

View File

@@ -242,7 +242,7 @@ void PhysicsClientExample::prepareAndSubmitCommand(int commandId)
{
case CMD_LOAD_URDF:
{
b3SharedMemoryCommandHandle commandHandle = b3LoadUrdfCommandInit(m_physicsClientHandle, "torus/torus.urdf");
b3SharedMemoryCommandHandle commandHandle = b3LoadUrdfCommandInit(m_physicsClientHandle, "torus/torus_with_separate_plane.urdf");
//setting the initial position, orientation and other arguments are optional
double startPosX = 0;
static double startPosY = 0;

View File

@@ -36,6 +36,8 @@ subject to the following restrictions:
#include "../TinyRenderer/model.h"
#include "../ThirdPartyLibs/stb_image/stb_image.h"
extern int count;
enum MyFileType
{
@@ -721,6 +723,8 @@ void TinyRendererVisualShapeConverter::render(const float viewMat[16], const flo
lightColor = m_data->m_lightColor;
}
count = 0;
// printf("num m_swRenderInstances = %d\n", m_data->m_swRenderInstances.size());
for (int i=0;i<m_data->m_swRenderInstances.size();i++)
{
@@ -757,7 +761,48 @@ void TinyRendererVisualShapeConverter::render(const float viewMat[16], const flo
renderObj->m_lightColor = lightColor;
}
}
TinyRenderer::renderObject(*renderObj);
TinyRenderer::renderObjectDepth(*renderObj);
}
}
count = 0;
for (int i=0;i<m_data->m_swRenderInstances.size();i++)
{
TinyRendererObjectArray** visualArrayPtr = m_data->m_swRenderInstances.getAtIndex(i);
if (0==visualArrayPtr)
continue;//can this ever happen?
TinyRendererObjectArray* visualArray = *visualArrayPtr;
btHashPtr colObjHash = m_data->m_swRenderInstances.getKeyAtIndex(i);
const btCollisionObject* colObj = (btCollisionObject*) colObjHash.getPointer();
for (int v=0;v<visualArray->m_renderObjects.size();v++)
{
TinyRenderObjectData* renderObj = visualArray->m_renderObjects[v];
//sync the object transform
const btTransform& tr = colObj->getWorldTransform();
tr.getOpenGLMatrix(modelMat);
for (int i=0;i<4;i++)
{
for (int j=0;j<4;j++)
{
renderObj->m_projectionMatrix[i][j] = projMat[i+4*j];
renderObj->m_modelMatrix[i][j] = modelMat[i+4*j];
renderObj->m_viewMatrix[i][j] = viewMat[i+4*j];
renderObj->m_localScaling = colObj->getCollisionShape()->getLocalScaling();
renderObj->m_lightDirWorld = lightDirWorld;
renderObj->m_lightColor = lightColor;
}
}
TinyRenderer::renderObjectShadow(*renderObj);
}
}
//printf("write tga \n");

View File

@@ -455,14 +455,13 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData)
count++;
}
for (int k = 0; k < 76800; ++k)
{
if (shadowbuffer[k] > -1e30f+0.00001)
{
printf("[%d]: %f\n", k, shadowbuffer[k]);
}
}
for (int k = 0; k < 76800; ++k)
{
if (shadowbuffer[k] > -1e30f+0.00001)
{
//printf("[%d]: %f\n", k, shadowbuffer[k]);
}
}
ShadowShader shadowShader(model, light_dir_local, light_color, modelViewMatrix, lightModelViewMatrix, renderData.m_projectionMatrix,renderData.m_modelMatrix, localScaling, model->getColorRGBA(), width, height, shadowbuffer);
@@ -496,3 +495,102 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData)
}
}
void TinyRenderer::renderObjectDepth(TinyRenderObjectData& renderData)
{
int width = renderData.m_rgbColorBuffer.get_width();
int height = renderData.m_rgbColorBuffer.get_height();
Vec3f light_dir_local = Vec3f(renderData.m_lightDirWorld[0],renderData.m_lightDirWorld[1],renderData.m_lightDirWorld[2]);
Vec3f light_color = Vec3f(renderData.m_lightColor[0],renderData.m_lightColor[1],renderData.m_lightColor[2]);
Model* model = renderData.m_model;
if (0==model)
return;
renderData.m_viewportMatrix = viewport(0,0,width, height);
b3AlignedObjectArray<float>& zbuffer = renderData.m_depthBuffer;
b3AlignedObjectArray<float>& shadowbuffer = renderData.m_shadowBuffer;
int* segmentationMaskBufferPtr = (renderData.m_segmentationMaskBufferPtr && renderData.m_segmentationMaskBufferPtr->size())?&renderData.m_segmentationMaskBufferPtr->at(0):0;
TGAImage tempFrame(width, height, TGAImage::RGB);
TGAImage& frame = renderData.m_rgbColorBuffer;
{
Matrix lightViewMatrix = lookat(light_dir_local*depth, Vec3f(0.0,0.0,0.0), Vec3f(0.0,0.0,1.0));
Matrix lightModelViewMatrix = lightViewMatrix*renderData.m_modelMatrix;
Matrix lightViewProjectionMatrix = renderData.m_projectionMatrix;
Matrix modelViewMatrix = renderData.m_viewMatrix*renderData.m_modelMatrix;
viewportmat = renderData.m_viewportMatrix;
Vec3f localScaling(renderData.m_localScaling[0],renderData.m_localScaling[1],renderData.m_localScaling[2]);
DepthShader shader(model, lightModelViewMatrix, lightViewProjectionMatrix,renderData.m_modelMatrix, localScaling);
setindex = true;
for (int i=0; i<model->nfaces(); i++)
{
for (int j=0; j<3; j++) {
shader.vertex(i, j);
}
triangle(shader.varying_tri, shader, tempFrame, &shadowbuffer[0], segmentationMaskBufferPtr, renderData.m_viewportMatrix, renderData.m_objectIndex);
count++;
}
for (int k = 0; k < 76800; ++k)
{
if (shadowbuffer[k] > -1e30f+0.00001)
{
//printf("[%d]: %f\n", k, shadowbuffer[k]);
}
}
}
}
void TinyRenderer::renderObjectShadow(TinyRenderObjectData& renderData)
{
int width = renderData.m_rgbColorBuffer.get_width();
int height = renderData.m_rgbColorBuffer.get_height();
Vec3f light_dir_local = Vec3f(renderData.m_lightDirWorld[0],renderData.m_lightDirWorld[1],renderData.m_lightDirWorld[2]);
Vec3f light_color = Vec3f(renderData.m_lightColor[0],renderData.m_lightColor[1],renderData.m_lightColor[2]);
Model* model = renderData.m_model;
if (0==model)
return;
renderData.m_viewportMatrix = viewport(0,0,width, height);
b3AlignedObjectArray<float>& zbuffer = renderData.m_depthBuffer;
b3AlignedObjectArray<float>& shadowbuffer = renderData.m_shadowBuffer;
int* segmentationMaskBufferPtr = (renderData.m_segmentationMaskBufferPtr && renderData.m_segmentationMaskBufferPtr->size())?&renderData.m_segmentationMaskBufferPtr->at(0):0;
TGAImage tempFrame(width, height, TGAImage::RGB);
TGAImage& frame = renderData.m_rgbColorBuffer;
{
Matrix lightViewMatrix = lookat(light_dir_local*depth, Vec3f(0.0,0.0,0.0), Vec3f(0.0,0.0,1.0));
Matrix lightModelViewMatrix = lightViewMatrix*renderData.m_modelMatrix;
Matrix lightViewProjectionMatrix = renderData.m_projectionMatrix;
Matrix modelViewMatrix = renderData.m_viewMatrix*renderData.m_modelMatrix;
viewportmat = renderData.m_viewportMatrix;
Vec3f localScaling(renderData.m_localScaling[0],renderData.m_localScaling[1],renderData.m_localScaling[2]);
ShadowShader shadowShader(model, light_dir_local, light_color, modelViewMatrix, lightModelViewMatrix, renderData.m_projectionMatrix,renderData.m_modelMatrix, localScaling, model->getColorRGBA(), width, height, shadowbuffer);
setindex = false;
for (int i=0; i<model->nfaces(); i++)
{
for (int j=0; j<3; j++) {
shadowShader.vertex(i, j);
}
triangle(shadowShader.varying_tri, shadowShader, frame, &zbuffer[0], segmentationMaskBufferPtr, renderData.m_viewportMatrix, renderData.m_objectIndex);
printf("count: %d\n", count);
count++;
}
}
}

View File

@@ -53,6 +53,8 @@ class TinyRenderer
{
public:
static void renderObject(TinyRenderObjectData& renderData);
static void renderObjectDepth(TinyRenderObjectData& renderData);
static void renderObjectShadow(TinyRenderObjectData& renderData);
};
#endif // TINY_RENDERER_Hbla