繁体   English   中英

从FrameBuffer渲染DepthTexture

[英]Render the DepthTexture from a FrameBuffer

您好,我正在尝试使用C ++在openGL中实现阴影。 我创建了一个FrameBuffer和一个DepthTexture。 我将每个m rendering my entities to the FrameBuffer. For now I每一帧m rendering my entities to the FrameBuffer. For now I m rendering my entities to the FrameBuffer. For now I只是像在其他GUI上一样在屏幕上显示纹理,但是纹理是完全白色的,当我移动相机时它并没有改变。 希望您能帮助我找到问题所在。

我的代码:



    //Creating FrameBuffer + Texture
    glGenFramebuffers(1, &depthMapFBO);
    glGenTextures(1, &depthMap);
    glBindTexture(GL_TEXTURE_2D, depthMap);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, m_width, m_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_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
    glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthMap, 0); 
    glDrawBuffer(GL_NONE);
    glReadBuffer(GL_NONE);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

在渲染器中:



    glm::mat4 lightSpaceMatrix = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, Camera::ZNEAR, Camera::ZFAR) * glm::lookAt(m_position, m_position + m_forward, m_up);
    m_shader.Bind();
    m_shader.loadMat4("lightSpaceMatrix", lightSpaceMatrix);

    //Bind the FrameBuffer
    glViewport(0, 0, m_width, m_height);
    glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO);
    glClear(GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

    //Render all Entitys
    for (auto entity : m_entitys) {
        m_shader.loadMat4("model", entity->getTransform()->getModel());
        entity->getTexturedModel()->getMesh()->Draw();
    }

    //Unbind the FrameBuffer
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glViewport(0, 0, Setting::width, Setting::height);
    m_shader.unbind();

我的顶点着色器:



    #version 430
    in layout(location=0) vec3 position;
    uniform mat4 lightSpaceMatrix;
    uniform mat4 model;

    void main()
    {
        gl_Position = lightSpaceMatrix * model * vec4(position, 1.0f);
    }

我的片段着色器:



    #version 430
    void main()
    {
        gl_FragColor = vec4(1);
    }

使用我的法线投影矩阵代替正交矩阵后,它可以正常工作。 因此,我进行了一些实验,并在创建正交矩阵时将近平面和远平面设置为0。 我修复了它,现在可以工作了。 谢谢您的帮助。

暂无
暂无

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

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