# 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
_the_phantom_    11250
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
swiftcoder    18426
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

• ### Similar Content

• Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.

• By KarimIO
Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!

• Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
I'm available for a good conversation about Game Engine / Graphics Programming
• By C0dR
I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.

The following features are implemented:
Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping
You can find the repository at https://github.com/0x2A/physicam

I would be happy about feedback, suggestions or contributions.

• By altay
Hi folks,
Imagine we have 8 different light sources in our scene and want dynamic shadow map for each of them. The question is how do we generate shadow maps? Do we render the scene for each to get the depth data? If so, how about performance? Do we deal with the performance issues just by applying general methods (e.g. frustum culling)?
Thanks,

• 14
• 9
• 16
• 66
• 12