i wrote a simple demo of shadow map,but it does not work well, the correct shadow does not show and it seems like the light does not work because the whole scene is dark, when i change the window from the initial size to the full size,the incorrect shadow disappear and the whole scene become darker,i can not figure it out.
void setlight()
{
GLfloat light_diff = {0.8f, 0.8f, 0.8f, 1.0f };
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diff); glLightfv(GL_LIGHT0, GL_POSITION, Lightpos);////光源坐标,初始Y=5
glPushMatrix();
glTranslatef(Lightpos[0],Lightpos[1],Lightpos[2]);
glColor3f(0,1,0);
auxSolidSphere(0.02f);
glPopMatrix();
glPushMatrix();
glColor4f(0.0f, 1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex3f(Lightpos[0], Lightpos[1], Lightpos[2]);
glVertex3f(LookAt[0],LookAt[1],LookAt[2]);
glEnd();
glPopMatrix();
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
VertexShader:
uniform vec3 LightPosition;
const float As=1.0/1.5;
const float Ds=1.0/3.0;
varying float Accessibility;
varying vec4 ShadowCoord;
varying vec4 Color;
void main()
{
vec4 ecPosition=gl_ModelViewMatrix*gl_Vertex;
vec3 ecPosition3=(vec3(ecPosition))/ecPosition.w;
vec3 VP=LightPosition-ecPosition3;
VP=normalize(VP);
vec3 normal=normalize(gl_NormalMatrix*gl_Normal);
float diffuse=max(0.0,dot(normal,VP));float costheta=dot(normal,VP);
float Accesibility=costheta*0.5+0.5;float scale=min(1.0,AccessibilityAs+diffuseDs);
vec4 texCoord=gl_TextureMatrix[0] * gl_ModelViewMatrix * gl_Vertex;
ShadowCoord=texCoord;
Color =vec4(scale*gl_Color.xyz,gl_Color.w);
gl_Position=ftransform();
}
FragmentShader:
uniform sampler2DShadow ShadowMap;
varying vec4 ShadowCoord;
varying vec4 Color;
void main()
{
float shadeFactor=shadow2DProj(ShadowMap,ShadowCoord);
shadeFactor=shadeFactor0.25+0.75;
gl_FragColor=vec4(shadeFactorColor.xyz,Color.w);
}
void GenShadowMapTexture()
{
glGenTextures(1, &shadowMapTexture);
glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, VB_WIDTH,VB_HEIGHT, 0,
GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(Light_ProjectionMatrix.mt);
glMatrixMode(GL_MODELVIEW); glLoadMatrixf( Light_ViewMatrix.mt);
glViewport(0, 0,VB_WIDTH, VB_HEIGHT);
glCullFace(GL_FRONT);
glShadeModel(GL_FLAT);
glColorMask(0, 0, 0, 0);
glPolygonOffset(8.0f, 4.0f);
glEnable(GL_POLYGON_OFFSET_FILL);RenderObject();
glBindTexture(GL_TEXTURE_2D, shadowMapTexture); glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0,1024, 1024);
glCullFace(GL_BACK);
glShadeModel(GL_SMOOTH);
glColorMask(1, 1, 1, 1);
glDisable(GL_POLYGON_OFFSET_FILL);
}
void RenderObject()
{
glColor3f(0.0f, 0.0f, 1.0f);
glPushMatrix();
glTranslatef(-2.0,-1.0,-2.0);
glutSolidTeapot(0.51);
glTranslatef(0,-1,0);
glScalef(3,0,3);
glutSolidCube(1);
glPopMatrix();
}
void CastShadowMap()
{
shader.bind();
shader.SendUniform(“ShadowMap”,0);
shader.SendUniform(“LightPosition”,Lightpos[0],Lightpos[1],Lightpos[2]);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, shadowMapTexture);
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); //Note:MCtoLightMatrix=Light_ProjectionMatrix.mtLight_ViewMatrix.mtCamera_ViewInverse*gl_Vertex
glLoadMatrixf(Light_ProjectionMatrix.mt);
glMultMatrixf(Light_ViewMatrix.mt);
glMultMatrixf(Camera_ViewInverse.mt);
glMatrixMode(GL_MODELVIEW);
RenderObject();
glDisable(GL_TEXTURE_2D);
shader.unbind();
}
int DrawGLScene(GLvoid) // Here’s Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();glPushMatrix(); glLoadIdentity(); gluPerspective(45.0f, (GLfloat)VB_WIDTH/(GLfloat)VB_HEIGHT, 2.0f, 60.0f); glGetFloatv(GL_MODELVIEW_MATRIX, Light_ProjectionMatrix.mt); glLoadIdentity(); gluLookAt(Lightpos[0],Lightpos[1],Lightpos[2],0,0, 0, 0.0, 1.0, 0.0); glGetFloatv(GL_MODELVIEW_MATRIX, Light_ViewMatrix.mt); glPopMatrix(); GenShadowMapTexture(); ReSizeGLScene(VB_WIDTH, VB_HEIGHT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); setlight(); CastShadowMap(); SwapBuffers(hDC); return 1;
}