Opengl 使用GLSL着色器的阴影贴图的奇怪行为
我集成了基本的阴影贴图。然而,我注意到一个奇怪的行为与渲染。总之,阴影映射技术需要两个步骤。第一个从灯光角度渲染场景(填充深度纹理),第二个从摄影机角度渲染场景。这里是C++代码的第一步:Opengl 使用GLSL着色器的阴影贴图的奇怪行为,opengl,glsl,shader,vertex-shader,shadow-mapping,Opengl,Glsl,Shader,Vertex Shader,Shadow Mapping,我集成了基本的阴影贴图。然而,我注意到一个奇怪的行为与渲染。总之,阴影映射技术需要两个步骤。第一个从灯光角度渲染场景(填充深度纹理),第二个从摄影机角度渲染场景。这里是C++代码的第一步: void Basic::GLSLRenderSystem::renderSceneFromLight(void) { SceneNodeList::iterator nodeListIt = this->m_pScene->getRootNode()->begin(); f
void Basic::GLSLRenderSystem::renderSceneFromLight(void)
{
SceneNodeList::iterator nodeListIt = this->m_pScene->getRootNode()->begin();
for (; nodeListIt != this->m_pScene->getRootNode()->end(); ++nodeListIt)
{
EntityList::const_iterator Ite = (*nodeListIt)->getListEntity().begin();
(*nodeListIt)->beginTransformations();
(*nodeListIt)->applyTransformations();
for (; Ite != (*nodeListIt)->getListEntity().end(); ++Ite)
{
//Send matrix
glm::mat4 modelMatrix = (*nodeListIt)->getModelMatrix();
glm::mat4 modelViewMatrix = this->m_ViewMatrix * modelMatrix;
glm::mat4 MVPMatrix = this->m_ProjectionMatrix * modelViewMatrix;
this->m_pShadowProgram->setUniform("MVP", MVPMatrix);
(*Ite)->getMesh()->getVertices()->lock();
//Send vertice positions
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), OFFSET_BUFFER(0));
(*Ite)->getMesh()->getVertices()->unlock();
//Draw primitive
if ((*Ite)->getMesh()->getVertices()->getCount() == 2)
glDrawArrays(GL_LINES, 0, (*Ite)->getMesh()->getVertices()->getSize());
else if ((*Ite)->getMesh()->getVertices()->getCount() == 3)
glDrawArrays(GL_TRIANGLES, 0, (*Ite)->getMesh()->getVertices()->getSize());
else if ((*Ite)->getMesh()->getVertices()->getCount() == 4)
glDrawArrays(GL_QUADS, 0, (*Ite)->getMesh()->getVertices()->getSize());
}
(*nodeListIt)->endTransformations();
}
}
以下是输出:
地板上有埃克尼。但是现在,如果我擦除对应于将顶点发送到程序着色器的部分下面的代码:
void Basic::GLSLRenderSystem::renderSceneFromLight(void)
{
SceneNodeList::iterator nodeListIt = this->m_pScene->getRootNode()->begin();
for (; nodeListIt != this->m_pScene->getRootNode()->end(); ++nodeListIt)
{
EntityList::const_iterator Ite = (*nodeListIt)->getListEntity().begin();
(*nodeListIt)->beginTransformations();
(*nodeListIt)->applyTransformations();
for (; Ite != (*nodeListIt)->getListEntity().end(); ++Ite)
{
//Send matrix
glm::mat4 modelMatrix = (*nodeListIt)->getModelMatrix();
glm::mat4 modelViewMatrix = this->m_ViewMatrix * modelMatrix;
glm::mat4 MVPMatrix = this->m_ProjectionMatrix * modelViewMatrix;
this->m_pShadowProgram->setUniform("MVP", MVPMatrix);
//Draw primitive
if ((*Ite)->getMesh()->getVertices()->getCount() == 2)
glDrawArrays(GL_LINES, 0, (*Ite)->getMesh()->getVertices()->getSize());
else if ((*Ite)->getMesh()->getVertices()->getCount() == 3)
glDrawArrays(GL_TRIANGLES, 0, (*Ite)->getMesh()->getVertices()->getSize());
else if ((*Ite)->getMesh()->getVertices()->getCount() == 4)
glDrawArrays(GL_QUADS, 0, (*Ite)->getMesh()->getVertices()->getSize());
}
(*nodeListIt)->endTransformations();
}
}
我有以下输出(无acnee):
以下是顶点着色器:
#version 400
layout (location = 0) in vec3 VertexPosition;
uniform mat4 MVP;
void main(void)
{
gl_Position = MVP * vec4(VertexPosition, 1.0f);
}
如您所见,我在灯光(视图)位置变换顶点。因此,在我的C++代码中,如果我不发送(第二种情况)的顶点,我想知道它是如何工作的!(我刚刚发送了轻型MVP矩阵!)最令人惊讶的是没有更多的acnee!你如何解释这种行为?我是否像第一种情况那样将顶点发送到着色器(并找到擦除地板上顶点的解决方案)?首先,您可能认为设置顶点属性指针是将顶点发送到GLSL程序的原因,但事实并非如此。顶点数组状态是持久的,除非您在别处更改了attrib指针状态,否则它将继续使用旧状态进行绘制(可能是在第一段代码中设置attrib指针是多余的)。在两个截图中,你如何变换场景还有一些根本不同的地方,因为“1”是顺时针旋转90度的。谢谢你的回答。场景围绕Y轴永久旋转是正常的。