PDA

View Full Version : shadow map question



xoyero
01-08-2009, 07:13 PM
I want to generate shadow map,but all value got form depth buffer is zero.these1s my test code,where`s wrong? thanks!



#include<stdlib.h>
#include<stdio.h>
#include<glew.h>
#include<glut.h>
#pragma comment(lib,"glew32.lib")

static float litPos[]={100.0f,300.0f,100.0f,1.0f};

void init()
{
glewInit();
glDisable(GL_NORMALIZE);
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClearDepth(0.0);
}

void createShadowMap()
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(150.0,1.0,1.0,400.0);

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
gluLookAt(litPos[0],litPos[1],litPos[2],0,0,0,0,1,0);

glDepthMask(GL_TRUE);
glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
glViewport(0,0,512,512);
glClear(GL_DEPTH_BUFFER_BIT);
glutSolidCube(2.0);

glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);

glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);

float* dexel=new float[512*512];
glReadPixels(0,0,512,512,GL_DEPTH_COMPONENT,GL_FLO AT,dexel);
for(int i=0;i<512*512;i++){
printf("%f\n",dexel[i]);
}
glRasterPos2i(0,0);
glDrawBuffer(GL_BACK);
glDrawPixels(512,512,GL_LUMINANCE,GL_FLOAT,dexel);
delete [] dexel;

glutSwapBuffers();
}


int main(int argc,char** argv)
{
glutInit(&amp;argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RG BA);
glutInitWindowPosition(0,0);
glutInitWindowSize(512,512);
glutCreateWindow("shadowMap");
init();
glutDisplayFunc(createShadowMap);
glutMainLoop();
return 0;
}

dletozeun
01-09-2009, 04:39 AM
If you clear depth buffer with 0 like you do here:
glClearDepth(0.0);

The depth test will always fail, since depth values are between 0 and 1, and can not be less than 0.

Moreover, this will not solve completly your problem, you are drawing from light position which is at hundreds distance units, without even taking care that you actually see the cube you are drawing!

To check, this enable color buffer writing, clear it. Maybe, put a plausible fovy value in your gluPerspective. And draw a bigger cube, or get the light closer to this one.

xoyero
01-12-2009, 06:01 PM
thanks,problem be solved