Sign in to follow this  
9ine

Less27 ShadowCasting - doesnt work in my VCproj?

Recommended Posts

9ine    122
I add code for shadow casting to my 3D renderer in VC++ from lesson 27 on ShadowCasting but no shadows at all? Why? My proj in brief as follows: There is class CObject3d it has arrays of Faces,Vertices and Edges. Edges objects has two vertices and two face neighbors, each vertex know its face and vertex neighbors and so on as in proper 3D render. (It builds adjacency automatically) simple prism object for short: vertex 0.0 2.0 0.0 2.0 0.0 0.0 -2.0 0.0 0.0 0.0 0.0 2.0 0.0 0.0 -2.0 face 0 3 1 0 2 3 0 4 2 0 1 4 Initial setup for depth and stencil as in lesson 27 ReSize(){ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, gldAspect, 1.0, 101.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0,0.0,10.0, 0.0,0.0,0.0, 0.0,1.0,0.0); } Draw(){ GLfloat light_position[] = { 0.0f, 0.0f, 5.0f, 1.0f }; /* Draw back and floor quads */ /* keep object at (0.0,0.0,0.0) */ /* hence light position is already relative and goahead */ DrawObject(); CastShadow(light_position); /* and no shadows ?????????????????? */ glFlush() } CastShadow(float *lp) { float infinity = 100.0f; float side, x,y,z; // chech to see if light is in front or behind the plane (face plane) for (int i=0; i<GetFaceNum(); i++) { side = GetFace(i)->getplane().a()*lp[0]+ GetFace(i)->getplane().b()*lp[1]+ GetFace(i)->getplane().c()*lp[2]+ GetFace(i)->getplane().d()*lp[3]; if (side > 0) GetFace(i)->isvisible(true); else GetFace(i)->isvisible(false); } glColor3f(0.0f,0.0f,0.0f); glDisable(GL_LIGHTING); glDepthMask(GL_FALSE); glDepthFunc(GL_LEQUAL); glEnable(GL_STENCIL_TEST); glColorMask(0, 0, 0, 0); glStencilFunc(GL_ALWAYS, 1, 0xffffffff); // first pass, stencil operation decreases stencil value glFrontFace(GL_CCW); glStencilOp(GL_KEEP, GL_KEEP, GL_INCR); for(int i=0; i<GetEdgeNum(); i++) { if(GetEdge(i)->getface1()->isvisible()) if(GetEdge(i)->getface2() == 0 || !GetEdge(i)->getface2()->isvisible()) { x = (GetEdge(i)->getvertex1()->x() - lp[0]) * infinity; y = (GetEdge(i)->getvertex1()->y() - lp[1]) * infinity; z = (GetEdge(i)->getvertex1()->z() - lp[2]) * infinity; CVertex v1(x,y,z); x = (GetEdge(i)->getvertex2()->x() - lp[0]) * infinity; y = (GetEdge(i)->getvertex2()->y() - lp[1]) * infinity; z = (GetEdge(i)->getvertex2()->z() - lp[2]) * infinity; CVertex v2(x,y,z); //draw the polygon glBegin(GL_TRIANGLE_STRIP); glVertex3f(GetEdge(i)->getvertex1()->x(),GetEdge(i)->getvertex1()->y(),GetEdge(i)->getvertex1()->z()); glVertex3f(GetEdge(i)->getvertex1()->x() + v1.x(), GetEdge(i)->getvertex1()->y() + v1.y(), GetEdge(i)->getvertex1()->z() + v1.z()); glVertex3f(GetEdge(i)->getvertex2()->x(),GetEdge(i)->getvertex2()->y(),GetEdge(i)->getvertex2()->z()); glVertex3f(GetEdge(i)->getvertex2()->x() + v1.x(), GetEdge(i)->getvertex2()->y() + v1.y(), GetEdge(i)->getvertex2()->z() + v1.z()); glEnd(); } } glFrontFace(GL_CW); glStencilOp(GL_KEEP, GL_KEEP, GL_DECR); for(int i=0; i<GetEdgeNum(); i++) { if(GetEdge(i)->getface1()->isvisible()) if(GetEdge(i)->getface2() == 0 || !GetEdge(i)->getface2()->isvisible()) { x = (GetEdge(i)->getvertex1()->x() - lp[0]) * infinity; y = (GetEdge(i)->getvertex1()->y() - lp[1]) * infinity; z = (GetEdge(i)->getvertex1()->z() - lp[2]) * infinity; CVertex v1(x,y,z); x = (GetEdge(i)->getvertex2()->x() - lp[0]) * infinity; y = (GetEdge(i)->getvertex2()->y() - lp[1]) * infinity; z = (GetEdge(i)->getvertex2()->z() - lp[2]) * infinity; CVertex v2(x,y,z); //draw the polygon glBegin(GL_TRIANGLE_STRIP); glVertex3f(GetEdge(i)->getvertex1()->x(),GetEdge(i)->getvertex1()->y(),GetEdge(i)->getvertex1()->z()); glVertex3f(GetEdge(i)->getvertex1()->x() + v1.x(), GetEdge(i)->getvertex1()->y() + v1.y(), GetEdge(i)->getvertex1()->z() + v1.z()); glVertex3f(GetEdge(i)->getvertex2()->x(),GetEdge(i)->getvertex2()->y(),GetEdge(i)->getvertex2()->z()); glVertex3f(GetEdge(i)->getvertex2()->x() + v1.x(), GetEdge(i)->getvertex2()->y() + v1.y(), GetEdge(i)->getvertex2()->z() + v1.z()); glEnd(); } } glFrontFace(GL_CCW); glColorMask(1, 1, 1, 1); glColor4f(0.0f, 0.0f, 0.0f, 0.4f); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glStencilFunc(GL_NOTEQUAL, 0, 0xffffffff); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); glPushMatrix(); glLoadIdentity(); glBegin(GL_TRIANGLE_STRIP); glVertex3f(-0.1f, 0.1f,-0.1f); glVertex3f(-0.1f,-0.1f,-0.1f); glVertex3f( 0.1f, 0.1f,-0.1f); glVertex3f( 0.1f,-0.1f,-0.1f); glEnd(); glPopMatrix(); glDisable(GL_BLEND); glDepthFunc(GL_LEQUAL); glDepthMask(GL_TRUE); glEnable(GL_LIGHTING); glDisable(GL_STENCIL_TEST); glShadeModel(GL_SMOOTH); }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this