PDA

View Full Version : OpenGL / SDL setup problem


Diplomtennis
2004.10.11, 11:31 PM
hi,

I just started with OpenGL & SDL and ran into the first problem right at the start. The following code is supposed to draw a triangle but the window remains black:

if ( SDL_Init(initflags) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
/* Initialize SDL */

SDL_GL_SetAttribute ( SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute ( SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute ( SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute ( SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute ( SDL_GL_DOUBLEBUFFER, 1);
//set attributes for OpenGL

info = SDL_GetVideoInfo();
video_bpp = info->vfmt->BitsPerPixel;
// get infos for bpp

window=SDL_SetVideoMode(xsize,ysize, video_bpp, videoflags);
/* Set video mode */
if (window == NULL) {
fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
SDL_Quit();
exit(2)

glClearColor (0,0,0,0);
// sets clear color to black
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// clears gl buffer
glLoadIdentity();
// resets identity

glTranslatef(0.0f,0.0f,-5.0f);
glBegin(GL_TRIANGLES);
glVertex3f(-1.0f,-1.0f, 1.0f); // Top
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right
glEnd();

SDL_GL_SwapBuffers();

I kinda copied this together and it should be exactly the source code from the SDL docs plus the Nehe tutorials and I really wonder why it is not working. My guess is that I forgot something. But what?

thanks. d

sealfin
2004.10.12, 05:20 AM
I presume that the value of videoflags is SDL_OPENGL? On second glance, I doubt that's your problem - I'd remove the call to glTranslatef() just before you start rendering the triangle.
On third glance, your coordinates for the vertices are also wrong...

glVertex3f(-1.0,-1.0, 1.0 ); // Top
glVertex3f( 1.0,-1.0, 1.0 ); // Bottom Left
glVertex3f( 1.0, 1.0, 1.0 ); // Bottom Right
...should really be...
glVertex3f( 0.0,-1.0, 0.0 ); // Top
glVertex3f( -1.0, 1.0, 0.0 ); // Bottom Left
glVertex3f( 1.0, 1.0, 0.0 ); // Bottom Right
( :blush: that's upside down; oh well, just flip the sign on the y coords...)

Otherwise, have a look a the first few of these (http://www.sealfin.com/rants/index.php?p=5) (scroll down for SDL.)

Diplomtennis
2004.10.12, 10:04 AM
I presume that the value of videoflags is SDL_OPENGL?
yes, it is.


I'd remove the call to glTranslatef() just before you start rendering the triangle.
why?


Otherwise, have a look a the first few of these (http://www.sealfin.com/rants/index.php?p=5) (scroll down for SDL.)
Actually this would have been my next question - answered now. Thanks. D.