gpt4 book ai didi

java - 渲染纹理深度

转载 作者:搜寻专家 更新时间:2023-11-01 03:22:10 25 4
gpt4 key购买 nike

我无法正确渲染我的深度。没有抛出任何错误,glCheckFramebufferStatus 表示它也已完成。

下面是代码,屏幕始终显示为白色。深度值不是 1,但非常非常接近:

编辑:

所以我尝试在深度片段着色器中线性化深度,然后将其直接绘制到屏幕上以确保值正确。他们是正确的。然而,即使我将该线性化深度发送到我的全屏四元着色器(下面的第二个着色器),屏幕仍然是全白的。

public void initFramebuffers() {
glBindFramebuffer(GL_FRAMEBUFFER, depthShader.fbo);
depthShader.initTexture(width, height, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthShader.tex, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
}

public void initTexture(int width, int height, int format, int internalFormat) {
tex = glGenTextures();
glBindTexture(GL_TEXTURE_2D, tex);
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_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_FLOAT, (ByteBuffer)null);
}

深度着色器:

#version 400

in vec3 pos;
in float radius;

uniform mat4 mView;
uniform mat4 projection;
uniform vec2 screenSize;
uniform vec3 lightPos;

out float depth;

float linearizeDepth(float depth) {
float n = 0.01;
float f = 100;
return (2.0 * n) / (f + n - depth * (f - n));
}

void main() {
//calculate normal
vec3 normal;
normal.xy = gl_PointCoord * 2.0 - 1.0;
float r2 = dot(normal.xy, normal.xy);

if (r2 > 1.0) {
discard;
}

normal.z = sqrt(1.0 - r2);

//calculate depth
vec4 pixelPos = vec4(pos + normal * radius, 1.0);
vec4 clipSpacePos = projection * pixelPos;

depth = clipSpacePos.z / clipSpacePos.w * 0.5f + 0.5f;
depth = linearizeDepth(depth);
}

读取深度的着色器。 linearizeDepth 中的值是我的近距离和远距离:

#version 400

in vec2 coord;

uniform sampler2D depthMap;
uniform vec2 screenSize;
uniform mat4 projection;

out vec4 color;

float linearizeDepth(float depth) {
float n = 0.01;
float f = 100;
return (2.0 * n) / (f + n - depth * (f - n));
}

void main() {
float curDepth = texture2D(depthMap, coord).x;
//float d = linearizeDepth(curDepth);

color = vec4(d, d, d, 1.0f);
}

绘制所有内容的代码:

//--------------------Particle Depth-----------------------
{
glUseProgram(depthShader.program);
glBindFramebuffer(GL_FRAMEBUFFER, depthShader.fbo);

depthShader.particleDepthVAO(points);

//Sets uniforms
RenderUtility.addMatrix(depthShader, mView, "mView");
RenderUtility.addMatrix(depthShader, projection, "projection");
RenderUtility.addVector2(depthShader, screenSize, "screenSize");
RenderUtility.addVector3(depthShader, lightPosition, "lightPos");

glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindVertexArray(depthShader.vao);

glDrawArrays(GL_POINTS, 0, points.size());
}

//Draw full screen
{
glUseProgram(blurShader.program);
glBindFramebuffer(GL_FRAMEBUFFER, 0);

blurShader.blurDepthVAO();

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, depthShader.tex);
glUniform1i(blurShader.depthMap, 0);

//Sets uniforms
RenderUtility.addMatrix(blurShader, mView, "mView");
RenderUtility.addMatrix(blurShader, projection, "projection");
RenderUtility.addVector2(blurShader, screenSize, "screenSize");

//glEnable(GL_DEPTH_TEST);

glBindVertexArray(blurShader.vao);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glViewport(0, 0, width, height);
}

最佳答案

问题最终是我的顶点着色器的输出变量名与片段着色器的输入变量名 (doh) 不匹配。上面发布的代码是 100% 正确的,以防将来有人看到。

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

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