Hi,
I’m trying to implement a framebuffer in order to do post processing.
However, I’m a bit stuck.
I get no errors when creating the framebuffer, or even during runtime.
Shaders produce no errors in compiling and linking.
Although it does produce an error val of 1280, but the message equaling null. Which is puzzling.
The error was from when I was checking the framebuffer completeness, I was putting the wrong value in the target for
GL30.glCheckFramebufferStatus(GL30.GL_FRAMEBUFFER);
Of which it does indeed complete.
This is what the code snippet looks like.
int val = GL11.glGetError();
System.out.println("Error Val: " + Integer.toString(val));
System.out.println("Error Message: " + GLU.gluGetString(val));
This returns GL_NO_ERROR but not GL_FRAMEBUFFER_COMPLETE
Besides that no errors.
Yet, it doesn’t render anything, other than the refresh color.
Oh the growth pains of learning…
Here’s some images:
Just the default framebuffer.
http://i.imgur.com/3LrFOVbl.png
With my unsuccessful attempt at implementing a framebuffer.
(Changed the refresh color, so it’s easier to tell if there’s any change)
http://i.imgur.com/YrxxNx0l.png
Here’s the code where the rendering is done.
public void update(GameObject[] gameObject) {
updateMatrices();
// Temp
if(fb) {
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, getFrameBuffer().getFBO());
} else GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
// Temp
float[] color1 = {0.4f, 0.6f, 0.9f, 0f};
setClCol(color1);
setClColBuffer(BufferUtils.createFloatBuffer(getClCol().length));
getClColBuffer().put(getClCol());
getClColBuffer().flip();
GL11.glViewport(0, 0, getCamera().getWidthi(), getCamera().getHeighti());
GL30.glClearBuffer(GL11.GL_COLOR, 0, getClColBuffer());
GL30.glClearBuffer(GL11.GL_DEPTH, 0, getClDepthBuffer());
GL20.glUseProgram(getShader().getShaderProgram());
// Projection Matrix
getProjectionMatrix().store(getMatrixBuffer());
getMatrixBuffer().flip();
GL20.glUniformMatrix4(getShader().getUniform(Uniform.PROJECTION).getLocation(), false, getMatrixBuffer());
// View Matrix
getViewMatrix().store(getMatrixBuffer());
getMatrixBuffer().flip();
GL20.glUniformMatrix4(getShader().getUniform(Uniform.VIEW).getLocation(), false, getMatrixBuffer());
// These two things will be removed.
GL11.glLineWidth(5f);
GL11.glPointSize(10f);
for(int i = 0; i < gameObject.length; i++) {
// Model matrix
for(int j = 0; j < gameObject[i].getNumOf(); j++) {
gameObject[i].getModelMatrix()[j].store(getMatrixBuffer());
}
getMatrixBuffer().flip();
GL20.glUniformMatrix4(getShader().getUniform(Uniform.MODEL).getLocation(), false, getMatrixBuffer());
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, gameObject[i].getMaterial().getVboT());
GL30.glBindVertexArray(gameObject[i].getModel().getVao());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, gameObject[i].getModel().getVboI());
// If statement is temporary, will be removed
// It will and will render tris not lines.
if(lines)GL31.glDrawElementsInstanced(GL11.GL_LINES, gameObject[i].getModel().getNumOfIndeces(),
GL11.GL_UNSIGNED_INT, 0, gameObject[i].getNumOf());
else GL31.glDrawElementsInstanced(GL11.GL_TRIANGLES, gameObject[i].getModel().getNumOfIndeces(),
GL11.GL_UNSIGNED_INT, 0, gameObject[i].getNumOf());
}
// If statement is temporary, will be removed
if(fb) {
// Render frame buffer
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
// Temp
float[] color2 = {0.2f, 0.3f, 0.5f, 0f};
setClCol(color2);
setClColBuffer(BufferUtils.createFloatBuffer(getClCol().length));
getClColBuffer().put(getClCol());
getClColBuffer().flip();
GL11.glViewport(0, 0, getCamera().getWidthi(), getCamera().getHeighti());
GL30.glClearBuffer(GL11.GL_COLOR, 0, getClColBuffer());
GL30.glClearBuffer(GL11.GL_DEPTH, 0, getClDepthBuffer());
GL11.glBindTexture(GL11.GL_TEXTURE_2D, getFrameBuffer().getTexture());
GL11.glBindTexture(GL11.GL_TEXTURE_2D, getFrameBuffer().getDepth());
GL20.glUseProgram(getPost().getShaderProgram());
// Draw square
GL30.glBindVertexArray(getBufferQuad().getVao());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, getBufferQuad().getVboI());
// If statement is temporary, will be removed
if(lines) GL11.glDrawElements(GL11.GL_LINES, getBufferQuad().getNumOfIndeces(), GL11.GL_UNSIGNED_BYTE, 0);
else GL11.glDrawElements(GL11.GL_TRIANGLES, getBufferQuad().getNumOfIndeces(), GL11.GL_UNSIGNED_BYTE, 0);
}
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0);
GL20.glUseProgram(0);
getCamera().reset();
if(getIfSync()) {
Display.sync(getFps());
}
Display.update();
ErrorCheck.checkForGLError(" * Render.java * ");
}
Where I create the framebuffer.
private void init(int width, int height, int[] buffers) {
// This is here just in case it fails some where,
// and to make it look pretty than a simple log in the console.
System.out.println("Initiating Framebuffer" + "\n -----------------------------");
setFBO(GL30.glGenFramebuffers());
GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, getFBO());
// Texture for color
setTexture(GL11.glGenTextures());
GL11.glBindTexture(GL11.GL_TEXTURE_2D, getTexture());
GL42.glTexStorage2D(GL11.GL_TEXTURE_2D, 1, GL30.GL_RGBA16F, width, height);
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);
// Texture for depth
setDepth(GL11.glGenTextures());
GL11.glBindTexture(GL11.GL_TEXTURE_2D, getDepth());
GL42.glTexStorage2D(GL11.GL_TEXTURE_2D, 1 , GL30.GL_DEPTH_COMPONENT32F, width, height);
GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_COLOR_ATTACHMENT0, getTexture(), 0);
GL32.glFramebufferTexture(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, getDepth(), 0);
IntBuffer drawBuffers = BufferUtils.createIntBuffer(buffers.length);
for(int i = 0; i < buffers.length; i++) {
drawBuffers.put(buffers[i]);
}
drawBuffers.flip();
GL20.glDrawBuffers(GL30.GL_COLOR_ATTACHMENT0);
errorCheck();
System.out.println("Framebuffer Initiated" + "\n -----------------------------");
}
Here are my shaders.
Main vertex.
#version 430 core
layout(location = 0) in vec4 in_Position;
layout(location = 1) in vec2 in_TextureCoord;
out VS_OUT {
vec2 pass_TextureCoord;
vec4 pass_Color;
} vs_out;
uniform mat4 proj_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
void main(void) {
gl_Position = in_Position;
gl_Position = proj_matrix * view_matrix * model_matrix * in_Position;
vs_out.pass_Color = vec4(1.0, 1.0, 1.0, 1.0);
vs_out.pass_TextureCoord = in_TextureCoord;
}
Main Fragment.
#version 430 core
uniform sampler2D diffuse_Tex;
in VS_OUT {
vec2 pass_TextureCoord;
vec4 pass_Color;
} fs_in;
out vec4 out_Color;
void main(void) {
out_Color = texture(diffuse_Tex, fs_in.pass_TextureCoord);
}
Post Vertex.
#version 430 core
layout(location = 0) in vec4 in_Position;
layout(location = 1) in vec2 in_TexCoord;
out vec2 pass_TexCoord;
void main(void) {
gl_Position = in_Position;
pass_TexCoord = in_TexCoord;
}
Post Fragment
#version 430 core
uniform sampler2D fbo_texture;
uniform sampler2D fbo_depth;
in vec2 pass_TextureCoord;
out vec4 out_Color;
void main(void) {
out_Color = texture2D(fbo_texture, pass_TextureCoord);
}
Um, if there’s any other code needed, please let me know, I prefer to try and figure these things out myself,
but this has me stumped. ???
Oh and sorry for what I have done to poor suzanne.
Any help is appreciated.