Reputation:
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
Upvotes: 3
Views: 726
Reputation: 162269
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
SDL_Event event;
int main (int argc, char **argv)
{
bool quit = false;
You should really call SDL_Init here, not delegating it into some init function. Otherwise somebode (you?) may change when init gets called and breaks your program. Really, your main function is your init function. Window creation should happen in a function called create_window
or something like that.
init();
Technically calling draw_square, or any drawing function outside the event loop calls for trouble. The effect you see: Well that happens if your window gets exposed (initially shown), but any events informing the program, that the contents need be updated, because they were overwritten, have no effect. Always call drawing functions from within the event loop. For static draws in reaction to draw events, for animated scenes in the idle section.
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 );
This is the most common newbie misconception, that OpenGL is "initialized". The following calls are meant to be called for scene setup, i.e. at the beginning of the drawing function.
You also lack:
So those should not be called here
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();
The next line is a big WTF?!
glPushMatrix();
Every matrix stack push must be balanced with a pop operationg (glPopMatrix). However pushing the matrix only makes sense if it is followed by some transformation. Which it isn't. If you called draw_square in a loop, you'd exhaust the maximum matrix stack depth within between 32 to 128 iteration (more likely on the lower numbers).
}
Upvotes: 0
Reputation: 3113
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
Upvotes: 4
Reputation: 25177
You passed 0 for the alpha in the call to glClearColor. Try "1".
That makes the entire screen transparent. I wouldn't have expected that to actually work, though, so good to know. :)
Upvotes: 0