From 5be4409a1a52c741b13dafa924e1ef258248bc3a Mon Sep 17 00:00:00 2001 From: yunfeibai Date: Mon, 21 Nov 2016 09:13:20 -0800 Subject: [PATCH] Render shadow buffer. --- examples/TinyRenderer/TinyRenderer.cpp | 56 +++++++++++++++++++++++++- examples/TinyRenderer/our_gl.cpp | 31 ++++++++++++++ 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/examples/TinyRenderer/TinyRenderer.cpp b/examples/TinyRenderer/TinyRenderer.cpp index 76b9e4053..88102e86a 100644 --- a/examples/TinyRenderer/TinyRenderer.cpp +++ b/examples/TinyRenderer/TinyRenderer.cpp @@ -13,7 +13,7 @@ #include "LinearMath/btAlignedObjectArray.h" #include "LinearMath/btVector3.h" - +const float depth = 2.f; struct Shader : public IShader { @@ -94,6 +94,55 @@ struct Shader : public IShader { } }; +struct DepthShader : public IShader { + + Model* m_model; + Matrix& m_modelMat; + Matrix m_invModelMat; + + Matrix& m_modelView1; + Matrix& m_projectionMatrix; + Vec3f m_localScaling; + Matrix& m_lightModelView; + + mat<2,3,float> varying_uv; // triangle uv coordinates, written by the vertex shader, read by the fragment shader + mat<4,3,float> varying_tri; // triangle coordinates (clip coordinates), written by VS, read by FS + mat<4,3,float> varying_tri_light_view; // triangle coordinates (clip coordinates), written by VS, read by FS + mat<3,3,float> varying_nrm; // normal per vertex to be interpolated by FS + + DepthShader(Model* model, Matrix& modelView, Matrix& lightModelView, Matrix& projectionMatrix, Matrix& modelMat, Vec3f localScaling) + :m_model(model), + m_modelView1(modelView), + m_lightModelView(lightModelView), + m_projectionMatrix(projectionMatrix), + m_modelMat(modelMat), + m_localScaling(localScaling) + { + m_invModelMat = m_modelMat.invert_transpose(); + } + virtual Vec4f vertex(int iface, int nthvert) { + Vec2f uv = m_model->uv(iface, nthvert); + varying_uv.set_col(nthvert, uv); + varying_nrm.set_col(nthvert, proj<3>(m_invModelMat*embed<4>(m_model->normal(iface, nthvert), 0.f))); + Vec3f unScaledVert = m_model->vert(iface, nthvert); + Vec3f scaledVert=Vec3f(unScaledVert[0]*m_localScaling[0], + unScaledVert[1]*m_localScaling[1], + unScaledVert[2]*m_localScaling[2]); + Vec4f gl_Vertex = m_projectionMatrix*m_modelView1*embed<4>(scaledVert); + varying_tri.set_col(nthvert, gl_Vertex); + Vec4f gl_VertexLightView = m_projectionMatrix*m_lightModelView*embed<4>(scaledVert); + varying_tri_light_view.set_col(nthvert, gl_VertexLightView); + return gl_Vertex; + } + + virtual bool fragment(Vec3f bar, TGAColor &color) { + Vec4f p = varying_tri_light_view*bar; + printf("coefficient: %f\n", 1.0-p[2]/depth); + color = TGAColor(255, 255, 255)*(1.0-p[2]/depth); + return false; + } +}; + TinyRenderObjectData::TinyRenderObjectData(TGAImage& rgbColorBuffer,b3AlignedObjectArray&depthBuffer) :m_rgbColorBuffer(rgbColorBuffer), m_depthBuffer(depthBuffer), @@ -283,9 +332,12 @@ void TinyRenderer::renderObject(TinyRenderObjectData& renderData) { + Matrix lightViewMatrix = lookat(Vec3f(0.0,0.1,2.0), Vec3f(0.0,0.0,0.0), Vec3f(0.0,0.0,1.0)); + Matrix lightModelViewMatrix = lightViewMatrix*renderData.m_modelMatrix; Matrix modelViewMatrix = renderData.m_viewMatrix*renderData.m_modelMatrix; Vec3f localScaling(renderData.m_localScaling[0],renderData.m_localScaling[1],renderData.m_localScaling[2]); - Shader shader(model, light_dir_local, light_color, modelViewMatrix, renderData.m_projectionMatrix,renderData.m_modelMatrix, localScaling, model->getColorRGBA()); + //Shader shader(model, light_dir_local, light_color, modelViewMatrix, renderData.m_projectionMatrix,renderData.m_modelMatrix, localScaling, model->getColorRGBA()); + DepthShader shader(model, modelViewMatrix, lightModelViewMatrix, renderData.m_projectionMatrix,renderData.m_modelMatrix, localScaling); //printf("Render %d triangles.\n",model->nfaces()); for (int i=0; infaces(); i++) diff --git a/examples/TinyRenderer/our_gl.cpp b/examples/TinyRenderer/our_gl.cpp index eb5a05a78..21300945f 100644 --- a/examples/TinyRenderer/our_gl.cpp +++ b/examples/TinyRenderer/our_gl.cpp @@ -30,6 +30,7 @@ Matrix projection(float coeff) { } Matrix lookat(Vec3f eye, Vec3f center, Vec3f up) { + /* Vec3f z = (eye-center).normalize(); Vec3f x = cross(up,z).normalize(); Vec3f y = cross(z,x).normalize(); @@ -44,6 +45,36 @@ Matrix lookat(Vec3f eye, Vec3f center, Vec3f up) { Matrix ModelView; ModelView = Minv*Tr; return ModelView; + */ + + Vec3f f = (center - eye).normalize(); + Vec3f u = up.normalize(); + Vec3f s = cross(f,u).normalize(); + u = cross(s,f); + + Matrix ModelView; + ModelView[0][0] = s.x; + ModelView[0][1] = s.y; + ModelView[0][2] = s.z; + + ModelView[1][0] = u.x; + ModelView[1][1] = u.y; + ModelView[1][2] = u.z; + + ModelView[2][0] =-f.x; + ModelView[2][1] =-f.y; + ModelView[2][2] =-f.z; + + ModelView[3][0] = 0.f; + ModelView[3][1] = 0.f; + ModelView[3][2] = 0.f; + + ModelView[0][3] = -(s[0]*eye[0]+s[1]*eye[1]+s[2]*eye[2]); + ModelView[1][3] = -(u[0]*eye[0]+u[1]*eye[1]+u[2]*eye[2]); + ModelView[2][3] = f[0]*eye[0]+f[1]*eye[1]+f[2]*eye[2]; + ModelView[3][3] = 1.f; + + return ModelView; } Vec3f barycentric(Vec2f A, Vec2f B, Vec2f C, Vec2f P) {