java 纹理_java – 渲染纹理的深度

我无法正确渲染我的深度.没有抛出任何错误,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%正确的,如果有人在将来看到这个.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值