PDA

View Full Version : Shadow mapping



Bob666
04-06-2010, 01:49 PM
Hi,

I'm trying to do some shadow mapping but I'm having
a hard time making it works. I use this code:

glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHT0);
glDisable(GL_LIGHTING);

GLfloat ambient[] = {0.0,0.0,0.0,0.0};
GLfloat position[] = {0.5,1.3,0.5};

GLfloat diffuse[] = {0.2,0.2,0.2,0.0};
GLfloat specular[] = {0.0,0.0,0.0,0.0};
GLfloat shininess[] = {0};

GLfloat ambiantMat[] = {0.0,0.0,0.0,0.0};
GLfloat diffuseMat[] = {0.2,0.2,0.2,0.0};
GLfloat emissiveMat[] = {0.0,0.0,0.0,1.0};
GLfloat specularMat[] = {0.0,0.0,0.0,0.0};
GLfloat shininessMat[] = {0};

glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_SHININESS, shininess);

glMaterialfv(GL_FRONT, GL_AMBIENT, ambiantMat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMat);
glMaterialfv(GL_FRONT, GL_EMISSION, emissiveMat);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularMat);
glMaterialfv(GL_FRONT, GL_SHININESS, shininessMat);

glMaterialfv(GL_BACK, GL_AMBIENT, ambiantMat);
glMaterialfv(GL_BACK, GL_DIFFUSE, diffuseMat);
glMaterialfv(GL_BACK, GL_EMISSION, emissiveMat);
glMaterialfv(GL_BACK, GL_SPECULAR, specularMat);
glMaterialfv(GL_BACK, GL_SHININESS, shininessMat);

glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

GLint viewport[4];
GLfloat lightPos[3];
glGetLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glGetIntegerv(GL_VIEWPORT,viewport);
glViewport(0,0,1024,1024);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(90, 4/3, 0.1, 10);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
gluLookAt(lightPos[0],lightPos[1],lightPos[2], 0,0,0, 0,1,0);

//---Drawing
glMatrixMode(GL_MODELVIEW);

glPushMatrix();
glLoadIdentity();

GLUquadricObj* cyl = gluNewQuadric();
glColor3f(1,1,0);
glTranslatef(0.4,0.4,0.4);
gluSphere(cyl, 0.2, 100, 100);

glPopMatrix();

glPushMatrix();
glLoadIdentity();
glTranslatef(-0.4,-0.4,-0.4);
glBegin(GL_POLYGON);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glVertex3f(-1.0,0.0,1.0);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glVertex3f(1.0,0.0,1.0);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glVertex3f(1.0,0.0,-1.0);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glVertex3f(-1.0,0.0,-1.0);

glEnd();

glPopMatrix();


glLoadIdentity();
//---End Drawing

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

glEnable(GL_DEPTH_TEST);

glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, 1024, 1024,0);
glViewport(viewport[0],viewport[1],viewport[2],viewport[3]);

GLfloat tempMat[16];
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.5,0.5,0.0);
glScalef(0.5,0.5,1.0);
gluPerspective(90, 4/3, 0.1, 10);
gluLookAt(0,2,2, 0,0,0, 0,1,0);
glGetFloatv(GL_MODELVIEW,tempMat);
glPopMatrix();



GLfloat *t = TransposeMat(tempMat);


glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);

glTexGenfv(GL_S, GL_OBJECT_PLANE, &t[0]);
glTexGenfv(GL_T, GL_OBJECT_PLANE, &t[4]);
glTexGenfv(GL_R, GL_OBJECT_PLANE, &t[8]);
glTexGenfv(GL_Q, GL_OBJECT_PLANE, &t[12]);

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);
glEnable(GL_TEXTURE_GEN_Q);

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_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glEnable(GL_TEXTURE_2D);

//---Drawing
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);

glDisable(GL_LIGHT0);
glDisable(GL_LIGHTING);


ambient[0] = 0.3;
ambient[1] = 0.3;
ambient[2] = 0.3;
diffuse[0] = 0.7;
diffuse[1] = 0.7;
diffuse[2] = 0.7;
specular[0] = 0.1;
specular[1] = 0.1;
specular[2] = 0.1;
emissiveMat[0] = 0.0;
emissiveMat[1] = 0.0;
emissiveMat[2] = 0.0;
specularMat[0] = 0.1;
specularMat[1] = 0.1;
specularMat[2] = 0.1;
shininess[0] = 0.7;
shininessMat[0] = 0.7;
ambiantMat[0] = 1;
ambiantMat[1] = 1;
ambiantMat[2] = 1;
diffuseMat[0] = 1;
diffuseMat[1] = 1;
diffuseMat[2] = 1;

glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_SHININESS, shininess);

glMaterialfv(GL_FRONT, GL_AMBIENT, ambiantMat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMat);
glMaterialfv(GL_FRONT, GL_EMISSION, emissiveMat);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularMat);
glMaterialfv(GL_FRONT, GL_SHININESS, shininessMat);

glMaterialfv(GL_BACK, GL_AMBIENT, ambiantMat);
glMaterialfv(GL_BACK, GL_DIFFUSE, diffuseMat);
glMaterialfv(GL_BACK, GL_EMISSION, emissiveMat);
glMaterialfv(GL_BACK, GL_SPECULAR, specularMat);
glMaterialfv(GL_BACK, GL_SHININESS, shininessMat);

glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

glPushMatrix();
glLoadIdentity();

GLUquadricObj* cyl1 = gluNewQuadric();
glColor3f(1,1,0);
glTranslatef(0.4,0.4,0.4);
gluSphere(cyl1, 0.2, 100, 100);

glPopMatrix();

glPushMatrix();
glLoadIdentity();

glBegin(GL_POLYGON);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glTexCoord2f(1.0, 0.0);
glVertex3f(-1.0,0.0,1.0);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glTexCoord2f(1.0, 1.0);
glVertex3f(1.0,0.0,1.0);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glTexCoord2f(0.0, 1.0);
glVertex3f(1.0,0.0,-1.0);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glTexCoord2f(0.0, 0.0);
glVertex3f(-1.0,0.0,-1.0);

glEnd();
glPopMatrix();

glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_GEN_Q);


I dont want to use fancy stuff. I red Nehe and Paul's Projects
tutorial but I just want to know whats wrong with my code. I post this here but I'm not sure if it's the right places.

Thanks

Bob666
04-06-2010, 02:13 PM
I forgot to say that it compile but there is no shadow

Bob666
04-06-2010, 04:08 PM
Sorry, easier to read that way




glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHT0);
glDisable(GL_LIGHTING);

GLfloat ambient[] = {0.0,0.0,0.0,0.0};
GLfloat position[] = {0.5,1.3,0.5};

GLfloat diffuse[] = {0.2,0.2,0.2,0.0};
GLfloat specular[] = {0.0,0.0,0.0,0.0};
GLfloat shininess[] = {0};

GLfloat ambiantMat[] = {0.0,0.0,0.0,0.0};
GLfloat diffuseMat[] = {0.2,0.2,0.2,0.0};
GLfloat emissiveMat[] = {0.0,0.0,0.0,1.0};
GLfloat specularMat[] = {0.0,0.0,0.0,0.0};
GLfloat shininessMat[] = {0};

glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_SHININESS, shininess);

glMaterialfv(GL_FRONT, GL_AMBIENT, ambiantMat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMat);
glMaterialfv(GL_FRONT, GL_EMISSION, emissiveMat);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularMat);
glMaterialfv(GL_FRONT, GL_SHININESS, shininessMat);

glMaterialfv(GL_BACK, GL_AMBIENT, ambiantMat);
glMaterialfv(GL_BACK, GL_DIFFUSE, diffuseMat);
glMaterialfv(GL_BACK, GL_EMISSION, emissiveMat);
glMaterialfv(GL_BACK, GL_SPECULAR, specularMat);
glMaterialfv(GL_BACK, GL_SHININESS, shininessMat);

glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

GLint viewport[4];
GLfloat lightPos[3];
glGetLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glGetIntegerv(GL_VIEWPORT,viewport);
glViewport(0,0,1024,1024);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(90, 4/3, 0.1, 10);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
gluLookAt(lightPos[0],lightPos[1],lightPos[2], 0,0,0, 0,1,0);

//---Drawing
glMatrixMode(GL_MODELVIEW);

glPushMatrix();
glLoadIdentity();

GLUquadricObj* cyl = gluNewQuadric();
glColor3f(1,1,0);
glTranslatef(0.4,0.4,0.4);
gluSphere(cyl, 0.2, 100, 100);

glPopMatrix();

glPushMatrix();
glLoadIdentity();
glTranslatef(-0.4,-0.4,-0.4);
glBegin(GL_POLYGON);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glVertex3f(-1.0,0.0,1.0);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glVertex3f(1.0,0.0,1.0);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glVertex3f(1.0,0.0,-1.0);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glVertex3f(-1.0,0.0,-1.0);

glEnd();

glPopMatrix();


glLoadIdentity();
//---End Drawing

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

glEnable(GL_DEPTH_TEST);

glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, 1024, 1024,0);
glViewport(viewport[0],viewport[1],viewport[2],viewport[3]);

GLfloat tempMat[16];
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.5,0.5,0.0);
glScalef(0.5,0.5,1.0);
gluPerspective(90, 4/3, 0.1, 10);
gluLookAt(0,2,2, 0,0,0, 0,1,0);
glGetFloatv(GL_MODELVIEW,tempMat);
glPopMatrix();



GLfloat *t = TransposeMat(tempMat);


glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);

glTexGenfv(GL_S, GL_OBJECT_PLANE, &t[0]);
glTexGenfv(GL_T, GL_OBJECT_PLANE, &t[4]);
glTexGenfv(GL_R, GL_OBJECT_PLANE, &t[8]);
glTexGenfv(GL_Q, GL_OBJECT_PLANE, &t[12]);

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);
glEnable(GL_TEXTURE_GEN_Q);

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_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glEnable(GL_TEXTURE_2D);

//---Drawing
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);

glDisable(GL_LIGHT0);
glDisable(GL_LIGHTING);


ambient[0] = 0.3;
ambient[1] = 0.3;
ambient[2] = 0.3;
diffuse[0] = 0.7;
diffuse[1] = 0.7;
diffuse[2] = 0.7;
specular[0] = 0.1;
specular[1] = 0.1;
specular[2] = 0.1;
emissiveMat[0] = 0.0;
emissiveMat[1] = 0.0;
emissiveMat[2] = 0.0;
specularMat[0] = 0.1;
specularMat[1] = 0.1;
specularMat[2] = 0.1;
shininess[0] = 0.7;
shininessMat[0] = 0.7;
ambiantMat[0] = 1;
ambiantMat[1] = 1;
ambiantMat[2] = 1;
diffuseMat[0] = 1;
diffuseMat[1] = 1;
diffuseMat[2] = 1;

glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_SHININESS, shininess);

glMaterialfv(GL_FRONT, GL_AMBIENT, ambiantMat);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMat);
glMaterialfv(GL_FRONT, GL_EMISSION, emissiveMat);
glMaterialfv(GL_FRONT, GL_SPECULAR, specularMat);
glMaterialfv(GL_FRONT, GL_SHININESS, shininessMat);

glMaterialfv(GL_BACK, GL_AMBIENT, ambiantMat);
glMaterialfv(GL_BACK, GL_DIFFUSE, diffuseMat);
glMaterialfv(GL_BACK, GL_EMISSION, emissiveMat);
glMaterialfv(GL_BACK, GL_SPECULAR, specularMat);
glMaterialfv(GL_BACK, GL_SHININESS, shininessMat);

glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

glPushMatrix();
glLoadIdentity();

GLUquadricObj* cyl1 = gluNewQuadric();
glColor3f(1,1,0);
glTranslatef(0.4,0.4,0.4);
gluSphere(cyl1, 0.2, 100, 100);

glPopMatrix();

glPushMatrix();
glLoadIdentity();

glBegin(GL_POLYGON);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glTexCoord2f(1.0, 0.0);
glVertex3f(-1.0,0.0,1.0);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glTexCoord2f(1.0, 1.0);
glVertex3f(1.0,0.0,1.0);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glTexCoord2f(0.0, 1.0);
glVertex3f(1.0,0.0,-1.0);

glNormal3f(0,1,0);
glColor3f(1,0,1);
glTexCoord2f(0.0, 0.0);
glVertex3f(-1.0,0.0,-1.0);

glEnd();
glPopMatrix();

glDisable(GL_TEXTURE_2D);
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
glDisable(GL_TEXTURE_GEN_R);
glDisable(GL_TEXTURE_GEN_Q);

dorbie
04-06-2010, 04:34 PM
Can you work with us here? This looks like the output from a glTrace utility.

Please expound on what you are attempting to do.

Does it work without shadows, what shadow algorithm are you trying to use.

zeoverlord
04-06-2010, 04:53 PM
I dont want to use fancy stuff. I red Nehe and Paul's Projects
tutorial but I just want to know whats wrong with my code. I post this here but I'm not sure if it's the right places.

Thanks

-------

I forgot to say that it compile but there is no shadow

Yea thats about right, it could be the texturing (of which i saw little of) or any of a handful of problems, doing it this way is pretty complicated and prone to problems (and did you really learn how it works), which is exactly why we don't do it this way anymore.
go here (http://www.flashbang.se/index.php?id=35) and see how it should be done

Bob666
04-06-2010, 05:05 PM
Hi,

Your right, I did'nt explain much. I'm trying to create the shadow of a sphere and make it appear on a plane surface.

I'm using this algorithm :

1-Render the scene from light source
2-Create a shadow map with the depth buffer
3-Generate texture coordonnates
4-Render the scene from the camera point of view


http://en.wikipedia.org/wiki/Shadow_mapping

http://www.cs.uiowa.edu/~cwyman/classes/common/gfxHandouts/shadowMapSteps.pdf

Thanks

Bob666
04-06-2010, 05:20 PM
Hi zeoverlord,

What I learn is a few lines of codes from my teacher and how its SUPPOSE to works, but nothing more. I've already went to the link you proposed and was'nt interrested in using something I did'nt learn at school.

I will understand if you dont want to help me anymore guys, since I'm a very beginner

Thanks

Dark Photon
04-06-2010, 06:13 PM
What I learn is a few lines of codes from my teacher and how its SUPPOSE to works, but nothing more.
If your teacher is telling you to do shadow mapping via "ancient" TEXGEN, you probably should find a new teacher.


I will understand if you dont want to help me anymore guys, since I'm a very beginner
Heck no, you'll find plenty of willing help here, as long as you don't just throw a bunch of code that doesn't compile at us and ask "what's wrong"?

Cook a short GLUT program that renders a cube or sphere with a plane under it. Next try to add shadow mapping. If you get stuck, post what you have -- something concrete that compiles and that folks can "grab onto". Even better, also describe what specifically you were trying to do when you "got lost", and what your rationale for doing it was.


I've already went to the link you proposed and was'nt interrested in using something I did'nt learn at school.
There's value in it regardless in teaching you the underlying concept. If you don't understand the concept, what way you try to do it is irrelevant. You'll fail except by random chance or by copying code.

Incidentally, here's an old OpenGL Shadow Mapping tutorial, and you're in luck. It uses that ancient TEX_GEN feature your teacher is hooked on:

* Shadow Mapping (Paul's Projects) (http://www.paulsprojects.net/tutorials/smt/smt.html)

dorbie
04-07-2010, 04:02 PM
There's nothing wrong with texgen in a fixed function pipeline, especially relative to eyespace for shadows because it's pretty much the equivalent of what you'll wind up doing in shaders eventually.

Just get ready to learn shaders and roll your own because that's where everything is headed.