Incomplete rendering to FBO/texture --> file
Posted by tarto on Aug 03, 2015; 5:03pm
URL: https://forum.jogamp.org/Incomplete-rendering-to-FBO-texture-file-tp4035021.html
Hello
I) I set an FBO and texture this way:
int[] p = new int[1];
gl.glGenFramebuffers(1, p, 0);
mFbo = p[0];
gl.glBindFramebuffer(GL2GL3.GL_FRAMEBUFFER, mFbo);
// create rendering texture
gl.glGenTextures(1, p, 0);
mTexture = p[0];
gl.glBindTexture(GL2GL3.GL_TEXTURE_2D, mTexture);
gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_MIN_FILTER, GL2GL3.GL_LINEAR);
gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_MAG_FILTER, GL2GL3.GL_LINEAR);
gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_WRAP_S, GL2GL3.GL_CLAMP_TO_EDGE);
gl.glTexParameteri(GL2GL3.GL_TEXTURE_2D, GL2GL3.GL_TEXTURE_WRAP_T, GL2GL3.GL_CLAMP_TO_EDGE);
gl.glTexImage2D(GL2GL3.GL_TEXTURE_2D, 0, GL2GL3.GL_RGBA, mWidth, mHeight, 0, GL2GL3.GL_RGBA, GL2GL3.GL_UNSIGNED_BYTE, null);
gl.glBindTexture(GL2GL3.GL_TEXTURE_2D, 0);
// associate the texture as the rendering buffer for the whiteboard framebuffer
gl.glFramebufferTexture2D(GL2GL3.GL_FRAMEBUFFER, GL2GL3.GL_COLOR_ATTACHMENT0, GL2GL3.GL_TEXTURE_2D, whiteboardRenderTexture, 0);
// create VBO to hold vertices
gl.glGenBuffers(1, p, 0);
mVBO = p[0];
gl.glBindBuffer(GL2GL3.GL_ARRAY_BUFFER, mVBO);
// check FBO status
if(gl.glCheckFramebufferStatus(GL2GL3.GL_FRAMEBUFFER) != GL2GL3.GL_FRAMEBUFFER_COMPLETE) {
System.out.println("Failed to complete whiteboard framebuffer object creation:" + gl.glCheckFramebufferStatus(GL2GL3.GL_FRAMEBUFFER));
return;
}
gl.glBindFramebuffer(GL2GL3.GL_FRAMEBUFFER, 0);
II) I try to draw a simple triangle to the FBO and its bound texture:
float vertices[] = { -0.5f, -0.5f, 0.5f, -0.5f, 0.0f, 0.5f };
int vertexCount = 3;
FloatBuffer verticesBuffer = FloatBuffer.wrap(vertices);
verticesBuffer.position(0);
gl.glBindFramebuffer(GL2GL3.GL_FRAMEBUFFER, mFbo);
gl.glClearColor(rand.nextFloat(), rand.nextFloat(), rand.nextFloat(), 1.0f);
gl.glClear(GL2GL3.GL_COLOR_BUFFER_BIT);
gl.glBufferData(GL2GL3.GL_ARRAY_BUFFER, vertexCount, verticesBuffer, GL2GL3.GL_DYNAMIC_DRAW); // populate the VBO with the actual vertices to be drawn
gl.glUseProgram(mDrawProgram);
gl.glEnableVertexAttribArray(pointAttributeLocation);
gl.glVertexAttribPointer(pointAttributeLocation, kVertexDimensions, GL2GL3.GL_FLOAT, false, kVertexStride, 0);
gl.glGetError();
gl.glViewport(0, 0, mWidth, mHeight);
gl.glDrawArrays(GL2GL3.GL_TRIANGLES, 0, vertexCount);
gl.glGetError();
gl.glBindFramebuffer(GL2GL3.GL_FRAMEBUFFER, 0);
III) I save everything to some file:
gl.glBindFramebuffer(GL2GL3.GL_FRAMEBUFFER, mFbo);
ByteBuffer buffer = ByteBuffer.allocateDirect(mWidth * mHeight * 4);
BufferedImage image = new BufferedImage(mWidth, mHeight, BufferedImage.TYPE_INT_ARGB);
gl.glReadBuffer(GL2GL3.GL_COLOR_ATTACHMENT0);
gl.glReadPixels(0, 0, mWidth, mHeight, GL2GL3.GL_RGBA, GL2GL3.GL_UNSIGNED_BYTE, buffer);
gl.glGetError();
for (int w = 0; w < mWidth; w++) {
for (int h = 0; h < mHeight; h++) {
int i = (w + (mWidth * h)) * 4;
int r = buffer.get(i) & 0xFF;
int g = buffer.get(i + 1) & 0xFF;
int b = buffer.get(i + 2) & 0xFF;
int a = buffer.get(i + 3) & 0xFF;
image.setRGB(w, h, (a << 24) | (r << 16) | (g << 8) | b);
}
}
try {
ImageIO.write(image, "png", new File(SAVED_IMAGES_FILES_PATH+”/whatever.png“));
} catch (IOException ex) { ex.printStackTrace(); }
gl.glBindFramebuffer(GL2GL3.GL_FRAMEBUFFER, 0);
IV) I use those basic shaders
#version 330 core
in vec2 point;
void main() {
gl_Position = vec4(point.x, point.y, 0.0, 1.0);
}
and
#version 330 core
out vec4 color;
void main() {
color = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}
Result : I can see in the png file that the background set by clearColor is changing each time I execute, so something is actually written in the texture, and then to the file. But the triangle itself never appears in the file.
Can somebody point out what I am missing in my FBO/texture rendering ?
Thank you