我正在使用定向光生成一个简单的场景。我的目标是通过阴影映射创建场景的阴影。我一直在寻找有关此主题的帮助,但无济于事。据我所知,我的代码应该与我在多个教程中看到的相同。
场景创建完美,没有阴影。我怀疑这是在我的程序的第一次渲染过程中生成(或不生成)的阴影贴图纹理的问题。当我将渲染纹理绘制到四边形时,四边形全是白色的。
我的缓冲区/纹理初始化代码如下所示:
void Renderer :: initBuffers()
{
glGenTextures(1, &SMDepthTexture);
glBindTexture(GL_TEXTURE_2D, SMDepthTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE );
glTexParameteri( GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE );
glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glBindTexture(GL_TEXTURE_2D, 0);
/*
glGenRenderbuffers(1, &SMRenderBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, SMRenderBuffer);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, SMRenderBuffer);
*/
glGenFramebuffers(1, &SMFrameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, SMFrameBuffer);
glDrawBuffer(GL_NONE); // No color buffer is drawn to.
glReadBuffer(GL_NONE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, SMDepthTexture, 0);
// Always check that our framebuffer is ok
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
std :: cerr << "Frame Buffer Status: " << glCheckFramebufferStatusEXT(GL_FRAMEBUFFER) <<"\n";
exit(0);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
我的显示功能。( pass = 1, if(pass == 1) 的东西似乎有点多余,但它是用于调试的)。
void Renderer :: displayCallback()
{
pass = 1;
if(pass == 1)
{
glMatrixMode(GL_PROJECTION);
gluPerspective(FOV,aspect,zNear, zFar);
glDisable(GL_TEXTURE_2D);
glBindFramebuffer(GL_FRAMEBUFFER, Renderer :: SMFrameBuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, Renderer :: SMDepthTexture, 0);
glUseProgram(0);
glViewport(0,0,width,height);
glClear(GL_DEPTH_BUFFER_BIT);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
display();
}
pass = 2;
if(pass == 2)
{
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, 0, 0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0,0,width,height);
glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glUseProgram(Renderer :: shader_program_pass2);
Renderer :: uniforms2();
Renderer :: SMDepthTexture_handle = glGetUniformLocation(Renderer :: shader_program_pass2, "shadowMap");
glEnable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE7);
glBindTexture(GL_TEXTURE_2D, Renderer :: SMDepthTexture);
glUniform1i(Renderer :: SMDepthTexture_handle, 7);
display();
}
drawMap();
glutSwapBuffers();
}
最后,我尝试使用以下函数将纹理绘制到屏幕填充四边形:
void Renderer :: drawMap()
{
Renderer :: camera_modelview = Globals::camera.getInverseCM();
Renderer :: light_modelview = Renderer :: light_cam.getInverseCM();
if(pass == 1)
{
glLoadMatrixd(Renderer :: light_cam.getInverseCMGLPointer());
//glUniformMatrix4fv(Renderer :: camera_modelview_handle_pass1, 1, false, Renderer :: camera_modelview.getGLfloatPointer());
//glUniformMatrix4fv(Renderer :: light_modelview_handle_pass1, 1, false, Renderer :: light_modelview.getGLfloatPointer());
}
else if(pass == 2)
{
glUniformMatrix4fv(Renderer :: camera_modelview_handle_pass2, 1, false, Renderer :: camera_modelview.getGLfloatPointer());
glUniformMatrix4fv(Renderer :: light_modelview_handle_pass2, 1, false, Renderer :: light_modelview.getGLfloatPointer());
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glUseProgram(0);
glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-width/2,width/2,-height/2,height/2,1,20);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor4f(1,1,1,1);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,Renderer :: SMDepthTexture);
glEnable(GL_TEXTURE_2D);
glTranslated(0,0,-1);
glBegin(GL_QUADS);
glTexCoord2d(0,0);glVertex3f(-width/2,-height/2,0);
glTexCoord2d(1,0);glVertex3f(width/2,-height/2,0);
glTexCoord2d(1,1);glVertex3f(width/2,height/2,0);
glTexCoord2d(0,1);glVertex3f(-width/2,height/2,0);
glEnd();
glDisable(GL_TEXTURE_2D);
glLoadIdentity();
glEnable(GL_LIGHTING);
}
我知道很多人遇到的一个问题是同时将相同的纹理绑定到帧缓冲区和活动纹理。我很确定我已经避免了这种情况。
如果需要更多细节,我将非常乐意发布更多代码(包括着色器)。感谢您的帮助,因此在此先感谢您!