gpt4 book ai didi

c++ - 帧缓冲区纹理显示为白色(片段着色器不影响它)

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:44 25 4
gpt4 key购买 nike

所以我有一个 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/

最佳答案

在查看我 Unresolved 旧问题后,只是回答我自己的问题。如果有人想知道,我的问题是我将两个教程合二为一,其中一个对平截头体有不同的远近值。帧缓冲区是黑色的,因为对象离相机太远。

关于c++ - 帧缓冲区纹理显示为白色(片段着色器不影响它),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52286843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com