PDA

View Full Version : Background color does not change



jpummill
03-26-2002, 05:40 AM
Hi All,

Can someone tell me why my background clear color alwasys shows up as black. I am trying for white so I used:

glClearColor(1.0f, 1.0f, 1.0f, 0.0f);

Thanks for any help,
jpummill

Here is the full code listing (69 lines):

//----------------------------------------
// DRAW A SPHERE USING GLUT AND OPENGL --
//----------------------------------------

#include <gl/glut.h> // The GL Utility Toolkit (Glut) Header

void init ( GLvoid ) // Create Some Everyday Functions
{

glShadeModel(GL_FLAT); // Enable Smooth Shading
glClearColor(1.0f, 1.0f, 1.0f, 0.0f); // White Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glEnable ( GL_COLOR_MATERIAL );
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void display ( void ) // Create The Display Function
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix


glColor3f(0.0f,1.0f,0.0f);
glTranslatef(0.0f, 0.0f, -3.0f);

glutWireSphere(1, 24, 24);


glutSwapBuffers ( );
}

void reshape ( int w, int h ) // Create The Reshape Function (the viewport)
{
glViewport ( 0, 0, w, h );
glMatrixMode ( GL_PROJECTION ); // Select The Projection Matrix
glLoadIdentity ( ); // Reset The Projection Matrix
if ( h==0 ) // Calculate The Aspect Ratio Of The Window
gluPerspective ( 80, ( float ) w, 1.0, 5000.0 );
else
gluPerspective ( 80, ( float ) w / ( float ) h, 1.0, 5000.0 );
glMatrixMode ( GL_MODELVIEW ); // Select The Model View Matrix
glLoadIdentity ( ); // Reset The Model View Matrix
}

void keyboard ( unsigned char key, int x, int y ) // Create Keyboard Function
{
switch ( key ) {
case 27: // When Escape Is Pressed...
exit ( 0 ); // Exit The Program
break; // Ready For Next Case
default: // Now Wrap It Up
break;
}
}

void main ( int argc, char** argv ) // Create Main Function For Bringing It All Together
{
glutInit ( &argc, argv ); // Erm Just Write It =)
init ();
glutInitDisplayMode ( GLUT_RGB | GLUT_DOUBLE ); // Display Mode
glutInitWindowSize ( 640, 480 ); // If glutFullScreen wasn't called this is the window size
glutCreateWindow ( "NeHe's OpenGL Framework" ); // Window Title (argv[0] for current directory as title)
glutDisplayFunc ( display ); // Matching Earlier Functions To Their Counterparts
glutReshapeFunc ( reshape );
glutKeyboardFunc ( keyboard );
glutMainLoop ( ); // Initialize The Main Loop
}

mikael_aronsson
03-26-2002, 06:13 AM
Hi !

It could be that you use an alpha value of 0, try glClearColor( 1, 1, 1, 1) instead and see if that works.

Mikael

jpummill
03-26-2002, 06:41 AM
Thanks for the reply Mikael.

I tried your suggestion but it is still showing up as a black background. http://www.opengl.org/discussion_boards/ubb/frown.gif

harsman
03-26-2002, 06:57 AM
Try putting the call to init after you create the window. If you want to draw stuff continously you'll probably wan't to register a glutIdleFunc as well.

jpummill
03-26-2002, 07:39 AM
Thanks for the help.

Putting the call to init() after glutCreateWindow() did the trick. Also just moving the glClearColor() and glClearDepth() calls into the top of the display() function worked.

So the next questions is, what else in the init() function is not in the right place??? As you can see from the code, I got the example from Nehe's website and I am just modifying it to learn what happens.

chowe6685
03-26-2002, 09:22 AM
no gl Calls are valid unless you have created a hRc

harsman
03-26-2002, 10:07 AM
To elaborate a bit: All opengl calls operate on the current gl rendering context. The handling of contexts is os-dependent. GLUT doesn't create a rendering context until you create a window, so if you make gl calls before you create your window there is no current rendering context and the calls have no effect. You shouldn't move everything into the display function, the init function should contain everything that needs to be done once, stuff you need to do every frame go in the display function. Just maek sure you call init after you have created the window.

zeckensack
03-26-2002, 10:30 AM
Alert:
Window creation can be quite lazy, a lot of message bouncing may happen before the gl context becomes valid.

The bullet proof way to do it is to place this into your display function:
if (init_done) {init(); init_done=true;}

then put this line somewhere outside the function:
bool init_done=false;

The rationale behind this is that glut guarantees you that the window and the context are both valid before calling the display function.

jpummill
03-26-2002, 10:55 AM
Thanks for all the replies and good advice.

This makes the learning process easier.