我正在写一个延迟的着色代码 我有一个带有4种颜色附件(纹理)和深度附件(渲染缓冲器)的FBO 我使用着色器渲染我的场景,写入这4个颜色附件 问题是,第0种颜色附件的数据也写入其他颜色。
以下是我的代码:
Renderer_Deferred.java
public class Renderer_Deferred
{
private final int FBO;
private final int depthbuffer;
public final int positiontexture, diffusetexture, normaltexture, bumpmap;
Renderer_Deferred()
{
System.out.println("Setting up the deferrer..");
positiontexture = GL11.glGenTextures();
diffusetexture = GL11.glGenTextures();
normaltexture = GL11.glGenTextures();
bumpmap = GL11.glGenTextures();
FBO = glGenFramebuffers();
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, positiontexture);
GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexImage2D(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0, GL30.GL_RGB32F, Configuration.displayWidth, Configuration.displayheight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, diffusetexture);
GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexImage2D(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0, GL11.GL_RGBA, Configuration.displayWidth, Configuration.displayheight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, normaltexture);
GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexImage2D(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0, GL30.GL_RGB16F, Configuration.displayWidth, Configuration.displayheight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, bumpmap);
GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
GL11.glTexParameterf(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
GL11.glTexImage2D(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0, GL11.GL_RGBA, Configuration.displayWidth, Configuration.displayheight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0);
GL11.glViewport(0, 0, Configuration.displayWidth, Configuration.displayheight);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, positiontexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, diffusetexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, normaltexture, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, bumpmap, 0);
depthbuffer = glGenRenderbuffers();
glBindRenderbuffer(GL_RENDERBUFFER, depthbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL14.GL_DEPTH_COMPONENT24, Configuration.displayWidth, Configuration.displayheight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
System.err.println("Generating the deferred FBO failed!");
System.exit(5321);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0); ////////////////////////////////////////////////////////
}
public void startRendering()
{
// CLEAR AND HOOK UP TEXTURES
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
IntBuffer buf = BufferUtils.createIntBuffer(4);
buf.put(GL_COLOR_ATTACHMENT0);
buf.put(GL_COLOR_ATTACHMENT1);
buf.put(GL_COLOR_ATTACHMENT2);
buf.put(GL_COLOR_ATTACHMENT3);
buf.flip();
GL20.glDrawBuffers(buf);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
System.err.println("Deferred renderer failed to setup correctly!");
}
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glViewport(0, 0, Configuration.displayWidth, Configuration.displayheight);
GL11.glClearColor(SimpleCraft.world.skycolor.x, SimpleCraft.world.skycolor.y, SimpleCraft.world.skycolor.z, 0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
}
public void stopRendering()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
}
以上是我的代码,用于激活和停用延迟渲染器。 完成此操作后,后处理器将激活:
public class Renderer_Postprocessor
{
PostprocessingObject postprocessor;
public static int loc_pos, loc_diffuse, loc_normal, loc_bump, loc_ambient;
public static int loc_viewingdistance, loc_modelviewmatrix;
Renderer_Postprocessor()
{
postprocessor = new PostprocessingObject();
}
public void postprocess(int pos, int diffuse, int normal, int bump)
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL20.glUseProgram(Shaders.PROGRAM_POSTPROCESSOR);
GL20.glDrawBuffers(GL11.GL_BACK);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, pos);
GL13.glActiveTexture(GL13.GL_TEXTURE1);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, diffuse);
GL13.glActiveTexture(GL13.GL_TEXTURE2);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, normal);
GL13.glActiveTexture(GL13.GL_TEXTURE3);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, bump);
GLUtility.uploadToShader(Configuration.viewDistanceInBlocks, loc_viewingdistance);
GLUtility.uploadToShader(Camera.getModelViewMatrix(new Matrix4f(), Camera.getViewMatrix()), loc_modelviewmatrix);
GL30.glBindVertexArray(postprocessor.getVAO());
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 6);
GL30.glBindVertexArray(0);
GL20.glUseProgram(0);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0);
GL13.glActiveTexture(GL13.GL_TEXTURE1);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0);
GL13.glActiveTexture(GL13.GL_TEXTURE2);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0);
GL13.glActiveTexture(GL13.GL_TEXTURE3);
GL11.glBindTexture(ARBTextureRectangle.GL_TEXTURE_RECTANGLE_ARB, 0);
GL11.glEnable(GL11.GL_DEPTH_TEST);
}
}
下面是deferrer和后处理器的片段着色器:
Deferred_Fragment.frag
#version 330 core
#extension GL_ARB_explicit_uniform_location : require
layout(location=0) uniform sampler2DArray texture_diffuse;
layout(location=1) uniform sampler2DArray texture_normal;
uniform vec3 pass_EyeVector;
in vec4 pass_Position;
in vec3 pass_TextureCoord;
in vec3 pass_Normal;
layout(location = 0) out vec4 pos;
layout(location = 1) out vec4 dif;
layout(location = 2) out vec4 nor;
layout(location = 3) out vec4 bum;
void main(void)
{
pos = texture(texture_diffuse, pass_TextureCoord); // Transform from -1 / 1 to 0 / 1
dif = pass_Position;
nor = vec4(pass_Normal, 1.0);
bum = texture(texture_normal, pass_TextureCoord);
}
和postprocessor.frag
#version 330 core
#extension GL_ARB_explicit_uniform_location : require
layout(location=0) uniform sampler2DRect positiontexture;
layout(location=1) uniform sampler2DRect diffusetexture;
layout(location=2) uniform sampler2DRect normaltexture;
layout(location=3) uniform sampler2DRect bumpmap;
in vec2 pass_TextureCoord;
out vec4 out_Color;
void main(void)
{
out_Color = texture(diffusetexture, pass_TextureCoord);
}
答案 0 :(得分:1)
您正在尝试将location
布局限定符用于统一变量:
layout(location=0) uniform sampler2DArray texture_diffuse;
layout(location=1) uniform sampler2DArray texture_normal;
layout(location=0) uniform sampler2DRect positiontexture;
layout(location=1) uniform sampler2DRect diffusetexture;
layout(location=2) uniform sampler2DRect normaltexture;
layout(location=3) uniform sampler2DRect bumpmap;
我一直无法在GLSL规范中找到任何暗示支持的内容。至少在330中,仅列出顶点着色器输入和片段着色器输出以支持location
。在以后的版本中,它似乎也支持其他着色器阶段的in
/ out
个变量,但我仍然没有看到它对制服的任何提及。
您可能打算使用此限定符设置采样器的纹理单元。有一个binding
限定符,如下所示:
layout(binding=0) uniform sampler2DArray texture_diffuse;
layout(binding=1) uniform sampler2DArray texture_normal;
仅在OpenGL 4.2(GLSL版本420)及更高版本中支持此功能。如果你想坚持使用GLSL 330,你需要使用glGetUniformLocaction()
和glUniform1i()
以老式的方式设置采样器制服的纹理单元。
答案 1 :(得分:0)
您应该使用布局绑定而不是布局位置来绑定采样器。 您可能还应该重新检查绑定点,因为您有多个采样器绑定到同一位置。如果将纹理绑定到TEXTURE0,请将其指定为binding = 0。
尝试将其更改为:
layout(binding=0) uniform sampler2DArray texture_diffuse;
layout(binding=1) uniform sampler2DArray texture_normal;
layout(binding=0) uniform sampler2DRect positiontexture;
layout(binding=1) uniform sampler2DRect diffusetexture;
layout(binding=2) uniform sampler2DRect normaltexture;
layout(binding=3) uniform sampler2DRect bumpmap;