Sign in to follow this  
dmikesell

OpenGL Simple problem

Recommended Posts

I'm having a problem with a simple OpenGL program. There is little GL code at all right now, just a framework for switching between different "scenes" and a couple of test triangles. I've zipped up the Python code and put it on my website (http://davemikesell.net/code.zip), but a print trace of the GL calls is shown below. I removed repetitions of the drawing of each triangle for brevity. The first red triangle appears as expected, but the 2nd green one does not after the scene is changed (i.e. the screen is black). I tried drawing the 2nd scene first and it drew, so it is a state problem of some kind. What am I overlooking? init menu_scene glShadeModel(GL_SMOOTH) glClearColor(0.0, 0.0, 0.0, 1.0) glClearDepth(1.0) glViewport(0, 0, WIDTH, HEIGHT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT) glMatrixMode(GL_MODELVIEW) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glColor4f(1.0, 0.0, 0.0, 1.0) glBegin(GL_TRIANGLES) glVertex3f( 0.0, 1.0, 0.0) glVertex3f(-1.0,-1.0, 0.0) glVertex3f( 1.0,-1.0, 0.0) glEnd() glFlush() pygame.display.flip() init game_scene glShadeModel(GL_SMOOTH) glClearColor(0.0, 0.0, 0.0, 1.0) glClearDepth(1.0) glViewport(0, 0, WIDTH, HEIGHT) glMatrixMode(GL_PROJECTION) glLoadIdentity() gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT) glMatrixMode(GL_MODELVIEW) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glLoadIdentity() glColor4f(0.0, 1.0, 0.0, 1.0) glBegin(GL_TRIANGLES) glVertex3f( 0.0, 1.0, 0.0) glVertex3f(-1.0,-1.0, 0.0) glVertex3f( 1.0,-1.0, 0.0) glEnd() glFlush() pygame.display.flip()

Share this post


Link to post
Share on other sites
You have a lot of repeated code, which could not only be causing the problem (with your glClear) but also means it is going to be very slow. Try something more like this:

glShadeModel(GL_SMOOTH)
glClearColor(0.0, 0.0, 0.0, 1.0)
glClearDepth(1.0)
glViewport(0, 0, WIDTH, HEIGHT)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT)
glMatrixMode(GL_MODELVIEW)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
renderScene()
glFlush()
pygame.display.flip()

And then have a renderScene function.

renderScene() {
// If you're rendering the menu do this code

blah
blah

// Else if you're rendering the scene, do this code

blah
blah
}

This way you've reduced the number of state changes, thus increasing performance, and you shouldn't have a problem any more.

Share this post


Link to post
Share on other sites
Yes, it looks like it was the repeated gluOrtho call on the projection matrix stack that was causing the problem. Thanks!

I'm still a little puzzled why it didn't work...I didn't think calls like that were cumulative.

Share this post


Link to post
Share on other sites

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