PDA

View Full Version : At a loss for shadow map



xoyero
02-02-2009, 04:38 PM
Hi everyone ,i have a question needed help of everybody :I check my program code very time,but didn`t find where wrong (no shadow appear) it is,this source code:


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

#define VS 0
#define FS 1
#define SHADOW_SIZE_X 512
#define SHADOW_SIZE_Y 512
#define shadow(s) (SHADOW_SIZE_##s)

static GLuint shadowMapID;
static GLfloat litProj[16];
static GLfloat litModelview[16];
static GLfloat cameraProj[16];
static GLfloat cameraModelview[16];
static GLuint shader[2];
static GLuint program;
static float camPos[]={-2.5f,3.5f,-2.5f};
static float litPos[]={ 1.0f,3.0f,-2.0f,1.0f};

const char* source[]={
"#version 120\n"
"void main()\n"
"{\n"
"gl_Position=gl_ModelViewProjectionMatrix*gl_Vertex ;\n"
"gl_FrontColor=gl_Color;\n"
"gl_TexCoord[0]=gl_MultiTexCoord0;\n"
"}"
,
"#version 120\n"
"uniform vec4 envcol;\n"
"uniform sampler2DShadow shadowMap;\n"
"void main()\n"
"{\n"
"float coeff=shadow2DProj(shadowMap,gl_TexCoord[0]).r;\n"
"gl_FragColor=coeff*(0.15*envcol+0.85*gl_Color);\n"
"}"
};

GLuint createShader(GLenum target)
{
GLuint shaderID=glCreateShader(target);
glShaderSource(shaderID,1,(const char**)&amp;source[target==GL_VERTEX_SHADER?VS:FS],0);
glCompileShader(shaderID);
GLint compiled=0;
glGetShaderiv(shaderID,GL_COMPILE_STATUS,&amp;compiled );
if(compiled!=GL_TRUE){
switch(target){
case GL_VERTEX_SHADER:
printf("error:vertex shader compile failed\n");
break;
case GL_FRAGMENT_SHADER:
printf( "error:fragment shader compile failed\n");
break;
}
return 0;
}
return shaderID;
}

void init()
{
if(glewInit()!=GLEW_OK){
printf("error:glew initialize failed\n");
exit(0);
}

shader[VS]=createShader(GL_VERTEX_SHADER);
shader[FS]=createShader(GL_FRAGMENT_SHADER);
program=glCreateProgram();
glAttachShader(program,shader[VS]);
glAttachShader(program,shader[FS]);
glLinkProgram(program);
GLint linked=0;
glGetProgramiv(program,GL_LINK_STATUS,&amp;linked);
if(!linked){
printf("error:program link failed\n");
exit(0);
}
glGenTextures(1,&amp;shadowMapID);
glBindTexture(GL_TEXTURE_2D,shadowMapID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_DEPTH_TEXTURE_MOD E,GL_LUMINANCE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_M ODE,GL_NONE);
glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT32, shadow(X),shadow(Y),0,GL_DEPTH_COMPONENT,GL_FLOAT, 0);
glBindTexture(GL_TEXTURE_2D,0);

glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glClearDepth(1.0f);
}

void draw()
{
{
glColor4f(0.0f, 0.0f, 1.0f,.0f);
glPushMatrix();
glScalef(1.0f, 0.025f, 1.0f);
glutSolidCube(2.5f);
glPopMatrix();
}

glPushMatrix();
glRotatef(45,0.0f,1.0f,0.0f);
{
glColor4f(0.95f, 0.15f, 0.05f,.0f);
glPushMatrix();
glTranslatef(0.45f, 1.0f, 0.45f);
glutSolidSphere(0.2, 24, 24);
glPopMatrix();
}
glPopMatrix();
}

void createShadowMap()
{
glMatrixMode(GL_TEXTURE);
glPushMatrix();

glLoadIdentity();
gluPerspective(45.0f,1.0f,1.0f,100.0f);
glGetFloatv(GL_TEXTURE_MATRIX,cameraProj);
glLoadIdentity();
gluLookAt(camPos[0],camPos[1],camPos[2],0.0f, 0.0f,0.0f,0.0f,1.0f,0.0f);
glGetFloatv(GL_TEXTURE_MATRIX,cameraModelview);

glLoadIdentity();
gluPerspective(45.0f,1.0f,2.0f,8.0f);
glGetFloatv(GL_TEXTURE_MATRIX,litProj);
glLoadIdentity();
gluLookAt(litPos[0],litPos[1],litPos[2],.0,.0,.0,.0,1.,.0);
glGetFloatv(GL_TEXTURE_MATRIX,litModelview);

glPopMatrix();

if(glIsEnabled(GL_NORMALIZE)){
glDisable(GL_NORMALIZE);
}
if(glIsEnabled(GL_LIGHTING)){
glDisable(GL_LIGHTING);
}
if(glIsEnabled(GL_COLOR_MATERIAL)){
glDisable(GL_COLOR_MATERIAL);
}

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadMatrixf(litProj);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadMatrixf(litModelview);
glViewport(0,0,shadow(X),shadow(Y));
glCullFace(GL_FRONT);
glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

draw();

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

glBindTexture(GL_TEXTURE_2D,shadowMapID);
glCopyTexSubImage2D(GL_TEXTURE_2D,0,0,0,0,0,shadow (X),shadow(Y));
glBindTexture(GL_TEXTURE_2D,0);
glCullFace(GL_BACK);
glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
}

GLuint LOC(GLuint p,const char* c)
{
GLuint loc;
if((loc=glGetUniformLocation(p,c))==-1){
printf("error: %s get location failed\n",c);
}
return loc;
}

void render()
{
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(cameraProj);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(cameraModelview);
glViewport(0,0,shadow(X),shadow(Y));
glClear(GL_DEPTH_BUFFER_BIT);
draw();

float bias[16];
bias[ 0]=0.5f; bias[ 1]=0.0f; bias[ 2]=0.0f; bias[ 3]=0.0f;
bias[ 4]=0.0f; bias[ 5]=0.5f; bias[ 6]=0.0f; bias[ 7]=0.0f;
bias[ 8]=0.0f; bias[ 9]=0.0f; bias[10]=0.5f; bias[11]=0.0f;
bias[12]=0.5f; bias[13]=0.5f; bias[14]=0.5f; bias[15]=1.0f;

glViewport(0,0,shadow(X),shadow(Y));
glPushMatrix();
glLoadMatrixf(litModelview);
glMultMatrixf(litProj);
glMultMatrixf(bias);
glGetFloatv(GL_TRANSPOSE_MODELVIEW_MATRIX,bias);
glPopMatrix();

glBindTexture(GL_TEXTURE_2D,shadowMapID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_M ODE,GL_COMPARE_REF_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_F UNC,GL_LEQUAL);
glBindTexture(GL_TEXTURE_2D,0);

glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
glTexGeni(GL_R,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
glTexGeni(GL_Q,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
glTexGenfv(GL_S,GL_EYE_PLANE,&amp;bias[ 0]);
glTexGenfv(GL_T,GL_EYE_PLANE,&amp;bias[ 4]);
glTexGenfv(GL_R,GL_EYE_PLANE,&amp;bias[ 8]);
glTexGenfv(GL_Q,GL_EYE_PLANE,&amp;bias[12]);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);
glEnable(GL_TEXTURE_GEN_Q);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

glUseProgram(program);
glUniform4f(LOC(program,"envcol"),.3f,.3f,.3f,.0f);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,shadowMapID);
glUniform1i(LOC(program,"shadowMap"),0);

draw();

glUseProgram(0);
glBindTexture(GL_TEXTURE_2D,0);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_GEN_Q);
glDisable(GL_DEPTH_TEST);

glFinish();
glutSwapBuffers();
}

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