diff --git a/examples/RenderingExamples/TinyRendererSetup.cpp b/examples/RenderingExamples/TinyRendererSetup.cpp index a38b13abd..84f7b0033 100644 --- a/examples/RenderingExamples/TinyRendererSetup.cpp +++ b/examples/RenderingExamples/TinyRendererSetup.cpp @@ -23,6 +23,7 @@ struct TinyRendererSetupInternalData TGAImage m_rgbColorBuffer; b3AlignedObjectArray m_depthBuffer; + b3AlignedObjectArray m_shadowBuffer; b3AlignedObjectArray m_segmentationMaskBuffer; @@ -53,6 +54,7 @@ struct TinyRendererSetupInternalData m_animateRenderer(0) { m_depthBuffer.resize(m_width*m_height); + m_shadowBuffer.resize(m_width*m_height); // m_segmentationMaskBuffer.resize(m_width*m_height); } @@ -188,6 +190,7 @@ TinyRendererSetup::TinyRendererSetup(struct GUIHelperInterface* gui) TinyRenderObjectData* ob = new TinyRenderObjectData( m_internalData->m_rgbColorBuffer, m_internalData->m_depthBuffer, + m_internalData->m_shadowBuffer, &m_internalData->m_segmentationMaskBuffer, m_internalData->m_renderObjects.size()); @@ -328,6 +331,7 @@ void TinyRendererSetup::stepSimulation(float deltaTime) { m_internalData->m_rgbColorBuffer.set(x,y,clearColor); m_internalData->m_depthBuffer[x+y*m_internalData->m_width] = -1e30f; + m_internalData->m_shadowBuffer[x+y*m_internalData->m_width] = -1e30f; } } diff --git a/examples/SharedMemory/PhysicsClientExample.cpp b/examples/SharedMemory/PhysicsClientExample.cpp index 3b60d3ce7..33849d923 100644 --- a/examples/SharedMemory/PhysicsClientExample.cpp +++ b/examples/SharedMemory/PhysicsClientExample.cpp @@ -242,7 +242,7 @@ void PhysicsClientExample::prepareAndSubmitCommand(int commandId) { case CMD_LOAD_URDF: { - b3SharedMemoryCommandHandle commandHandle = b3LoadUrdfCommandInit(m_physicsClientHandle, "kuka_iiwa/model.urdf"); + b3SharedMemoryCommandHandle commandHandle = b3LoadUrdfCommandInit(m_physicsClientHandle, "torus/torus.urdf"); //setting the initial position, orientation and other arguments are optional double startPosX = 0; static double startPosY = 0; diff --git a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp index 34478bee7..53d19c503 100644 --- a/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp +++ b/examples/SharedMemory/TinyRendererVisualShapeConverter.cpp @@ -72,6 +72,7 @@ struct TinyRendererVisualShapeConverterInternalData TGAImage m_rgbColorBuffer; b3AlignedObjectArray m_textures; b3AlignedObjectArray m_depthBuffer; + b3AlignedObjectArray m_shadowBuffer; b3AlignedObjectArray m_segmentationMaskBuffer; btVector3 m_lightDirection; bool m_hasLightDirection; @@ -89,6 +90,7 @@ struct TinyRendererVisualShapeConverterInternalData m_hasLightColor(false) { m_depthBuffer.resize(m_swWidth*m_swHeight); + m_shadowBuffer.resize(m_swWidth*m_swHeight); m_segmentationMaskBuffer.resize(m_swWidth*m_swHeight,-1); } @@ -552,7 +554,7 @@ void TinyRendererVisualShapeConverter::convertVisualShapes(int linkIndex, const if (vertices.size() && indices.size()) { - TinyRenderObjectData* tinyObj = new TinyRenderObjectData(m_data->m_rgbColorBuffer,m_data->m_depthBuffer, &m_data->m_segmentationMaskBuffer, bodyUniqueId); + TinyRenderObjectData* tinyObj = new TinyRenderObjectData(m_data->m_rgbColorBuffer,m_data->m_depthBuffer, m_data->m_shadowBuffer, &m_data->m_segmentationMaskBuffer, bodyUniqueId); unsigned char* textureImage=0; int textureWidth=0; int textureHeight=0; @@ -658,6 +660,7 @@ void TinyRendererVisualShapeConverter::clearBuffers(TGAColor& clearColor) { m_data->m_rgbColorBuffer.set(x,y,clearColor); m_data->m_depthBuffer[x+y*m_data->m_swWidth] = -1e30f; + m_data->m_shadowBuffer[x+y*m_data->m_swWidth] = -1e30f; m_data->m_segmentationMaskBuffer[x+y*m_data->m_swWidth] = -1; } } @@ -772,6 +775,7 @@ void TinyRendererVisualShapeConverter::render(const float viewMat[16], const flo for (int i=0;im_swWidth;i++) { btSwap(m_data->m_depthBuffer[l1+i],m_data->m_depthBuffer[l2+i]); + btSwap(m_data->m_shadowBuffer[l1+i],m_data->m_shadowBuffer[l2+i]); btSwap(m_data->m_segmentationMaskBuffer[l1+i],m_data->m_segmentationMaskBuffer[l2+i]); } } @@ -791,6 +795,7 @@ void TinyRendererVisualShapeConverter::setWidthAndHeight(int width, int height) m_data->m_swHeight = height; m_data->m_depthBuffer.resize(m_data->m_swWidth*m_data->m_swHeight); + m_data->m_shadowBuffer.resize(m_data->m_swWidth*m_data->m_swHeight); m_data->m_segmentationMaskBuffer.resize(m_data->m_swWidth*m_data->m_swHeight); m_data->m_rgbColorBuffer = TGAImage(width, height, TGAImage::RGB); diff --git a/examples/TinyRenderer/TinyRenderer.cpp b/examples/TinyRenderer/TinyRenderer.cpp index cf71eb180..d41e6450f 100644 --- a/examples/TinyRenderer/TinyRenderer.cpp +++ b/examples/TinyRenderer/TinyRenderer.cpp @@ -13,8 +13,11 @@ #include "LinearMath/btAlignedObjectArray.h" #include "LinearMath/btVector3.h" -b3AlignedObjectArray shadowbuffer; Matrix viewportmat; +float depth = 2.0; +int indexmap[10000000]; +int count = 0; +bool setindex = true; struct Shader : public IShader { @@ -134,7 +137,7 @@ struct DepthShader : public IShader { virtual bool fragment(Vec3f bar, TGAColor &color) { Vec4f p = varying_tri*bar; - color = TGAColor(255, 255, 255)*p[2]; + color = TGAColor(255, 255, 255)*(p[2]/depth); return false; } }; @@ -153,6 +156,8 @@ struct ShadowShader : public IShader { Matrix& m_lightModelView; Vec4f m_colorRGBA; + b3AlignedObjectArray& m_shadowBuffer; + int m_width; int m_height; @@ -163,7 +168,7 @@ struct ShadowShader : public IShader { mat<4,3,float> varying_tri_light_view; mat<3,3,float> varying_nrm; // normal per vertex to be interpolated by FS - ShadowShader(Model* model, Vec3f light_dir_local, Vec3f light_color, Matrix& modelView, Matrix& lightModelView, Matrix& projectionMatrix, Matrix& modelMat, Vec3f localScaling, const Vec4f& colorRGBA, int width, int height) + ShadowShader(Model* model, Vec3f light_dir_local, Vec3f light_color, Matrix& modelView, Matrix& lightModelView, Matrix& projectionMatrix, Matrix& modelMat, Vec3f localScaling, const Vec4f& colorRGBA, int width, int height, b3AlignedObjectArray& shadowBuffer) :m_model(model), m_light_dir_local(light_dir_local), m_light_color(light_color), @@ -174,7 +179,8 @@ struct ShadowShader : public IShader { m_localScaling(localScaling), m_colorRGBA(colorRGBA), m_width(width), - m_height(height) + m_height(height), + m_shadowBuffer(shadowBuffer) { m_invModelMat = m_modelMat.invert_transpose(); } @@ -195,14 +201,18 @@ struct ShadowShader : public IShader { virtual bool fragment(Vec3f bar, TGAColor &color) { Vec4f p = viewportmat*(varying_tri_light_view*bar); - p = p/p[3]; float depth = p[2]; - int index_x = b3Min(m_width-1, int(p[0])); - index_x = b3Max(0, index_x); - int index_y = b3Min(m_height-1, int(p[1])); - index_y = b3Max(0, index_y); - int idx = index_x + index_y*m_width; // index in the shadowbuffer array - float shadow = 0.3+0.7*(shadowbuffer[idx]<-depth); // magic coeff to avoid z-fighting + //p = p/p[3]; + //int index_x = b3Min(m_width-1, int(p[0])); + //index_x = b3Max(0, index_x); + //int index_y = b3Min(m_height-1, int(p[1])); + //index_y = b3Max(0, index_y); + //int idx = index_x + index_y*m_width; // index in the shadowbuffer array + int idx = indexmap[count]; + float shadow = 0.8+0.2*(m_shadowBuffer[idx]<-depth+0.43); // magic coeff to avoid z-fighting + + //printf("count: %d, idx: %d\n", count, idx); + //printf("shadowbuffer: %f, depth: %f\n", m_shadowBuffer[idx], -depth+0.43); Vec3f bn = (varying_nrm*bar).normalize(); Vec2f uv = varying_uv*bar; @@ -234,9 +244,10 @@ struct ShadowShader : public IShader { } }; -TinyRenderObjectData::TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer) +TinyRenderObjectData::TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer,b3AlignedObjectArray&shadowBuffer) :m_rgbColorBuffer(rgbColorBuffer), m_depthBuffer(depthBuffer), +m_shadowBuffer(shadowBuffer), m_segmentationMaskBufferPtr(0), m_model(0), m_userData(0), @@ -254,9 +265,10 @@ m_objectIndex(-1) -TinyRenderObjectData::TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer, b3AlignedObjectArray* segmentationMaskBuffer, int objectIndex) +TinyRenderObjectData::TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer, b3AlignedObjectArray&shadowBuffer, b3AlignedObjectArray* segmentationMaskBuffer, int objectIndex) :m_rgbColorBuffer(rgbColorBuffer), m_depthBuffer(depthBuffer), +m_shadowBuffer(shadowBuffer), m_segmentationMaskBufferPtr(segmentationMaskBuffer), m_model(0), m_userData(0), @@ -414,18 +426,14 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData) renderData.m_viewportMatrix = viewport(0,0,width, height); b3AlignedObjectArray& zbuffer = renderData.m_depthBuffer; + b3AlignedObjectArray& 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; - shadowbuffer.resize(zbuffer.size()); - for (int i = 0; i < shadowbuffer.size(); ++i) + { - shadowbuffer[i] = -1e30f; - } - - { - Matrix lightViewMatrix = lookat(light_dir_local, Vec3f(0.0,0.0,0.0), Vec3f(0.0,0.0,1.0)); + 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; @@ -435,16 +443,31 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData) DepthShader shader(model, lightModelViewMatrix, lightViewProjectionMatrix,renderData.m_modelMatrix, localScaling); + setindex = true; + count = 0; + for (int i=0; infaces(); i++) { for (int j=0; j<3; j++) { shader.vertex(i, j); } - triangle(shader.varying_tri, shader, frame, &zbuffer[0], segmentationMaskBufferPtr, renderData.m_viewportMatrix, renderData.m_objectIndex); + triangle(shader.varying_tri, shader, tempFrame, &shadowbuffer[0], segmentationMaskBufferPtr, renderData.m_viewportMatrix, renderData.m_objectIndex); + count++; } - /* - ShadowShader shadowShader(model, light_dir_local, light_color, modelViewMatrix, lightModelViewMatrix, renderData.m_projectionMatrix,renderData.m_modelMatrix, localScaling, model->getColorRGBA(), width, height); + 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); + + setindex = false; + count = 0; for (int i=0; infaces(); i++) { @@ -452,8 +475,11 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData) shadowShader.vertex(i, j); } triangle(shadowShader.varying_tri, shadowShader, frame, &zbuffer[0], segmentationMaskBufferPtr, renderData.m_viewportMatrix, renderData.m_objectIndex); + count++; } + + /* Shader shader(model, light_dir_local, light_color, modelViewMatrix, renderData.m_projectionMatrix,renderData.m_modelMatrix, localScaling, model->getColorRGBA()); //printf("Render %d triangles.\n",model->nfaces()); diff --git a/examples/TinyRenderer/TinyRenderer.h b/examples/TinyRenderer/TinyRenderer.h index fcb8298bd..745667782 100644 --- a/examples/TinyRenderer/TinyRenderer.h +++ b/examples/TinyRenderer/TinyRenderer.h @@ -29,10 +29,11 @@ struct TinyRenderObjectData TGAImage& m_rgbColorBuffer; b3AlignedObjectArray& m_depthBuffer;//required, hence a reference + b3AlignedObjectArray& m_shadowBuffer; b3AlignedObjectArray* m_segmentationMaskBufferPtr;//optional, hence a pointer - TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer); - TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer, b3AlignedObjectArray* segmentationMaskBuffer,int objectIndex); + TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer,b3AlignedObjectArray&shadowBuffer); + TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer,b3AlignedObjectArray&shadowBuffer, b3AlignedObjectArray* segmentationMaskBuffer,int objectIndex); virtual ~TinyRenderObjectData(); void loadModel(const char* fileName); diff --git a/examples/TinyRenderer/main.cpp b/examples/TinyRenderer/main.cpp index 32f5e7edd..614b12d3a 100644 --- a/examples/TinyRenderer/main.cpp +++ b/examples/TinyRenderer/main.cpp @@ -147,6 +147,7 @@ int main(int argc, char* argv[]) renderData.m_rgbColorBuffer.set(x,y,color); renderData.m_depthBuffer[x+y*textureWidth] = -1e30f; + renderData.m_shadowBuffer[x+y*textureWidth] = -1e30f; } } diff --git a/examples/TinyRenderer/our_gl.cpp b/examples/TinyRenderer/our_gl.cpp index c2f529597..46c08dd45 100644 --- a/examples/TinyRenderer/our_gl.cpp +++ b/examples/TinyRenderer/our_gl.cpp @@ -4,11 +4,15 @@ #include "our_gl.h" #include "Bullet3Common/b3MinMax.h" +extern int indexmap[10000000]; +extern int count; +extern bool setindex; + IShader::~IShader() {} Matrix viewport(int x, int y, int w, int h) { - + /* Matrix Viewport; Viewport = Matrix::identity(); Viewport[0][3] = x+w/2.f; @@ -18,8 +22,8 @@ Matrix viewport(int x, int y, int w, int h) Viewport[1][1] = h/2.f; Viewport[2][2] = 0; return Viewport; + */ - /* Matrix Viewport; Viewport = Matrix::identity(); Viewport[0][3] = x+w/2.f; @@ -29,7 +33,7 @@ Matrix viewport(int x, int y, int w, int h) Viewport[1][1] = h/2.f; Viewport[2][2] = .5f; return Viewport; - */ + } Matrix projection(float coeff) { @@ -140,7 +144,11 @@ void triangle(mat<4,3,float> &clipc, IShader &shader, TGAImage &image, float *zb Vec3f bc_clip = Vec3f(bc_screen.x/pts[0][3], bc_screen.y/pts[1][3], bc_screen.z/pts[2][3]); bc_clip = bc_clip/(bc_clip.x+bc_clip.y+bc_clip.z); float frag_depth = -1*(clipc[2]*bc_clip); - if (bc_screen.x<0 || bc_screen.y<0 || bc_screen.z<0 || + if (setindex) + { + indexmap[count] = P.x+P.y*image.get_width(); + } + if (bc_screen.x<0 || bc_screen.y<0 || bc_screen.z<0 || zbuffer[P.x+P.y*image.get_width()]>frag_depth) continue; bool discard = shader.fragment(bc_clip, color);