帧缓冲纹理呈现白色(片段着色器不影响它)

时间:2018-09-12 02:56:26

标签: c++ glsl textures framebuffer

所以我有一个fbo,试图从光源的角度输出深度纹理。不幸的是,即使我在片段着色器中将黑色硬编码为黑色,深度纹理也会变成纯白色。

这是我的帧缓冲区初始化

//Render frame to a texture
m_FrameBuffer = 0;
glGenFramebuffers(1, &m_FrameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBuffer); // once frame buffer is bound, must draw to it or black screen
glEnable(GL_TEXTURE_2D);

glGenTextures(1, &m_depthTexture);
glBindTexture(GL_TEXTURE_2D, m_depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);

glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, m_depthTexture, 0);
    glDrawBuffer(GL_NONE);
//Always check that our framebuffer is ok
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    printf("frame buffer binding error");

glBindTexture(GL_TEXTURE_2D, 0);

这是我渲染的帧缓冲区

glBindFramebuffer(GL_FRAMEBUFFER, m_FrameBuffer);
glViewport(0, 0, 1024, 1024);

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glBindVertexArray(m_VAO);

glBindBuffer(GL_ARRAY_BUFFER, m_VBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

glDrawArrays(GL_TRIANGLES, 0, vertices.size());
glDisableVertexAttribArray(0);
glBindVertexArray(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

这是我的片段着色器

#version 330 core
// Ouput data
layout(location = 0) out float fragmentdepth;
void main()
{
//fragmentdepth = gl_FragCoord.z;
fragmentdepth = 0;
}

这是我的主循环

while (!window.closed())
{
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glDepthFunc(GL_LESS);
    window.clear();

    CameraControls();
    depth.enable();

    //depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;
    depth.setUniformMat4("projection", projection);
    depth.setUniformMat4("view", camera);
    depth.setUniformMat4("model", shape1->modelMatrix);     

    shape1->RenderToTexture();
    depth.disable();
    window.clear();
    basic.enable();
    basic.setUniformMat4("proj", projection);
    //basic.setUniform3f("light_Pos", lightPos);
    basic.setUniformMat4("view", camera);
    basic.setUniformMat4("model", shape1->modelMatrix);
    basic.setUniformMat4("DepthBiasMVP", biasMatrix); 

    //NEED TO CHANGE THIS TO BIAS MATRIX X DEPTHMVP
    //MVP = projection * camera * shape1->modelMatrix;
    //basic.setUniformMat4("MVP", MVP);
    shape1->Render(basic);
    basic.disable();
    window.update();
}

我正在关注本教程http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/

1 个答案:

答案 0 :(得分:0)

看完我的旧问题后再回答我自己的问题。如果有人想知道,我的问题是我将两个教程融为一体,其中一个教程的平截头体值具有不同的近和远值。帧缓冲区是黑色的,因为对象离摄像机太远。