简体   繁体   English

渲染阴影的 OpenGL 正射投影问题

[英]OpenGL ortho projection issue with rendering shadows

I've been trying to implement shadow mapping into my OpenGL engine in SFML 2.2 , and they don't seem to be rendering right.我一直在尝试在SFML 2.2中将阴影映射实现到我的OpenGL引擎中,但它们似乎没有正确渲染。 I believe I narrowed down the issue to the ortho projection used to calculate the shadows.我相信我将问题缩小到用于计算阴影的正射投影。

/* before the main loop, creating the depth buffer for shadow mapping */
glm::vec3 lightPos(glm::vec3(-45.f, 45.f, -40.f));

const GLuint SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024;
GLuint depthMapFBO;
glGenFramebuffers(1, &depthMapFBO);

GLuint depthMap;
glGenTextures(1, &depthMap);
glBindTexture(GL_TEXTURE_2D, depthMap);

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 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_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
GLfloat borderColor[] = { 1.0, 1.0, 1.0, 1.0 };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor);

glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMap, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

/* in the main loop, sending information to the appropiate shaders and setting viewports */
glm::mat4 lightProjection, lightView;
glm::mat4 lightSpaceMatrix;
GLfloat near_plane = 1.f, far_plane = 300.f;
lightProjection = glm::ortho(-10.f, 10.f, -10.f, 10.f, near_plane, far_plane);
lightView       = glm::lookAt(lightPos, glm::vec3(0.0f), glm::vec3(0.0, 1.0, 0.0));
lightSpaceMatrix = lightProjection * lightView;

glUseProgram(simpleDepthShader);
glUniformMatrix4fv(glGetUniformLocation(simpleDepthShader, "lightSpaceMat"), 1, GL_FALSE, glm::value_ptr(lightSpaceMatrix));
glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
    glClear(GL_DEPTH_BUFFER_BIT);
    RenderScene();
glBindFramebuffer(GL_FRAMEBUFFER, 0);

// reset viewport, and display the scene as normal
glViewport(0, 0, window.getSize().x, window.getSize().y);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUniform3fv(glGetUniformLocation(lightingShader, "lightPos_shade"), 1, &lightPos[0]);
glUniform3fv(glGetUniformLocation(lightingShader, "viewPos"), 1, &getPos()[0]);
glUniformMatrix4fv(glGetUniformLocation(lightingShader, "lightSpaceMat"), 1, GL_FALSE, glm::value_ptr(lightSpaceMatrix));
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, depthMap);

RenderScene();

I've applied the view and projection matrix to the camera to see what the light sees, and this was the result (along with the view from the depth buffer) .我已将视图和投影矩阵应用于相机以查看光看到的内容,这就是结果(以及来自深度缓冲区的视图)

Here's what the scene looks like with the shadows (which the shadow for the stall doesn't even cast onto itself for some reason).这是带有阴影的场景(由于某种原因,摊位的阴影甚至没有投射到自身上)。

It seems that your shader doesn't fetch your shadow pixels correctly.您的着色器似乎没有正确获取您的阴影像素。

When you transform your vertices with your light matrix they are in the [-1, 1] range, but texture sampling is in the range [0, 1].当您使用光矩阵变换顶点时,它们在 [-1, 1] 范围内,但纹理采样在 [0, 1] 范围内。

http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/ http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM