Reputation: 51
I'm now using opengl3.3 and glfw to draw+show. However, currently, OpenGL draws in its internal frame buffer. I suppose it is "glBindFramebuffer(GL_FRAMEBUFFER, 0);"
I wonder if I can define a buffer by myself and let OpenGL draw in this buffer? e.g.
GLuint ownbuffer[800*600*4]; // width:800, height:600, ARGB channel
// by setting some APIs to let OpenGL draw in the above buffer?
I read about FBO, my original thinking is that I can use it to draw in FBO buffer by filling the buffer address in the texture buffer. However, it seems that it does not work. I get an all-zero buffer. e.g.
GLuint frameBuffer;
glGenFramebuffers(1, &frameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
GLuint texColorBuffer;
GLuint ownbuffer[800*600*4];
glGenTextures(1, &texColorBuffer);
glActiveTexture(GL_TEXTURE8);
glBindTexture(GL_TEXTURE_2D, texColorBuffer);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, 800, 600, 0, GL_RGBA, GL_UNSIGNED_BYTE, ownbuffer
); // by setting buffer here instead of fill in NULL
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);
Moreover, is it possible to let glfw show our own buffer? e.g. ownbuffer mentioned above.
In this case, I don't need to dump buffer data into files for checking the result.
Upvotes: 1
Views: 337
Reputation: 210877
I want opengl directly draws into the buffer I prepare (
ownbuffer
) ...
Sorry, but that's not possible. OpenGL renders in the (default) framebuffer and you can't redirect that (or mirror) it to a CPU memory address.
You cannot attach a texture or render buffer to the default framebuffer. You have to create a named framebuffer object:
GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);
and you can copy the pixels of the color plane from the default framebuffer (0)
into the named framebuffer with glBlitFramebuffer
:
glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
glBlitFramebuffer(0, 0, 800, 600, 0, 0, 800, 600, GL_COLOR_BUFFER_BIT, GL_NEAREST);
Anyway, now the data is still in the texture data store and not in your ownbuffer
. The textures data store and the source data are not linked. glTexImage2D
just copies the data.
Instead of rendering to the default framebuffer, you can render to the named framebuffer directly (glBindFramebuffer(GL_FRAMEBUFFER, fbo);
). In this case, however, nothing is drawn in the window at all.
If you just want to read data from the framebuffer, you need to use glReadPixels
. glReadPixles
can read the data directly into CPU memory or read it in a GL_PIXEL_PACK_BUFFER
buffer. You can use Buffer Mapping to access the data from the pixel buffer (glMapBuffer
). However, in either case it is necessary to call glReadPixles
each time you want to get the current buffer data.
Upvotes: 2