Framebuffer Objects

Started by
5 comments, last by Josh@Dreamland 16 years, 1 month ago
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=oldarray;
          }
          __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==NULL) 
    {
        #<span class="cpp-keyword">if</span> SHOWERRORS
        show_error(<span class="cpp-literal">"Surface does not exist."</span>,<span class="cpp-number">0</span>);
        <span class="cpp-directive">#endif</span>
        <span class="cpp-keyword">return</span> -<span class="cpp-number">1</span>; 
    }
    
    glBindTexture(GL_TEXTURE_2D,__ENIGMA_surface_array-&amp;gt;tex);
    <span class="cpp-keyword">int</span> w=__ENIGMA_surface_array-&amp;gt;width;
    <span class="cpp-keyword">int</span> h=__ENIGMA_surface_array-&amp;gt;height;
    
    glBegin(GL_QUADS);
    glTexCoord2f(<span class="cpp-number">0</span>, <span class="cpp-number">0</span>);		glVertex2f(x,   y);
    glTexCoord2f(<span class="cpp-number">1</span>, <span class="cpp-number">0</span>);		glVertex2f(x+w, y);
    glTexCoord2f(<span class="cpp-number">1</span>, <span class="cpp-number">1</span>);		glVertex2f(x+w, y+h);
    glTexCoord2f(<span class="cpp-number">0</span>, <span class="cpp-number">1</span>);		glVertex2f(x,   y+h);
    glEnd();
    
    glBindTexture(GL_TEXTURE_2D, <span class="cpp-number">0</span>);
    
    <span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;
}
</pre></div><!–ENDSCRIPT–>

Please excuse my coding &#115;tyle. Everyone I know tells me it's horrible.

The code I use to draw it is as follows:
<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>	<span class="cpp-keyword">static</span> <span class="cpp-keyword">bool</span> runOnce = <span class="cpp-keyword">false</span>;
	<span class="cpp-keyword">static</span> <span class="cpp-keyword">int</span> s,t;
    <span class="cpp-keyword">static</span> <span class="cpp-keyword">int</span> u;
	
	<span class="cpp-keyword">if</span>(!runOnce) {
	runOnce = <span class="cpp-keyword">true</span>;
	u=<span class="cpp-number">0</span>;
	
	t = surface_create(<span class="cpp-number">256</span>, <span class="cpp-number">256</span>);
    s = surface_create(<span class="cpp-number">32</span>, <span class="cpp-number">32</span>);
	}
	surface_set_target(s);
	
    draw_clear(c_blue);
    
	glColor3f(<span class="cpp-number">0</span>, <span class="cpp-number">1</span>, <span class="cpp-number">1</span>);				
	glBegin(GL_TRIANGLES);
	glVertex2f(<span class="cpp-number">128</span>, <span class="cpp-number">0</span>);
	glVertex2f(<span class="cpp-number">256</span>, <span class="cpp-number">256</span>);
	glVertex2f(<span class="cpp-number">0</span>, <span class="cpp-number">256</span>);
	glEnd();
	
	surface_reset_target();
	
	surface_set_target(t);
	draw_clear(c_red);
	
	glColor3f(.<span class="cpp-number">5</span>, <span class="cpp-number">1</span>, <span class="cpp-number">1</span>);	
	
	glBegin(GL_TRIANGLES);
	glVertex2f(<span class="cpp-number">128</span>, <span class="cpp-number">0</span>);
	glVertex2f(<span class="cpp-number">0</span>,  <span class="cpp-number">256</span>);
	glVertex2f(<span class="cpp-number">256</span>, <span class="cpp-number">256</span>);
	glEnd();
				
	surface_reset_target();
	POINT __ENIGMA_mouse;
	GetCursorPos(&amp;__ENIGMA_mouse);
	mouse_x=__ENIGMA_mouse.x;
	mouse_y=__ENIGMA_mouse.y;
	
    draw_clear(c_fuchsia);
    
    draw_surface(t,<span class="cpp-number">0</span>,<span class="cpp-number">0</span>);
    draw_surface(s,<span class="cpp-number">0</span>,<span class="cpp-number">0</span>);
    
    
    draw_line(mouse_x-<span class="cpp-number">16</span>,mouse_y,mouse_x+<span class="cpp-number">16</span>,mouse_y);
    draw_line(mouse_x,mouse_y-<span class="cpp-number">16</span>,mouse_x,mouse_y+<span class="cpp-number">16</span>);
    
    SwapBuffers(__window_hDC);
    
    Sleep(<span class="cpp-number">1</span>);
</pre></div><!–ENDSCRIPT–>

Lastly, the projection is set, and the extension initialized, like so:

<!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre>#<span class="cpp-keyword">if</span> GMSURFACE
glBindFramebufferEXT   =    (PFNGLBINDFRAMEBUFFEREXTPROC) wglGetProcAddress(<span class="cpp-literal">"glBindFramebufferEXT"</span>);
glGenFramebuffersEXT   =    (PFNGLGENFRAMEBUFFERSEXTPROC) wglGetProcAddress(<span class="cpp-literal">"glGenFramebuffersEXT"</span>);
glFramebufferTexture2DEXT = (PFNGLFRAMEBUFFERTEXTURE2DEXTPROC) wglGetProcAddress(<span class="cpp-literal">"glFramebufferTexture2DEXT"</span>);
<span class="cpp-keyword">if</span> ((glBindFramebufferEXT==NULL) || (glGenFramebuffersEXT==NULL) || (glFramebufferTexture2DEXT==NULL))
{
   MessageBox(<span class="cpp-number">0</span>,<span class="cpp-literal">"Additional drawing surfaces cannot be created. Application will now terminate."</span>,<span class="cpp-literal">"Error"</span>,MB_OK);
}
<span class="cpp-directive">#endif</span>



glViewport(<span class="cpp-number">0</span>, <span class="cpp-number">0</span>, <span class="cpp-number">255</span>, <span class="cpp-number">255</span>);

glMatrixMode(GL_PROJECTION);

glClearColor(<span class="cpp-number">1</span>,<span class="cpp-number">1</span>,<span class="cpp-number">1</span>,<span class="cpp-number">1</span>);
glLoadIdentity();
glViewport(<span class="cpp-number">0</span>,<span class="cpp-number">0</span>,room_width,room_height);
glScaled(<span class="cpp-number">1</span>,-<span class="cpp-number">1</span>,<span class="cpp-number">1</span>);
glOrtho(<span class="cpp-number">0</span>,room_width,<span class="cpp-number">0</span>,room_height,-<span class="cpp-number">1</span>,<span class="cpp-number">1</span>);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


mplay_maek_opengl_work();
</pre></div><!–ENDSCRIPT–>

The &#111;nly thing I have not tried, which I &#111;nly now think of, is if I initialized the extension wrong. Thanks in advance for any help you have to offer. 

-Josh
Advertisement
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);


------------------------------

redwoodpixel.com

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]
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.

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.
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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Just so you all know, I got it now.

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

This topic is closed to new replies.

Advertisement