Page 1 of 1

Double Buffering in OpenGL.

Posted: Fri Aug 27, 2010 10:20 pm
by epicasian
Hey guys,
I'm just starting with OpenGL and am confused on how to actually use double buffering in OpenGL with SDL. The book I'm reading uses GLUT, so that doesn't really help at all. I've looked on teh intrawebz and haven't found anything useful yet.

Here is some code I've got so far (please note it is poorly structured and very ugly, as I am just beginning:D)

Code: Select all

include <SDL.h>
#include <SDL_opengl.h>

//coordinate variables

void RenderScene()
{
	//Clear windows with current clearing color
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, 640, 480, 0, -1, 1);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//sets current drawing color to red
	glColor3f(1.0f, 0.0f, 0.0f);

	//draw a rect w/ current color
	glRectf(x1, y1, x2, y2);

	SDL_GL_SwapBuffers();
}
void SetupRC()
{
	glClearColor(0.6f, 0.4f, 0.7f, 1.0f);
}

int main(int argc, char *argv[])
{
        //init stuff
	SDL_SetVideoMode(640, 480, 32, SDL_OPENGL | SDL_GL_DOUBLEBUFFER);

	while(event.type != SDL_QUIT)
	{
		SDL_PollEvent(&event);
		key = SDL_GetKeyState(NULL);
		RenderScene();
		SetupRC();
                //window collision stuff

		SDL_Delay(45);
	}
	SDL_Quit();
	return 0;
}
Thanks you in advance for any and all help :D

Re: Double Buffering in OpenGL.

Posted: Fri Aug 27, 2010 10:28 pm
by Ginto8
that's pretty much how you do it =P SDL_GL_SwapBuffers() changes your buffer, and glClear() clears it for the next drawing sequence.

Re: Double Buffering in OpenGL.

Posted: Fri Aug 27, 2010 10:29 pm
by dandymcgee
First result on Google:

Code: Select all

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);

Re: Double Buffering in OpenGL.

Posted: Sat Aug 28, 2010 5:33 am
by epicasian
Ginto8 wrote:that's pretty much how you do it =P SDL_GL_SwapBuffers() changes your buffer, and glClear() clears it for the next drawing sequence.
Thanks Ginto, but when I run my program, (which basically is just a red rectangle, bouncing off the edges of the screen) it looks very jumpy and jittery. Am I missing something?


Thanks again.

EdiT:Added my main.cpp

Re: Double Buffering in OpenGL.

Posted: Sat Aug 28, 2010 12:00 pm
by qpHalcy0n
Because you have a 45msec sleep time in there. This is a tremendous amount of time. Assuming the granularity of the timer is decent (which SDL's is NOT), you'd get somewhere around 22fps. Maybe more, maybe less.

My suggestion would be to ditch that and write your own timer to get the app to run at some target rate.

Re: Double Buffering in OpenGL.

Posted: Sat Aug 28, 2010 1:09 pm
by Ginto8
qpHalcy0n wrote:Because you have a 45msec sleep time in there. This is a tremendous amount of time. Assuming the granularity of the timer is decent (which SDL's is NOT), you'd get somewhere around 22fps. Maybe more, maybe less.

My suggestion would be to ditch that and write your own timer to get the app to run at some target rate.
and iirc <ctime> has a number of utilities to do exactly that ;)