我正在尝试渲染到FBO,然后在OpenGL / LWJGL中进行屏幕显示,我得到了奇怪的结果。
我的场景如果直接渲染到帧缓冲区就可以正常工作,看起来像这样:
但是当我渲染到FBO然后再渲染到默认帧缓冲区的四边形时,我得到了这个:
看起来图像中的所有颜色都采用单一颜色进行平均,就像屏幕被清除一样。
我的猜测是,四边形纹理的采样有问题,但我无法弄明白。
四边形显然是在那里,因为我看到glPolymode - GL_LINE的边缘。此外,当我移动游戏相机时,四边形上的单色会发生变化,并以某种方式反映出屏幕上最突出的颜色。那么也许你可以指出我可能很明显的解决方案?我将在本文末尾提出一些我认为相关的代码。
这就是我设置小队顶点和uv坐标的方法。我对立方体使用相同的方法和父类,所以我知道这有效:
vertices[0] = new Vertex(-1.0f,-1.0f,0.0f);
vertices[0].setTexCoords(0.0f,0.0f);
vertices[1] = new Vertex(-1.0f,1.0f,0.0f);
vertices[1].setTexCoords(0.0f,1.0f);
vertices[2] = new Vertex(1.0f,1.0f,0.0f);
vertices[2].setTexCoords(1.0f,1.0f);
vertices[3] = new Vertex(1.0f,-1.0f,0.0f);
vertices[3].setTexCoords(1.0f,0.0f);
这就是我将它们绑定到vbos的方式:
vaoID = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vaoID);
//POSITION - LOCATION 0
int vboID = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
FloatBuffer buffer = BufferUtils.createFloatBuffer(vertices.length*3);
buffer.put(getPositions());
buffer.flip();
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0,0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
buffer.clear();
//... some code for color and normal, going into VBO location 1 and 2
//TEX COORDS - LOCATION 3
vboID = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboID);
buffer = BufferUtils.createFloatBuffer(vertices.length*2);
buffer.put(getTexCoords());
buffer.flip();
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(3, 2, GL11.GL_FLOAT, false, 0,0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
buffer.clear();
这是我如何绑定四边形着色器中的属性位置:
protected void bindAttributes(){
GL20.glBindAttribLocation(id, 0, "vertex_position");
GL20.glBindAttribLocation(id, 3, "vertex_texCoord");
}
这是我渲染屏幕四边形的方式:
GL20.glUseProgram(presentShader.getProgramID());
GL30.glBindVertexArray(screenQuad.getMesh(0).getVaoID());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(3);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
//GL11.glBindTexture(GL11.GL_TEXTURE_2D, frameBufferTextureID);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, frameBufferTextureID);
GL11.glDrawElements(GL11.GL_TRIANGLES, screenQuad.getMesh(0).getIndexCount(), GL11.GL_UNSIGNED_INT, 0);
GL20.glDisableVertexAttribArray(3);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
GL20.glUseProgram(0);
这是我的顶点和片段着色器: 顶点:
#version 400 core
in vec2 vertex_position;
in vec2 vertex_texCoords;
out vec2 pass_texCoords;
void main(){
gl_Position = vec4(vertex_position.x, vertex_position.y, 0.0f, 1.0f);
pass_texCoords = vertex_texCoords;
}
片段:
#version 400 core
in vec2 pass_texCoords;
out vec4 fragment_color;
uniform sampler2D screenTexture;
void main(){
fragment_color = texture(screenTexture, pass_texCoords);
//fragment_color = vec4(0.3,0.3,0.3,1.0);
}
最后,这是我的渲染程序: 准备(); drawScene函数(); 本();
使用这些功能:
public void prepare(){
//FBO aktivieren, alles danach wird auf FBO-Textur gerendert
GL11.glClearColor(0.0f,0.0f,0.0f, 1.0f);
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBufferID);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glEnable(GL11.GL_STENCIL_TEST);
GL11.glStencilOp(GL11.GL_KEEP, GL11.GL_KEEP, GL11.GL_REPLACE);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
}
public void present(){
//Standard Framebuffer aktivieren
GL11.glClearColor(1.0f,1.0f,1.0f, 1.0f);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glDisable(GL11.GL_STENCIL_TEST);
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL20.glUseProgram(presentShader.getProgramID());
GL30.glBindVertexArray(screenQuad.getMesh(0).getVaoID());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(3);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, frameBufferTextureID);
GL11.glDrawElements(GL11.GL_TRIANGLES, screenQuad.getMesh(0).getIndexCount(), GL11.GL_UNSIGNED_INT, 0);
GL20.glDisableVertexAttribArray(3);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
GL20.glUseProgram(0);
}
编辑:也许我应该添加我如何设置FBO:
private void linitializeFrameBuffer(int viewportWidth, int viewportHeight){
//COLOR TEXTURE
frameBufferID = GL30.glGenFramebuffers();
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, frameBufferID); // GL_READ_FRAMEBUFFER, GL_DRAW_FRAMEBUFFER, GL_FRAMEBUFFER
frameBufferTextureID = GL11.glGenTextures();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, frameBufferTextureID); //GL_TEXTURE_1D, GL_TEXTURE2D, GL_TEXTURE3D
FloatBuffer pixelBuffer;
pixelBuffer = BufferUtils.createFloatBuffer(viewportWidth*viewportHeight*3);
pixelBuffer.clear();
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, viewportWidth, viewportHeight, 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, (java.nio.ByteBuffer) null);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR );
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR );
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
//Attach the new Texture to the framebuffer
GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, GL11.GL_TEXTURE_2D, frameBufferTextureID, 0);
//DEPTH BUFFER
depthBufferID = GL30.glGenRenderbuffers();
GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, depthBufferID);
GL30.glRenderbufferStorage(GL30.GL_RENDERBUFFER, GL30.GL_DEPTH24_STENCIL8, viewportWidth, viewportHeight);
GL30.glBindRenderbuffer(GL30.GL_RENDERBUFFER, 0);
//Attach the new renderbuffer to the framebuffer
GL30.glFramebufferRenderbuffer(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_STENCIL_ATTACHMENT, GL30.GL_RENDERBUFFER, depthBufferID);
if(GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER) != GL30.GL_FRAMEBUFFER_COMPLETE){
System.out.println("ERROR::FRAMEBUFFER:: Framebuffer is not complete!");
}
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
}
答案 0 :(得分:1)
首先,
GL30.glBindVertexArray(screenQuad.getMesh(0).getVaoID());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(3);
enableVertexAttribArray应该在初始化VAO的函数中,而不是在渲染循环期间;)。就这样,disableVertexAttribArray是“无用的”。
其次,由于直接状态访问是OpenGL核心的一部分,我对“绑定到工作”并不是很方便。 但是,我可以看到2个最终的警告(即使我不太确定)
你给glTexImage2D提供了“内部格式”GL_RGB,如果我是你,我会使用GL_RGB8。 (我知道GL_RGB使用glTexStorage是不正确的,但也许可以将它与GLTexImage2D一起使用,启用arb调试输出以了解是否存在错误)。
在将纹理发送到frameBuffer之前取消绑定纹理(在阅读文档之后,它应该没问题。)
您正在创建从未使用过的“pixelBuffer”。
最后,我在使用Depth和Stencil缓冲区方面遇到了很多问题,特别是当我使用renderBuffer时。 我建议你切换到纹理而不是renderBuffer,并开始只使用没有模板的depthTexture来“调试”你的程序。
编辑:正如Djindjidj所写,你在你的着色器中不尊重vec2 / vec3答案 1 :(得分:1)
得到了解决方案。
着色器中的纹理坐标称为in vec2 vertex_texCoords;
我将它们引用为GL20.glBindAttribLocation(id, 3, "vertex_texCoord");
将其更改为GL20.glBindAttribLocation(id, 3, "vertex_texCoords");
完成了这项工作。