Sign in to follow this  

Can't draw polygons (SDL_OpenGL)

This topic is 3486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Straight to the problem: I can create the screen and draw it black, but I can't put a white triangle on it. I know that one doesn't always give enough of the source code to have people help him or her, so I've made the entirety of the source available on pastebin: main, SDL. Here are the snippets I think are relevant: Main:
int main ( int argc, char** argv )
{
    initSDL_GL();

    SDL_PollEvent( &SDLevent );

    // The main loop. Get cozy, you'll be here a while.
    while( SDLevent.type != SDL_QUIT )
    {
        // TESTING //
        glTranslatef( 20, 20, 0 );

        glBegin( GL_TRIANGLES );
            glColor4f( 1.0, 1.0, 1.0, 1.0 );

            glVertex3f(  0.0f,  0.0f, 0.0f );
            glVertex3f( 12.0f,  0.0f, 0.0f );
            glVertex3f(  0.0f, 12.0f, 0.0f );
        glEnd();

        glLoadIdentity();
        // \TESTING //

        // DRAW //
        glClear( GL_COLOR_BUFFER_BIT );
        drawGLScene();
        SDL_GL_SwapBuffers();

        SDL_PollEvent( &SDLevent );
    }

    // Clean up.
    SDL_Quit();
    killGLWindow();

    return 0;
}

SDL_Work:
/* Initialize everything OpenGL. */
inline void init_GL()
{
    // Enable Smooth Shading. (from NeHe)
    glShadeModel(GL_SMOOTH);

    // Make background black by default.
    glClearColor( 0.0F, 0.0F, 0.0F, 0.0F );

  // (from NeHe)
    glClearDepth(1.0f);		 // Depth Buffer Setup
	glEnable(GL_DEPTH_TEST); // Enables Depth Testing
	glDepthFunc(GL_LEQUAL);	 // The Type Of Depth Test To Do

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    glOrtho( 0, screenWidth, screenHeight, 0, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    if( getGL_Error() )
    {
        exit( -1 );
    }
}

/* Initialize SDL and all subsystems. Returning a bool to make sure it worked. */
bool initSDL_GL( const int SCREEN_WIDTH = 640, const int SCREEN_HEIGHT = 480, const int SCREEN_BPP = 32 )
{
    // INIT EVERYTHING //
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    {
        errors << "SDL Init error: " << SDL_GetError() << endl;
        exit( -1 );
    }
    errors << "SDL initialized!" << endl;

    // INIT SCREEN //
    if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
    {
        errors << "SDL Video Mode error: " << SDL_GetError() << endl;
        exit( -1 );
    }
    errors << "SDL Video mode initialized!" << endl;

    // INIT OPENGL //
    init_GL();
    errors << "OpenGL initialized!" << endl;

    return true; // Made it this far, huh?
}

inline int drawGLScene()
{
    // Clear The Screen And The Depth Buffer.
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glLoadIdentity();							// Reset The Current Modelview Matrix
	return TRUE;								// Everything Went OK
}



Share this post


Link to post
Share on other sites
glLoadIdentity() does clear the matrix, I think, but I think calling it after glEnd() means it's been saved somewhere...? At least the tutorials seem to find no problem with it--which I guess is my problem. My source pretty much follows the tutorials: a little of Lazy Foo, and a little of NeHe.

Or maybe I'm not linking something right?

"Any particular reason you are using color4f instead of 3f?"
Tutorials. Didn't know there was a 3f, but I'll use it now.

Share this post


Link to post
Share on other sites
You're drawing the triangle, and then calling drawGLScene() which immediately clears the framebuffer holding the image you just drew. Try moving the code to draw the triangle into drawGLScene so it gets called after the buffer is cleared.

Share this post


Link to post
Share on other sites
As dwahler said, your rendering your triangle and then clearing the back buffer just before flipping.

You want to be clearing the backbuffer once, then render any objects such as your triangle. After that you then flip the backbuffer.


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();
glTranslatef(0.0f, 0.0f, -10.0f);
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(0.0f, 3.0f, 0.0f);
glVertex3f(3.0f, -3.0f, 0.0f);
glVertex3f(-3.0f, -3.0f, 0.0f);
glEnd();

SDL_GL_SwapBuffers();

Share this post


Link to post
Share on other sites
OK, the problem was using two different tutorials: one for SDL_OpenGL, and one for OpenGL.

Thanks, everyone. I was calling glClear() before SDL_GL_SwapBuffers(), and that caused chaos.

BTW: Does anyone know where I can find a reference for what SDL does for me in OpenGL, and what I have to do myself? So far, I just know that it simplifies opening the window greatly, but it will no longer be my greatest option for loading surfa...textures into my programs.

Share this post


Link to post
Share on other sites
Quote:
Original post by Splinter of Chaos
but it will no longer be my greatest option for loading surfa...textures into my programs.


You can still load the surface in SDL, then grab it's pixel data and pass it to OpenGL. However - note that this can get a little tricky, since SDL is not an image manipulation library. Something like corona is better suited for image loading, since it can load and convert you a format you want (no manual conversion), and it can load (and save, for that matter) a variety of file types.

Share this post


Link to post
Share on other sites
Quote:
Original post by Splinter of Chaos
BTW: Does anyone know where I can find a reference for what SDL does for me in OpenGL, and what I have to do myself? So far, I just know that it simplifies opening the window greatly, but it will no longer be my greatest option for loading surfa...textures into my programs.


It does very little, sets the window, the context, the buffer sizes and their resolution and whether or not to use double buffering, other than that, everything else is done in OpenGL.

Besides SDL_SetVideoMode, take a look at the SDL_GL* functions here.

Share this post


Link to post
Share on other sites

This topic is 3486 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this