Double Buffering in OpenGL.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
epicasian
Chaos Rift Junior
Chaos Rift Junior
Posts: 232
Joined: Mon Feb 22, 2010 10:32 pm
Current Project: Gigazilla Engine
Favorite Gaming Platforms: Dreamcast, SNES, PS2, PC
Programming Language of Choice: C/++
Location: WoFo, KY

Double Buffering in OpenGL.

Post 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
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Double Buffering in OpenGL.

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Double Buffering in OpenGL.

Post by dandymcgee »

First result on Google:

Code: Select all

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
epicasian
Chaos Rift Junior
Chaos Rift Junior
Posts: 232
Joined: Mon Feb 22, 2010 10:32 pm
Current Project: Gigazilla Engine
Favorite Gaming Platforms: Dreamcast, SNES, PS2, PC
Programming Language of Choice: C/++
Location: WoFo, KY

Re: Double Buffering in OpenGL.

Post 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
Attachments
main.cpp
(1.55 KiB) Downloaded 58 times
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Double Buffering in OpenGL.

Post 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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Double Buffering in OpenGL.

Post 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 ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Post Reply