OpenGL / SDL setup problem

Diplomtennis
Unregistered
 
Post: #1
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:

Code:
    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
Quote this message in a reply
Moderator
Posts: 697
Joined: 2002.04
Post: #2
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...
Code:
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...
Code:
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 (scroll down for SDL.)

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
Diplomtennis
Unregistered
 
Post: #3
Quote:I presume that the value of videoflags is SDL_OPENGL?
yes, it is.


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


Quote:Otherwise, have a look a the first few of these (scroll down for SDL.)
Actually this would have been my next question - answered now. Thanks. D.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  OpenGL Alpha Channel Problem Moganza 1 2,888 Jan 19, 2013 08:25 AM
Last Post: sealfin
  OpenGL Pixel Buffer Object setup issue dotbianry 2 2,923 Jan 6, 2013 11:03 AM
Last Post: dotbianry
  OpenGL ES2 matrix setup (humbly crawling back) Fenris 2 6,355 Aug 31, 2011 06:47 AM
Last Post: Fenris
  iPad, OpenGL ES, and XCode Instruments problem! Bandit 0 3,833 Dec 13, 2010 01:21 PM
Last Post: Bandit
  OpenGL global and local coordinate problem. mikey 4 3,803 May 26, 2009 08:48 AM
Last Post: mikey