# OpenGL Framebuffer Objects

## Recommended Posts

After fiddling and fiddling with framebuffer objects, I've just about called it quits. So this is my last lifeline, because no one I know can figure it out. First let me explain. I'm working on a project I named ENIGMA, whose purpose is to simulate the effects of high(er) level languages in low level code. I've had good success with the project to date, but these have me stumped. The Framebuffer objects' purpose is to simulate drawing surfaces. Because high level languages often don't deal with pointers, I keep all the surface data in an array to be referenced by an integer, which would be harmless for novices. So, that in mind. surface_create(width,height) is self explanatory, like most high(er) level functions. It creates a surface of the given dimensions, and returns an integer--its position in the array. surface_set_target(int id) is supposed to set the projection so all drawing fits the surface; surface->width is the right of the projection, 0 is the left. Same for vertical. surface_reset_target() is intended to restore the projection to its former state, and stop drawing to framebuffers. draw_surface(int id,int x,int y) is of course supposed to draw the surface. The surface system code is as follows. Note that I stopped messing with the projection in set_ and reset_ target. This is where I just gave up.
/************************************************************************************************                                                                                            **
**  Copyright (C) 2008 The ENIGMA Team  &lt;www.enigma-dev.org&gt;                                  **
**                                                                                            **
**  This file is a part of the ENIGMA Development Environment.                                **
**                                                                                            **
**  ENIGMA is free software: you can redistribute it and/or modify it under the               **
**  Foundation, version 3 of the license or any later version.                                **
**                                                                                            **
**  This application and its source code is distributed AS-IS, WITHOUT ANY WARRANTY; without  **
**  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the **
**  GNU General Public License for more details.                                              **
**                                                                                            **
**  You should have recieved a copy of the GNU General Public License along with this code.   **
**  If not, see &lt;http://www.gnu.org/licenses/&gt;                                                **
**                                                                                            **
**  ENIGMA is an environment designed to create games and other programs with a high-level,   **
**  compilable language. Developers of ENIGMA or anything associated with ENIGMA are in no    **
**  way responsible for its users or applications created by its users, or damages caused     **
**  the environment or programs made in the environment.                                      **
**                                                                                            **
\**********************************************************************************************/

PFNGLBINDFRAMEBUFFEREXTPROC      glBindFramebufferEXT;
PFNGLGENFRAMEBUFFERSEXTPROC      glGenFramebuffersEXT;
PFNGLFRAMEBUFFERTEXTURE2DEXTPROC glFramebufferTexture2DEXT;

struct __ENIGMA_surface
{
GLuint tex, fbo;
int width, height;
};

__ENIGMA_surface **__ENIGMA_surface_array;
int __ENIGMA_surface_max=0;

int surface_create(double width, double height)
{
GLuint tex, fbo;
int prevFbo;

int id,
w=(int)width,
h=(int)height; //get the integer width and height, and prepare to search for an id

if (__ENIGMA_surface_max==0)
{
__ENIGMA_surface_array=new __ENIGMA_surface*[1];
__ENIGMA_surface_max=1;
}

for (id=0; __ENIGMA_surface_array[id]!=NULL; id++)
{
if (id+1&gt;=__ENIGMA_surface_max)
{
__ENIGMA_surface **oldarray=__ENIGMA_surface_array;
__ENIGMA_surface_array=new __ENIGMA_surface*[__ENIGMA_surface_max+1];
for (int i=0; i&lt;__ENIGMA_surface_max; i++)
{
__ENIGMA_surface_array[i]=oldarray[i];
}
__ENIGMA_surface_array[__ENIGMA_surface_max]=NULL;
__ENIGMA_surface_max++;
delete[] oldarray;
}
}

__ENIGMA_surface_array[id] = new __ENIGMA_surface;
__ENIGMA_surface_array[id]-&gt;width = w;
__ENIGMA_surface_array[id]-&gt;height = h;

glGenTextures(1, &tex);
glGenFramebuffersEXT(1, &fbo);

glPushAttrib(GL_TEXTURE_BIT);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &prevFbo);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, tex, 0);
glDrawBuffer(GL_COLOR_ATTACHMENT0_EXT);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, prevFbo);
glPopAttrib();

__ENIGMA_surface_array[id]-&gt;tex = tex;
__ENIGMA_surface_array[id]-&gt;fbo = fbo;

return id;
}

void surface_set_target(int id)
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, __ENIGMA_surface_array[id]-&gt;fbo);
glPushAttrib(GL_VIEWPORT_BIT);
glScaled(1,1,1);
}

void surface_reset_target(void)
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glScaled(1,-1,1);
}

void surface_destroy(int id)
{
__ENIGMA_surface* surf=__ENIGMA_surface_array[id];
surf-&gt;width = surf-&gt;height = surf-&gt;tex = surf-&gt;fbo = 0;
delete surf;
}

int draw_surface(double id, double x, double y)
{
int s=(int)id;
if (s&gt;__ENIGMA_surface_max)
{
#if SHOWERRORS
show_error("Surface does not exist.",0);
#endif
return -1;
}
if (__ENIGMA_surface_array[s]==NULL)
{
#if SHOWERRORS
show_error("Surface does not exist.",0);
#endif
return -1;
}

glBindTexture(GL_TEXTURE_2D,__ENIGMA_surface_array[s]-&gt;tex);
int w=__ENIGMA_surface_array[s]-&gt;width;
int h=__ENIGMA_surface_array[s]-&gt;height;

glTexCoord2f(0, 0);		glVertex2f(x,   y);
glTexCoord2f(1, 0);		glVertex2f(x+w, y);
glTexCoord2f(1, 1);		glVertex2f(x+w, y+h);
glTexCoord2f(0, 1);		glVertex2f(x,   y+h);
glEnd();

glBindTexture(GL_TEXTURE_2D, 0);

return 0;
}

Please excuse my coding style. Everyone I know tells me it's horrible. The code I use to draw it is as follows:
	static bool runOnce = false;
static int s,t;
static int u;

if(!runOnce) {
runOnce = true;
u=0;

t = surface_create(256, 256);
s = surface_create(32, 32);
}
surface_set_target(s);

draw_clear(c_blue);

glColor3f(0, 1, 1);
glBegin(GL_TRIANGLES);
glVertex2f(128, 0);
glVertex2f(256, 256);
glVertex2f(0, 256);
glEnd();

surface_reset_target();

surface_set_target(t);
draw_clear(c_red);

glColor3f(.5, 1, 1);

glBegin(GL_TRIANGLES);
glVertex2f(128, 0);
glVertex2f(0,  256);
glVertex2f(256, 256);
glEnd();

surface_reset_target();
POINT __ENIGMA_mouse;
GetCursorPos(&__ENIGMA_mouse);
mouse_x=__ENIGMA_mouse.x;
mouse_y=__ENIGMA_mouse.y;

draw_clear(c_fuchsia);

draw_surface(t,0,0);
draw_surface(s,0,0);

draw_line(mouse_x-16,mouse_y,mouse_x+16,mouse_y);
draw_line(mouse_x,mouse_y-16,mouse_x,mouse_y+16);

SwapBuffers(__window_hDC);

Sleep(1);

Lastly, the projection is set, and the extension initialized, like so:
#if GMSURFACE
if ((glBindFramebufferEXT==NULL) || (glGenFramebuffersEXT==NULL) || (glFramebufferTexture2DEXT==NULL))
{
MessageBox(0,"Additional drawing surfaces cannot be created. Application will now terminate.","Error",MB_OK);
}
#endif

glViewport(0, 0, 255, 255);

glMatrixMode(GL_PROJECTION);

glClearColor(1,1,1,1);
glViewport(0,0,room_width,room_height);
glScaled(1,-1,1);
glOrtho(0,room_width,0,room_height,-1,1);

glMatrixMode(GL_MODELVIEW);

mplay_maek_opengl_work();

The only thing I have not tried, which I only now think of, is if I initialized the extension wrong. Thanks in advance for any help you have to offer. -Josh

##### Share on other sites
Well for starters, since the FBO is a buffer just like a regular opengl buffer, you need to clear the color and depth buffers of the fbo. try that and see if you get anything.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

##### Share on other sites
Unfortunately, that wasn't the problem.

I did fix something small. Apparently I forgot to push the color and set to white in the draw_surface() function.

So no more discoloration problems.

But now I just need some help with setting the projection correctly.

Since I used glScaled() in the projection, I don't know what to do about it come time for surfaces...

Thanks for the help. ^_^

[Edited by - Josh@Dreamland on March 2, 2008 9:21:30 AM]

##### Share on other sites
I'm lost as to why you are even using glScaled() in the function at all.

If you want to ensure all drawing operations fit into the FBO then you'll need to set the view port to the same size as the FBO. You'll probably want to set the projection matrix to sane values if you want to keep 1:1 mapping for pixels to values passed in.

You don't need glScaled() btw, you can just say glOrtho(0,room_width,room_height,0,-1,1); to set 0,0 as bottom left and width,height as top right.

##### Share on other sites
All right. glScaled is gone. And now everything is in order. But. I want to keep the 0,width,height,0 projection from surface to surface, as well as on the screen.

What I have so now is this:
void surface_set_target(int id){    __ENIGMA_surface* surf=__ENIGMA_surface_array[id]; //copy the surface data from the array    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, surf->fbo); //bind it    glPushMatrix(); //So you can pop it in the reset    glPushAttrib(GL_VIEWPORT_BIT); //same    glViewport(0,0,surf->width,surf->height); //fit view to surface    glOrtho(0,surf->width/room_width,surf->height/room_height,0,-1,1); //does nothing}

I'm trying to use glOrtho to make it so primitive coords will be integers from 0 to width and 0 to height, instead of always being from 0 to room_width and 0 to room_height. Of course, it won't accept anything I change it to anymore for some reason. I'm officially done messing with it. For now. >_<

Also, either way, all the while when I get the big surface to work the little one is too small for its huge and somehow unchanging projection.

I just need the projection of any surface to be its width by its height. Is that hard? =\ I've had no luck.

##### Share on other sites
How about glOrtho(0, surf->width, 0, surf->height, -1, 1)? That sets the coordinates to be from 0-width in the x direction and from 0-height in the y direction.

##### Share on other sites
Just so you all know, I got it now.

[Edited by - Josh@Dreamland on March 4, 2008 1:10:25 PM]

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627678
• Total Posts
2978605
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 12
• 12
• 10
• 12
• 22