Sign in to follow this  
Josh@Dreamland

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  <www.enigma-dev.org>                                  **
**                                                                                            **
**  This file is a part of the ENIGMA Development Environment.                                **
**                                                                                            **
**  ENIGMA is free software: you can redistribute it and/or modify it under the               **
**  terms of the GNU General Public License as published by the Free Software                 **
**  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 <http://www.gnu.org/licenses/>                                                **
**                                                                                            **
**  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>=__ENIGMA_surface_max)
        {
          __ENIGMA_surface **oldarray=__ENIGMA_surface_array;
          __ENIGMA_surface_array=new __ENIGMA_surface*[__ENIGMA_surface_max+1];
          for (int i=0; i<__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]->width = w;
	__ENIGMA_surface_array[id]->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);
	glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, prevFbo);
	glPopAttrib();
	
	__ENIGMA_surface_array[id]->tex = tex;
	__ENIGMA_surface_array[id]->fbo = fbo;
	
	return id;
}

void surface_set_target(int id)
{
    glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, __ENIGMA_surface_array[id]->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->width = surf->height = surf->tex = surf->fbo = 0;
	delete surf;
}

int draw_surface(double id, double x, double y)
{
    int s=(int)id;
    if (s>__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]->tex);
    int w=__ENIGMA_surface_array[s]->width;
    int h=__ENIGMA_surface_array[s]->height;
    
    glBegin(GL_QUADS);
    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
glBindFramebufferEXT   =    (PFNGLBINDFRAMEBUFFEREXTPROC) wglGetProcAddress("glBindFramebufferEXT");
glGenFramebuffersEXT   =    (PFNGLGENFRAMEBUFFERSEXTPROC) wglGetProcAddress("glGenFramebuffersEXT");
glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC) wglGetProcAddress("glFramebufferTexture2DEXT");
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);
glLoadIdentity();
glViewport(0,0,room_width,room_height);
glScaled(1,-1,1);
glOrtho(0,room_width,0,room_height,-1,1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this