0

我已经集成了基本的阴影贴图。但是我注意到渲染有一个奇怪的行为。总而言之,阴影映射技术需要 2 个步骤。第一个从光的角度渲染场景(填充深度纹理),第二个从相机的角度渲染。这是第一步的 C++ 代码:

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();
    }
}

我有以下输出(没有痤疮):

在此处输入图像描述

这是顶点着色器:

#version 400

layout (location = 0) in vec3 VertexPosition;

uniform mat4 MVP;

void main(void)
{
    gl_Position = MVP * vec4(VertexPosition, 1.0f);
}

如您所见,我将顶点转换为光(视图)位置。因此,在我的 C++ 代码中,如果我不发送(在第二种情况下)顶点,我想知道它是如何工作的!(我只是发轻MVP矩阵!)最神奇的是没有粉刺了!你如何解释这种行为?我是否像第一种情况一样将顶点发送到着色器(并找到消除地板上痤疮的解决方案)?

4

0 回答 0