PDA

View Full Version : Blank Window :(



07-25-2003, 01:24 PM
I'm a complete beginner on OpenGL, so bare with me http://www.opengl.org/discussion_boards/ubb/frown.gif

I got this piece of code down here, and I was supposed to add another object to it, smaller, behind the pyramid:

{******************************}
void RenderScene ()

{
glClear( GL_COLOR_BUFFER_BIT );
pyramid();
glutSwapBuffers();
}

void initFunc ()

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

glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0,2.0,-2.0,2.0,8.0,12.0);

glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glEnable(GL_CULL_FACE);
}

void idleFunc()

{
if ( rotateX ) {
glRotatef( 0.7f, 1.0f, 0.0f, 0.0f );
glutPostRedisplay();
} else if ( rotateY) {
glRotatef( 0.7f, 0.0f, 1.0f, 0.0f );
glutPostRedisplay();
}
}
{******************************}
What I did was modify the renderScene display function this way:

void RenderScene ()
{
glClear( GL_COLOR_BUFFER_BIT );
pyramid();
glPushMatrix;
glScalef(0.75,0.75,0.75);
glTranslatef(0,0,-3);
pyramid();
glPopMatrix;
glutSwapBuffers();
}

However, after the first frame, the screen goes blank http://www.opengl.org/discussion_boards/ubb/frown.gif I assume it's a problem with the eye/modelview matrix, but I'm too dumb to figure it ^^"

Oh BTW, the frames are updated by the idleFunc. rotateX/Y are true according to mouse activity (that is the mouse rotates the scene).

Major thanks for any help http://www.opengl.org/discussion_boards/ubb/smile.gif

nexusone
07-25-2003, 05:15 PM
What happens is the matrix is not being cleared and each time you call the renderscene function the matrix is increased from it last state.

Add glLoadIdentity() after your clear statment in the renderscene function

Also you will need to move the gluLookAt function from the init routine also into the the renderscene.

Also in your idle function, change to this:

void idelfunc( void )
{
glutPostRedisplay();
}

Move the if statments and glRotate into the renderscene routine.

There location will be based on what you are trying to rotate.


Here is a bit of my display rendering code



glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen

glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work
glLoadIdentity(); // Clear the matrix
glOrtho(-8.0, 8.0, -8.0, 8.0, 0.0, 30.0); // Setup an Ortho view
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
glLoadIdentity(); // Clear the model matrix



[This message has been edited by nexusone (edited 07-25-2003).]

Leather Shield
07-26-2003, 02:03 AM
I haven't tested your suggestion yet, but let me ask a few questions, perhaps this whole thing gets clearer in my mind.


Originally posted by nexusone:
What happens is the matrix is not being cleared and each time you call the renderscene function the matrix is increased from it last state.
Add glLoadIdentity() after your clear statment in the renderscene function

But if I want the rotations to be cumulative, wouldn't the loading of Identity cancel previous rotations? BTW the first sample of code I posted works nicely, I can see the object and rotate around it with the mouse. It's just when I insert the second object that it gets messed up. Since I was using a PushMatrix and later a PopMatrix to draw this object, I thought it wouldn't affect the ModelMatrix afterwards, thus the camera would be defined by the matrix "below" the popped one... why isn't it so?


Also you will need to move the gluLookAt function from the init routine also into the the renderscene.
Hmm does transformations (ie my rotate) apply over the camera or the objects to be drawn? Or both? Nothing to do with the matter, but I'm just curious (and dumb)...


Also in your idle function, change to this:
void idelfunc( void )
{
glutPostRedisplay();
}
Move the if statments and glRotate into the renderscene routine.

I'd be doing unnecessary rendering, but ok if that's the easiest way out =)

Thanks again http://www.opengl.org/discussion_boards/ubb/smile.gif

[This message has been edited by Leather Shield (edited 07-26-2003).]

Leather Shield
07-26-2003, 02:28 AM
Hmm interesting, I've done something else that worked... look:




void RenderScene ()
{
glClear( GL_COLOR_BUFFER_BIT );
pyramid();
glPushMatrix;
glScalef(0.5,0.5,0.5);
glTranslatef(0,0,-3);
pyramid();
glTranslatef(0,0,3);
glScalef(2.,2.,2.);
glPopMatrix;
glutSwapBuffers();
}


That is, now I'm "undoing" the transformations after drawing the object. So I assume even though I was popping the matrix I used to draw the object, the camera was still affected by the transformations, because any transformation on the modelmatrix stack will be reflected on the camera?
If so, is there a way I can mess with the model stack to draw the object AND not affect the camera? I don't want to have to "undo" all my transformations =)

Sorry if i'm ignoring your solution for now, but even if it solves my problem, I still need to understand all this stuff if I want to make any progress...

[This message has been edited by Leather Shield (edited 07-26-2003).]

nexusone
07-26-2003, 05:30 AM
Here is a problem with your aproach, while a lot of people so something like for simple demo of how to rotate a cube. It is not practical in real teams of creating a scene with many objects.

Yes the matrix is accumalated, but also the matrix setting effects all objects.

Instead of trying to use the matrix accumalation to keep track of you objects rotation you can use a variable.

int object_1_rotation_x
int object_2_rotation_y


gluLookAt effects the model matrix, works like calling a few glRotate's and glTranslates. But if you call load Identity after gluLookAt, you undo it.

Look back at my rendering routine, I clear the matrix, then set the gluLookAt.

Matrix operations work in that they are first in, first out.


glpushmatrix(); Saves current matrix
pryamid(); We draw something maybe do some glTranslates and rotations on this object.
glpopmatrix(); we restore the matrix to that last glpushmatrix, this saves our gluLookat matrix changes, remember the matrix calls are accumalated.

repeat the push/pop matrix for each object we draw and make rotation and translations.
This way each object's rotations and translations don't effect each other and they all are drawn with the gluLookat matrix settings.



You would get a better feel for how things work by looking at some basic example programs.
www.angelfire.com/linux/nexusone/ (http://www.angelfire.com/linux/nexusone/) my website with example on matrix usage...

also nehe.gamedev.net