gpt4 book ai didi

opengl - 从帧缓冲纹理渲染深度

转载 作者:行者123 更新时间:2023-12-01 10:48:31 27 4
gpt4 key购买 nike

我一直在尝试将 SSAO(基于此处的教程:http://john-chapman-graphics.blogspot.co.nz/2013/01/ssao-tutorial.html)添加到我的一个项目中,但我一直在正确渲染深度方面陷入困境。

我创建了一个帧缓冲纹理来绘制每个帧的深度。当我将它绘制到四边形时,纹理本身看起来是正确的,但我不知道如何正确地将它应用于整个场景。

这是常规场景的样子:

enter image description here

这是应用的深度纹理:
enter image description here

目前在我的片段着色器中,我只是使用我的常规纹理坐标 (TexCoord0) 来绘制深度纹理。我猜我需要改变它,但我不知道将它改为什么。
这是相关的片段着色器代码:

#version 330 core

in vec2 TexCoord0;

smooth in vec3 vNormal;
smooth in vec3 vWorldPos;
in mat4 ProjectionMatrix;

uniform sampler2D uDepthTex;

layout(location = 0) out vec4 FragColor;

void main()
{
FragColor = texture(uDepthTex, TexCoord0);
}

我对如何解决这个问题感到有些茫然。我在网上阅读了很多示例代码,它们似乎使用常规纹理坐标来绘制深度。

编辑:
这是我的帧缓冲区设置代码:
glGenFramebuffers(1, &ssaoFramebufferID);
glBindFramebuffer(GL_FRAMEBUFFER, ssaoFramebufferID);

glGenTextures(1, &ssaoDepthTextureID);
glBindTexture(GL_TEXTURE_2D, ssaoDepthTextureID);

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);

glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, ssaoDepthTextureID, 0);

glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);

glBindFramebuffer(GL_FRAMEBUFFER, 0);

以及我在常规绘制之前调用每一帧的深度传递代码:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, ssaoFramebufferID);
glViewport(0, 0, 1024, 1024);

depthShader.Bind();
depthShader.SetUniformMatrixfv("depthMVP", camera.GetViewMatrix() * Matrix4::Perspective(60, aspectRatio, 0.1f, 10.0f));

DrawScene(true);

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, width, height);

我的深度着色器,顶点:
#version 330 core

layout(location = 0) in vec3 Position;

uniform mat4 depthMVP;

void main() {
gl_Position = depthMVP * vec4(Position, 1.0);
}

分段:
#version 330 core

layout(location = 0) out float fragmentdepth;

void main() {
fragmentdepth = gl_FragCoord.z;
}

编辑2:
当前所在的 glClear 调用给了我一个完全黑色的纹理。把它放在第一个 glViewport 调用下面给出了我在上面显示的深度图像:
glBindTexture(GL_TEXTURE_2D, 0);
glBindFramebuffer(GL_FRAMEBUFFER, ssaoFramebufferID);
glViewport(0, 0, 1024, 1024);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

最佳答案

由于评论中的一些帮助,我设法让它工作:
enter image description here

我的代码现在如下,片段着色器:

#version 330 core

in vec2 TexCoord0;

smooth in vec3 vNormal;
smooth in vec3 vWorldPos;
in mat4 ProjectionMatrix;

uniform sampler2D uDepthTex;
uniform float uWidth;
uniform float uHeight;

layout(location = 0) out vec4 FragColor;

void main()
{
vec2 res = gl_FragCoord.xy / vec2(uWidth, uHeight);
FragColor = texture(uDepthTex, res);
}

帧缓冲区创建:
glGenFramebuffers(1, &ssaoFramebufferID);
glBindFramebuffer(GL_FRAMEBUFFER, ssaoFramebufferID);

glGenTextures(1, &ssaoDepthTextureID);
glBindTexture(GL_TEXTURE_2D, ssaoDepthTextureID);

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 1024, 768, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);

深度绘制代码:
glBindFramebuffer(GL_FRAMEBUFFER, ssaoFramebufferID);
glViewport(0, 0, 1024, 768);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

depthShader.Bind();

glDisable(GL_ALPHA_TEST);

depthShader.SetUniformMatrixfv("depthMVP", Matrix4::Perspective(60, aspectRatio, 0.1f, 10.0f) * camera.GetViewMatrix());

DrawScene();

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, width, height);
glEnable(GL_ALPHA_TEST);

关于opengl - 从帧缓冲纹理渲染深度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23179081/

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