PDA

View Full Version : Two problem about shadow mapping?



Liufu
10-04-2010, 06:37 AM
Hello,
I tried making shadow mapping for some torus. I stored the depth test result into a texture. Because, I need compute rotation for torus, and floor is no need do this. I used two shader snippets for torus shadow and floor shadow. But I found two problems. One is I can't make shadow for floor, only can make shadow for torus. The other problem is I find my shadow effect is not fine. Some shadow looks like formed by many isolated dark points. I attached my code below. Thanks!

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
setlight();
GenShadowMap();

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(Camera_ProjectionMatrix.mt);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(Camera_ViewMatrix.mt);

glTranslatef(translate_x, translate_y, translate_z);
glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
glGetFloatv(GL_MODELVIEW_MATRIX, Camera_ViewInverse.mt);
Camera_ViewInverse = Camera_ViewInverse.GetmvMatrixInverse();
CastShadowMap();

glutSwapBuffers();
}

void GenShadowMap()
{
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glShadeModel(GL_FLAT);

glPolygonOffset(2.0f, 4.0f);
glEnable(GL_POLYGON_OFFSET_FILL);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, depthFBO);
glDrawBuffer(GL_DEPTH_ATTACHMENT_EXT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

drawObjects();//////////////////////////1
drawFloor();////////////////////////////1

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);

glShadeModel(GL_SMOOTH);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDisable(GL_POLYGON_OFFSET_FILL);
}

void setlight()
{
GLfloat Lf[4];
GLfloat Lc[4];

/* Use a white light. */
Lc[0] = 1.0;
Lc[1] = 1.0;
Lc[2] = 1.0;
Lc[3] = 1.0;
glLightfv(GL_LIGHT0, GL_DIFFUSE, &Lc[0]);
glLightfv(GL_LIGHT0, GL_SPECULAR, &Lc[0]);

glEnable(GL_LIGHT0);

ambientLight(1);

Lf[0] = L[0];
Lf[1] = L[1];
Lf[2] = L[2];
Lf[3] = L[3];
glLightfv(GL_LIGHT0, GL_POSITION, &Lf[0]);

glMatrixMode(GL_PROJECTION);
glLoadMatrixf(Light_ProjectionMatrix.mt);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(Light_ViewMatrix.mt);

glDisable(GL_LIGHTING);
glPushMatrix();
/* Draw light source position as a Red dot. */
glColor3f(1,0,0);
glBegin(GL_POINTS);
glVertex3dv(L);
glEnd();
glPopMatrix();
glEnable(GL_LIGHTING);
}

void CastShadowMap()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, shadowmapid);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);

glMatrixMode(GL_TEXTURE);
glPushAttrib(GL_TEXTURE_BIT);
glActiveTextureARB(GL_TEXTURE7);
glLoadIdentity();
glLoadMatrixf(Light_ProjectionMatrix.mt);
glMultMatrixf(Light_ViewMatrix.mt);
glMultMatrixf(Camera_ViewInverse.mt);

glMatrixMode(GL_MODELVIEW);
RenderObjects();
glBindTexture(GL_TEXTURE_2D,0);
glDisable(GL_TEXTURE_2D);
glPopAttrib();
}

void init(void)
{
checkGLExtensions();
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_TEXTURE_2D);
SetFrameBufferObject(wWidth, wHeight);

glGenTextures(1,&floorTex);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(65.0, (double)wWidth / (double)wHeight, 0.001, 1000.0);
glGetFloatv(GL_PROJECTION_MATRIX, Camera_ProjectionMatrix.mt);
glPopMatrix();

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
gluLookAt(.0, .0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glGetFloatv(GL_MODELVIEW_MATRIX, Camera_ViewMatrix.mt);
glPopMatrix();

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(45.0f, (double)wWidth / (double)wHeight, 0.001, 1000.0);
glGetFloatv(GL_PROJECTION_MATRIX, Light_ProjectionMatrix.mt);
glPopMatrix();

glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
gluLookAt( L[0], L[1], L[2],
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, Light_ViewMatrix.mt);
glPopMatrix();

glPointSize(16.0);

if (useTextures)
{
makeFloorTexture();
}

}

shadowmap1.vert
varying vec4 shadowTexcoord;
varying vec4 diffuse;
void main()
{
vec4 centerOfMass;
vec2 texCrd;
.................................................. ......
rotatedPos.xyz = rotatedPos.xyz + centerOfMass.xyz;
gl_Position = gl_ModelViewProjectionMatrix * rotatedPos;

vec4 Vpos = gl_ModelViewMatrix * rotatedPos;
vec3 pos = Vpos.xyz / Vpos.w;

vec3 lightdir = normalize( vec3(gl_LightSource[0].position) - pos);
norm = normalize(gl_NormalMatrix * gl_Normal);

float NdotL = max(0.0, dot(lightdir, norm));
diffuse = gl_LightSource[0].diffuse * NdotL;

vec4 texcoord = gl_TextureMatrix[7] * gl_ModelViewMatrix * rotatedPos;

shadowTexcoord = texcoord / texcoord.w;
shadowTexcoord = 0.5 * shadowTexcoord +0.5;

gl_FrontColor = gl_Color;
}

shadowmap1.frag
uniform sampler2DShadow shadowmap;
varying vec3 norm;
varying vec4 shadowTexcoord;
varying vec4 diffuse;
void main()
{
const float epsilon = 0.5;

float factor = 1.0;
float depth = shadow2DProj(shadowmap, shadowTexcoord).r + epsilon;
depth = clamp(depth, 0.0, 1.0);
if(depth != 1.0)factor = 0.25;


if(shadowTexcoord.x >= 0.0 && shadowTexcoord.y >= 0.0
&amp;&amp; shadowTexcoord.x <= 1.0 &amp;&amp; shadowTexcoord.y <= 1.0 )
{
gl_FragColor = vec4(gl_Color.rgb* diffuse.rgb * factor, 1.0);
}
else
{
gl_FragColor = vec4(gl_Color.rgb* diffuse.rgb, 1.0);
}
}

shadowmap2.vert
varying vec3 norm;
varying vec4 shadowTexcoord;
varying vec4 diffuse;
void main()
{


vec4 Vpos = gl_ModelViewMatrix * gl_Vertex;
vec3 pos = Vpos.xyz / Vpos.w;

vec3 lightdir = normalize( vec3(gl_LightSource[0].position) - pos);
norm = normalize(gl_NormalMatrix * gl_Normal);

float NdotL = max(0.0, dot(lightdir, norm));
diffuse = gl_LightSource[0].diffuse * NdotL;

vec4 texcoord = gl_TextureMatrix[7] * gl_ModelViewMatrix * gl_Vertex;

shadowTexcoord = texcoord / texcoord.w;
shadowTexcoord = 0.5 * shadowTexcoord +0.5;

gl_FrontColor = gl_Color;
}

shadowmap2.frag
uniform sampler2DShadow shadowmap;
varying vec3 norm;
varying vec4 shadowTexcoord;
varying vec4 diffuse;
void main()
{
const float epsilon = 0.5;

float factor = 1.0;
float depth = shadow2DProj(shadowmap, shadowTexcoord).r + epsilon;
depth = clamp(depth, 0.0, 1.0);
if(depth != 1.0)factor = 0.25;


if(shadowTexcoord.x >= 0.0 &amp;&amp; shadowTexcoord.y >= 0.0
&amp;&amp; shadowTexcoord.x <= 1.0 &amp;&amp; shadowTexcoord.y <= 1.0 )
{
gl_FragColor = vec4(gl_Color.rgb* diffuse.rgb * factor, 1.0);
}
else
{
gl_FragColor = vec4(gl_Color.rgb* diffuse.rgb, 1.0);
}
}

Liufu
10-04-2010, 07:23 PM
I called the shader like this.
void Demo::renderShadow()
{
setshader("shadowmap1.vert","shadowmap1.frag");
const int nBodies = m_system->getNumBodies();
glPushAttrib(GL_TEXTURE_BIT);
glUseProgram(shaderprogram);
texLoc = glGetUniformLocationARB(shaderprogram, "posTex");
glUniform1iARB(texLoc, 0);
texLoc = glGetUniformLocationARB(shaderprogram, "quatTex");
glUniform1iARB(texLoc, 1);
texLoc = glGetUniformLocationARB(shaderprogram, "shadowmap");
glUniform1iARB(texLoc, 2);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, shadowmapid);

glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,indexV BO);
glBindBufferARB(GL_ARRAY_BUFFER, vertexVBO);
glVertexPointer(3,GL_FLOAT,sizeof(Vertex),0);
glNormalPointer(GL_FLOAT,sizeof(Vertex),(void*)(si zeof(Vertex)/2));

glDrawElementsInstancedEXT(GL_QUADS, 150*4, GL_UNSIGNED_INT, 0, nBodies);

glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_NORMAL_ARRAY );
glUseProgram(0);
glBindTexture(GL_TEXTURE_2D, 0);
glPopAttrib();

glPushAttrib(GL_TEXTURE_BIT);
glUseProgram(floorprogram);
texLoc = glGetUniformLocationARB(shaderprogram, "shadowmap");
glUniform1iARB(texLoc, 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, shadowmapid);
glCallList(floor_display_list);
glUseProgram(0);
glBindTexture(GL_TEXTURE_2D, 0);
glPopAttrib();
}

BionicBytes
10-05-2010, 04:50 AM
I'm not sure if I or anyone else can follow what you are saying.
Perhaps just start by asking one question and describe the issue (with pictures?)

Liufu
10-05-2010, 04:58 AM
Yes, you're right. I will attach the image.Thanks!

Liufu
10-05-2010, 05:05 AM
Here is the snapshot. You can see many isolated dark points. And no shadow on floor, just shadow on torus. I just don't understand why I save the depth information into texture. But only depth information take effect on torus not on floor. It should be the same usage. Thanks!

nickels
10-05-2010, 07:01 AM
Wow, cool pic. It looks like the infinite land of fractal tires!

Liufu
10-05-2010, 07:08 AM
Thanks.
Actually, the floor is finite, but big:)

BionicBytes
10-05-2010, 09:41 AM
you have got so much going on there, who's tosay what you should see.
Perhaps simplify the scene down to one or two torus, and then lets see if it casts shadow on the terrain.

Liufu
10-05-2010, 06:55 PM
Thanks. You see this new snapshot. Only shadow can be add on torus, not on the floor. And in some place, shadow look like isolated points. I also want to know others how do they add shadow for many objects, using DirectX:)

BionicBytes
10-06-2010, 01:48 AM
You are not applying the shadowmap to the floor object.
You have not posted the code for the floorprogram so I cannot say for sure. Also, some of your shader code you posted earlier is incomplete as I can not follow through with your uniforms and samplers as they are missing from the shaders.

Liufu
10-06-2010, 01:56 AM
Sorry, the code seems too long.
void drawFloor()
{
float x, y;
float d = 1.0;
float s = 60.0f;
/* Draw ground. */
if (useTextures) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,floorTex);
}
glColor3f(1,1,0);
glNormal3f(0,1,0);
glPushMatrix();
glTranslatef(0.0, 1.0, 0.0);

/* Tesselate floor so lighting looks reasonable. */
for (x=-s; x<s; x+=d) {
glBegin(GL_QUAD_STRIP);
for (y=-s; y<s; y+=d) {
glTexCoord2f(x+1, y);
glVertex3f(x+1, -1, y);
glTexCoord2f(x, y);
glVertex3f(x, -1, y);
}
glEnd();
}
glPopMatrix();

if (useTextures) {
glBindTexture(GL_TEXTURE_2D,0);
glDisable(GL_TEXTURE_2D);
}
}

void drawObjects()
{
glPushMatrix();
s_demo->render();
glPopMatrix();
}

void RenderObjects()
{
glPushMatrix();
s_demo->renderShadow();
glPopMatrix();
}

void Demo::renderShadow()
{
const int nBodies = m_system->getNumBodies();
glPushAttrib(GL_TEXTURE_BIT);
glUseProgram(shaderprogram);
texLoc = glGetUniformLocationARB(shaderprogram, "posTex");
glUniform1iARB(texLoc, 0);
texLoc = glGetUniformLocationARB(shaderprogram, "quatTex");
glUniform1iARB(texLoc, 1);
texLoc = glGetUniformLocationARB(shaderprogram, "shadowmap");
glUniform1iARB(texLoc, 2);

glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, shadowmapid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);

glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,indexV BO);
glBindBufferARB(GL_ARRAY_BUFFER, vertexVBO);
glVertexPointer(3,GL_FLOAT,sizeof(Vertex),0);
glNormalPointer(GL_FLOAT,sizeof(Vertex),(void*)(si zeof(Vertex)/2));

glDrawElementsInstancedEXT(GL_QUADS, 150*4, GL_UNSIGNED_INT, 0, nBodies);

glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_NORMAL_ARRAY );
glUseProgram(0);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
glPopAttrib();

glPushAttrib(GL_TEXTURE_BIT);
glUseProgram(floorprogram);
texLoc = glGetUniformLocationARB(shaderprogram, "shadowmap");
glUniform1iARB(texLoc, 0);

glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, shadowmapid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);

glCallList(floor_display_list);
glUseProgram(0);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
glPopAttrib();
}

void DemoP::render()
{

const int nBodies = m_system->getNumBodies();

glPushAttrib(GL_TEXTURE_BIT);
glUseProgram(program);
texLoc = glGetUniformLocationARB(program, "posTex");
glUniform1iARB(texLoc, 0);
texLoc = glGetUniformLocationARB(program, "quatTex");
glUniform1iARB(texLoc, 1);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture2);

glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_NORMAL_ARRAY );
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,indexV BO);
glBindBufferARB(GL_ARRAY_BUFFER, vertexVBO);
glVertexPointer(3,GL_FLOAT,sizeof(Vertex),0);
glNormalPointer(GL_FLOAT,sizeof(Vertex),(void*)(si zeof(Vertex)/2));

glDrawElementsInstancedEXT(GL_QUADS, 150*4, GL_UNSIGNED_INT, 0, nBodies);

glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_NORMAL_ARRAY );
glUseProgram(0);

glBindTexture(GL_TEXTURE_2D, 0);

glPopAttrib();
}

#version 120
#extension GL_EXT_gpu_shader4 : enable
uniform sampler2D posTex;
uniform sampler2D quatTex;
const float PI = 3.1415926;
varying vec3 norm;
varying vec4 shadowTexcoord;
varying vec4 diffuse;
void main()
{
vec4 centerOfMass;
vec2 texCrd;
texCrd = vec2((gl_InstanceID/128)/128.0, (gl_InstanceID-gl_InstanceID/128*128)/128.0);
centerOfMass = texture2D(posTex, texCrd);
vec4 quat = texture2D(quatTex, texCrd);
vec4 inquat = vec4(-quat.xyz, quat.w);
vec4 inVec = gl_Vertex;
if(mod(gl_InstanceID, 2) == 1) inVec = 1.2*inVec;
inVec.w = 0.0;
vec4 ans;
ans.xyz = cross(quat.xyz, inVec.xyz);
ans.xyz = ans.xyz + quat.w*inVec.xyz + inVec.w*quat.xyz;
ans.w = quat.w*inVec.w - dot(quat.xyz, inVec.xyz);
vec4 rotatedPos;
rotatedPos.xyz = cross(ans.xyz, inquat.xyz);
rotatedPos.xyz = rotatedPos.xyz + ans.w*inquat.xyz + inquat.w*ans.xyz;
rotatedPos.w = 1.0;
rotatedPos.xyz = rotatedPos.xyz + centerOfMass.xyz;
gl_Position = gl_ModelViewProjectionMatrix * rotatedPos;

vec4 Vpos = gl_ModelViewMatrix * rotatedPos;
vec3 pos = Vpos.xyz / Vpos.w;

vec3 lightdir = normalize( vec3(gl_LightSource[0].position) - pos);
norm = normalize(gl_NormalMatrix * gl_Normal);

float NdotL = max(0.0, dot(lightdir, norm));
diffuse = gl_LightSource[0].diffuse * NdotL;

vec4 texcoord = gl_TextureMatrix[7] * gl_ModelViewMatrix * rotatedPos;

shadowTexcoord = texcoord / texcoord.w;
shadowTexcoord = 0.5 * shadowTexcoord +0.5;

gl_FrontColor = gl_Color;
//gl_Position = ftransform();
//===== LIGHTING =====
vec3 lightDir = normalize( vec3(gl_LightSource[0].position) - pos);
vec4 outColor;
outColor.xyz = 0.2 + 0.8*vec3(pow(sin(texCrd.x*70.0),2), pow(sin(texCrd.x*30.0),2), pow(sin(texCrd.x*40.0),2));
vec4 inNormal;
inNormal.xyz = gl_Normal;
inNormal.w = 0.0;
ans.xyz = cross(quat.xyz, inNormal.xyz);
ans.xyz = ans.xyz + quat.w*inNormal.xyz + inNormal.w*quat.xyz;
ans.w = quat.w*inNormal.w - dot(quat.xyz, inNormal.xyz);
vec4 ans1;
ans1.xyz = cross(ans.xyz, inquat.xyz);
ans1.xyz = ans1.xyz + ans.w*inquat.xyz + inquat.w*ans.xyz;
inNormal.xyz = ans1.xyz;
inNormal.xyz = gl_NormalMatrix * inNormal.xyz;
gl_FrontColor = outColor * dot(normalize(inNormal.xyz), normalize(lightDir))*2;
}

And the floordisplaylist is like this:
// the floor
float x, y;
float d = 1.0;
float s = 60.0f;
floor_display_list = glGenLists(1);
glNewList(floor_display_list,GL_COMPILE);
glColor3f(0.35, 0.35, 0.0);
/* Draw ground. */
if (useTextures) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,floorTex);
}
glColor3f(1,1,0);
glNormal3f(0,1,0);
glPushMatrix();
glTranslatef(0.0, 1.0, 0.0);

/* Tesselate floor so lighting looks reasonable. */
for (x=-s; x<s; x+=d) {
glBegin(GL_QUAD_STRIP);
for (y=-s; y<s; y+=d) {
glTexCoord2f(x+1, y);
glVertex3f(x+1, -1, y);
glTexCoord2f(x, y);
glVertex3f(x, -1, y);
}
glEnd();
}
glPopMatrix();

if (useTextures) {
glBindTexture(GL_TEXTURE_2D,0);
glDisable(GL_TEXTURE_2D);
}
glEndList();

BionicBytes
10-06-2010, 04:18 AM
I can't see the definition of the 'floorprogram' shader you refer to.
The display list you build to render the floor does not bind the shadow map texture nor enable the shadow texture coord[7] matrix and hence what I said before...you are not applying the shadowmap to the floor.
On a separate note, what does all that math do in the shaders....


ans.xyz = cross(quat.xyz, inNormal.xyz);
ans.xyz = ans.xyz + quat.w*inNormal.xyz + inNormal.w*quat.xyz;
ans.w = quat.w*inNormal.w - dot(quat.xyz, inNormal.xyz);
vec4 ans1;
ans1.xyz = cross(ans.xyz, inquat.xyz);
ans1.xyz = ans1.xyz + ans.w*inquat.xyz + inquat.w*ans.xyz;

Liufu
10-06-2010, 04:23 AM
Hello,
floorprogram shader is shadowmap2.vert and shadowmap2.frag.

The code snippet you pointed is for computing the light effect for each torus.

void Demo::_initGL()
{
setshader("shadowmap1.vert","shadowmap1.frag");
setshaderFloor("shadowmap2.vert","shadowmap2.frag");
}

void Demo::setshader(const GLchar* vertfile, const GLchar* fragfile)
{
char *vs = NULL,*fs = NULL;
const char *use_vs = NULL, *use_fs = NULL;

//glewInit();
shaderprogram = glCreateProgram();

GLuint vertshader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragshader = glCreateShader(GL_FRAGMENT_SHADER);

vs = textFileRead(vertfile);
fs = textFileRead(fragfile);

use_vs = vs;
use_fs = fs;

glShaderSource(vertshader, 1, &amp;use_vs, NULL);
glShaderSource(fragshader, 1, &amp;use_fs, NULL);

delete []vs;
delete []fs;

glCompileShader(vertshader);
glCompileShader(fragshader);


glAttachShader(shaderprogram, vertshader);
glAttachShader(shaderprogram, fragshader);

glLinkProgram(shaderprogram);

}

void Demo::setshaderFloor(const GLchar* vertfile, const GLchar* fragfile)
{
char *vs = NULL,*fs = NULL;
const char *use_vs = NULL, *use_fs = NULL;

//glewInit();
floorprogram = glCreateProgram();

GLuint vertshader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragshader = glCreateShader(GL_FRAGMENT_SHADER);

vs = textFileRead(vertfile);
fs = textFileRead(fragfile);

use_vs = vs;
use_fs = fs;

glShaderSource(vertshader, 1, &amp;use_vs, NULL);
glShaderSource(fragshader, 1, &amp;use_fs, NULL);

delete []vs;
delete []fs;

glCompileShader(vertshader);
glCompileShader(fragshader);


glAttachShader(floorprogram, vertshader);
glAttachShader(floorprogram, fragshader);

glLinkProgram(floorprogram);

}

BionicBytes
10-06-2010, 05:16 AM
And the floordisplaylist is like this:
// the floor
float x, y;
float d = 1.0;
float s = 60.0f;
floor_display_list = glGenLists(1);
glNewList(floor_display_list,GL_COMPILE);
glColor3f(0.35, 0.35, 0.0);
/* Draw ground. */
if (useTextures) {
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,floorTex);


Thats your mistake right there. The display list is rebinding a different texture to Tex Unit#0 which you previously bound as the shadowmap.

Liufu
10-06-2010, 05:25 AM
I remove the lines:
if(useTextures){
.......................................
}
But, I still found no shadow on floor. I attached the snapshot.
Thanks!

BionicBytes
10-06-2010, 06:00 AM
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, shadowmapid);

You do this in quite a few places...enable the state with out first selecting the texture unit.
Does not matter so much for shaders...but this is wrong per say.


void Demo::renderShadow()



glUseProgram(floorprogram);
texLoc = glGetUniformLocationARB(shaderprogram, "shadowmap");
glUniform1iARB(texLoc, 0);

Have you checked that texloc is > -1
If not the uniform was not found in your shader.

...in any case that's the wrong shader name. Your supposed to be using "floorprogram" not "shaderprogram"

Liufu
10-06-2010, 06:21 AM
Thanks so much!
It should be texLoc = glGetUniformLocationARB(floorprogram, "shadowmap");
And, I add this sentence:
if(texLoc == -1) printf("shadowmap cannot be found in shader!!!");
You are right. I found shadowmp cannot be found in floorprogram.
But, I have already bound this shaderprogram. I don't know why.
Which way is correct?
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, shadowmapid);

The way above, is it correct?

BionicBytes
10-06-2010, 07:43 AM
Yes, select the texture unit, then enable and bind the texture. For shaders though, you don't have to enable the texture target, eg glEnable (Texture_2D) is not required.

Liufu
10-06-2010, 08:50 AM
I still don't understand why shaderprogram can not find uniform sampler2DShadow shadowmap in shadowmap2.frag.
I already detached shaderprogram(glUseProgram(0)), and attached floorprogram(glUseProgram(floorprogram)).
Thanks!

BionicBytes
10-06-2010, 09:08 AM
Perhaps the sampler is being optimised out. I don't know why, but that's the possibility.
Try messing witht he shader to force the depth output to the screen and see if the getUniformLocation return a proper index location value.

Liufu
10-06-2010, 07:26 PM
Thanks!
I found the reason.
I need add
#version 120
#extension GL_EXT_gpu_shader4 : enable
at the top of floorshadow vertex shader file.

Although, floor shader works, the floor disappeared. My code seems too messy, many problems:)

Liufu
10-07-2010, 03:16 AM
Finally, I implemented shadow mapping on floor.
I should not uncomment gl_Position = ftransform().
But, I still found shadow formed by many isolated dark points.
I don't know this effect is correct or not.
I attached the image.