I’m back again with probably what is a small problem that I can’t seem to work out whilst undertaking my OpenGL/SDL adventure.
This program brings up a 640x480px SDL screen, but there is nothing inside it, not even a background, it is just transparent through to whatever is on the screen behind it and I have to admit, I am unsure why albeit it’s probably another silly mistake.
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
SDL_Event event;
int main (int argc, char **argv)
{
bool quit = false;
init();
draw_square(0,0,0);
while (quit == false)
{
if( event.type == SDL_QUIT )
{
quit = true;
}
}
SDL_Quit();
return 0;
}
void init ()
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640,480,32,SDL_OPENGL);
SDL_WM_SetCaption( "OpenGL Test", NULL );
glClearColor( 0, 0, 0, 0 );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, 0 , 640 , 480, 0, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
void draw_square (int x, int y, int z)
{
glTranslatef(x,y,z);
glColor4f(1.0,1.0,1.0,1.0);
glBegin(GL_QUADS);
glVertex3f(0, 0 ,0);
glVertex3f(50,0 ,0);
glVertex3f(50,50,0);
glVertex3f(0, 50,0);
glEnd();
glPushMatrix();
}
Many thanks ~ Michael
You don’t clear the screen despite setting the clear colour. You need a call like glClear( GL_COLOR_BUFFER_BIT ); to actually do the clearing.
Also, it is best to continuously render inside of the loop if you want to do anything other than static drawing… it is likely you see nothing at all because its expecting you to do double buffered drawing with this in mind and needs you to swap the buffers to see the results on screen. Typically this is done each time through the loop after all of the draw calls have been made. I believe you can do this with SDL_GL_SwapBuffers