PDA

View Full Version : help wanted...



zombie
10-26-2004, 08:51 AM
hay, guys. I got a problem regarding reshapeFcn. Once I use glutReshapeFunc(winReshapeFcn), all the object disappare. Following is my code.


#include <ostream.h>
#include "glut.h"

void main(int argc, char** argv)
{

//Initalize GLUT
glutInit(&amp;argc,argv);
initGlut("3D Representations");

//3D initalization
//Initalize display-window
initDisplayWnd3D();
//Initalize projection and view plan
initView();
//Send graphics to display window
glutDisplayFunc(DisplayFcn3D);


//reshape function
glutReshapeFunc(winReshapeFcn);

//Display everything and wait in loop
glutMainLoop();
}


GLint initGlut(char* title)
{
//window ID
GLint WndID;
//Initialize GLUT
//Set display mode
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
//Set top-left dislplay-window position
glutInitWindowPosition(250,300);
//Set display window width and height
glutInitWindowSize(400,400);
//Creat display window
WndID=glutCreateWindow(title);
//return WndID
return WndID;
}


void initView(void)
{
//Viewing coordinate origin
GLfloat x0=100.0,y0=100.0,z0=100.0;
//Look-at point
GLfloat xRef=50.0,yRef=50.0,zRef=50.0;
//View-up vector
GLfloat Vx=0.0,Vy=0.0,Vz=1.0;

//Clear display window
glClear(GL_COLOR_BUFFER_BIT);

//Set the viewing parameters
gluLookAt(x0,y0,z0,xRef,yRef,zRef,Vx,Vy,Vz);

glMatrixMode(GL_PROJECTION);
}


void initDisplayWnd3D(void)
{
//Set display-window color to black
glClearColor(0.0,0.0,0.0,0.0);
//Clear display window
glClear(GL_COLOR_BUFFER_BIT);
//Set projection parameters
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
glOrtho(-200.0,200.0,-200.0,200.0,-200.0,200.0);

}




void winReshapeFcn(GLint newWidth,GLint newHeight)
{
glViewport(0,0,newWidth,newHeight);
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
glOrtho(-200.0,200.0,-200.0,200.0,- 200.0,200.0);
glClear(GL_COLOR_BUFFER_BIT);
}



void DisplayFcn3D(void)
{

//Set parameters for a square fill area
//Set fill color to green
glColor3f(0.0,1.0,0.0);
glPolygonMode(GL_FRONT,GL_LINE);
//Wire-frame back face
glPolygonMode(GL_BACK,GL_FILL);

glBegin(GL_QUADS);
glVertex3f(0.0,0.0,0.0);
glVertex3f(150.0,0.0,0.0);
glVertex3f(150.0,150.0,0.0);
glVertex3f(0.0,150.0,0.0);
glEnd();

//Postion and display wire-frame cone
glColor3f(1.0,0.0,0.0);
glPushMatrix();
glTranslatef(100.0,-50.0,0);
glutWireCone(50.0,150.0,7,6);
glPopMatrix();

//Position and display wire-frame sphere
glColor3f(0.0,1.0,0.0);
glPushMatrix();
glTranslatef(50.0,50.0,0);
glutWireSphere(60.0,8,6);
glPopMatrix();

glFlush();
}


When I comment the glutReshapeFunc(winReshapeFcn) in the main function, every thing just works fine. So what is wrong with my winReshapeFcn?

Another thing is what kind of library you guys using? Is any difference among gl.h,glu.h and glut.h although I know glut.h includs gl.h and glu.h? Is any more convenient library, object-oriented, available?

Any suggestion and help will be appreciated.

Aeluned
10-26-2004, 09:23 AM
well, for one, your reshape and init view function leave the current matrix mode as GL_PROJECTION which means that all of the transformations you're applying in your display function are occuring to the projection matrix. You probably want to set the matrix mode to GL_MODELVIEW when you're done resizing.

as an aside:

glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
glOrtho(-200.0,200.0,-200.0,200.0,-200.0,200.0);
your first call to glMatrixMode(GL_MODELVIEW) does nothing and should be removed.

lgrosshennig
10-26-2004, 09:44 AM
Try this:


#include "glut.h"

GLint initGlut(char* title)
{
//window ID
GLint WndID;
//Initialize GLUT
//Set display mode
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
//Set top-left dislplay-window position
glutInitWindowPosition(250,300);
//Set display window width and height
glutInitWindowSize(400,400);
//Creat display window
WndID=glutCreateWindow(title);
//return WndID
return WndID;
}

void initView(void)
{
//Set the viewing parameters
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(100.0f,100.0f,100.0f,50.0f,50.0f,50.0f,0 .0f,0.0f,1.0f);
}

void initDisplayWnd3D(void)
{
//Set display-window color to black
glClearColor(0.0,0.0,0.0,0.0);
//Set projection parameters
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-200.0,200.0,-200.0,200.0,-200.0,200.0);
glMatrixMode(GL_MODELVIEW);
}

void winReshapeFcn(GLint newWidth,GLint newHeight)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//you dont want to update your projection to the new width and height?
glOrtho(-200.0,200.0,-200.0,200.0,- 200.0,200.0);
glMatrixMode(GL_MODELVIEW);
}

void DisplayFcn3D(void)
{
//clear
glClear(GL_COLOR_BUFFER_BIT);

//Set parameters for a square fill area
//Set fill color to green
glColor3f(0.0,1.0,0.0);
glPolygonMode(GL_FRONT,GL_LINE);
//Wire-frame back face
glPolygonMode(GL_BACK,GL_FILL);

glBegin(GL_QUADS);
glVertex3f(0.0,0.0,0.0);
glVertex3f(150.0,0.0,0.0);
glVertex3f(150.0,150.0,0.0);
glVertex3f(0.0,150.0,0.0);
glEnd();

//Postion and display wire-frame cone
glColor3f(1.0,0.0,0.0);
glPushMatrix();
glTranslatef(100.0,-50.0,0);
glutWireCone(50.0,150.0,7,6);
glPopMatrix();

//Position and display wire-frame sphere
glColor3f(0.0,1.0,0.0);
glPushMatrix();
glTranslatef(50.0,50.0,0);
glutWireSphere(60.0,8,6);
glPopMatrix();

glFlush();
}

void main(int argc, char** argv)
{
//Initalize GLUT
glutInit(&amp;argc,argv);
initGlut("3D Representations");

//3D initalization
//Initalize display-window
initDisplayWnd3D();

//Initalize projection and view plan
initView();

//Send graphics to display window
glutDisplayFunc(DisplayFcn3D);

//reshape function
glutReshapeFunc(winReshapeFcn);

//Display everything and wait in loop
glutMainLoop();
}

zombie
10-26-2004, 10:06 AM
thanks a lot. your code does work!

so would you like give some comments regarding how to manage matrix? I am really confused about that part.

Aeluned
10-26-2004, 10:51 AM
glMatrixMode() sets the current working matrix.
all consecutive transformations glRotate,Translate,Scale alter the last selected matrix using glMatrixMode().

your reshape function for example was leaving the last selected matrix as GL_PROJECTION (your projection matrix).

your display function after that was performing transformations on your projection matrix (the projection matrix is responsible for transforming your vertices from world space to screen space) - so who knows where your objects were ending up after that.

the projection matrix is normally set up once and then not messed around with anymore.

it's the modelview matrix you want to alter when you want to rotate something or move it around.

you can save the current state of a matrix, do some stuff to it and then restore it using a push and pop:

glMatrixMode(GL_MODELVIEW);
glPushMatrix() //save matrix on stack
glRotatef();
glTranslatef();
glALotMoreTransformations();

//draw object using altered modelview

glPopMatrix(); //restore the modelview matrix to what it was before you did all that other stuff...hope that answers some of your questions.

zombie
10-26-2004, 02:14 PM
Buddy, you help me a lot. Thanks very much.

So generally we use glMartixMode to select which matrix stack to ues, one is modelview matrix stack and the other is projection matrix stack. Is that right? And we define the projection matrix one time(at init and reshapefun) if we dont need change it anymore?

lgrosshennig
10-26-2004, 02:37 PM
Originally posted by zombie:
Is that right?Yep, but there are other stacks too.


Originally posted by zombie:
And we define the projection matrix one time(at init and reshapefun) if we dont need change it anymore?Well the projection matrix is for all operations that are related to the projection ;) (like for example glOrtho) while the modelview matrix deals with everything that is related to the position of 3d objects in your 3d world as well as the viewer/camera position.

zombie
10-26-2004, 02:40 PM
Please see the following snippet,


glPushMatrix();
glTranslatef(50.0,0.0,0.0);
glColor3f(0.0,1.0,0.0);
glBegin(GL_QUADS);
glVertex3f(0.0,0.0,0.0);
glVertex3f(50.0,0.0,0.0);
glVertex3f(50.0,50.0,0.0);
glVertex3f(0.0,50.0,0.0);
glEnd();
glPopMatrix();

glColor3f(0.0,1.0,0.0);
glBegin(GL_QUADS);
glPushMatrix();
glTranslatef(50.0,0.0,0.0);
glVertex3f(0.0,0.0,0.0);
glVertex3f(50.0,0.0,0.0);
glVertex3f(50.0,50.0,0.0);
glVertex3f(0.0,50.0,0.0);
glPopMatrix();
glEnd();
Why in the second case the translation does not work?

lgrosshennig
10-26-2004, 02:58 PM
A glTranslatef() call is not allowed between a glBegin()/glEnd() block.

Doing this will result in a GL_INVALID_OPERATION error.

The best practice in case something doesnt work as expected is to check for errors using glGetError().

Aeluned
10-28-2004, 05:22 AM
So generally we use glMartixMode to select which matrix stack to ues, one is modelview matrix stack and the other is projection matrix stack. Is that right?
actually there is only 1 matrix stack.
both the modelview matrix as well as the projection matrix get pushed onto this same stack.
this is important because if you push both matrices you have to pop them in reverse order (since they are both on the same stack and because of a stacks LIFO nature).

plasmonster
10-29-2004, 01:46 AM
So generally we use glMartixMode to select which matrix stack to ues, one is modelview matrix stack and the other is projection matrix stack. Is that right?Yes, that is correct. The GL maintains a separate stack for the projection, modelview, and texture matrices, in additon some others. This means that if you push a matrix on the stack in modelview mode, you had better be in modelview mode when you pop it.


And we define the projection matrix one time (at init and reshapefun) if we dont need change it anymore?Yup. Of couse, you could set this once each frame if you want to; it's not an expensive operation.

Deiussum
10-29-2004, 04:34 AM
Originally posted by Aeluned:
actually there is only 1 matrix stack.
both the modelview matrix as well as the projection matrix get pushed onto this same stack.
this is important because if you push both matrices you have to pop them in reverse order (since they are both on the same stack and because of a stacks LIFO nature).[/QB]Are you sure about that? There are even different stack limits for the projection and modelview stacks. If I recall correctly, the projection matrix is only guaranteed a stack depth of 2, and the modelview stack a depth of 16 or 32 or something like that. (Though implementations are allowed to increase those sizes, of course...)

Actually, let's take a look at what MSDN says:


There is a stack of matrices for each of the matrix modes. In GL_MODELVIEW mode, the stack depth is at least 32. In the other two modes, GL_PROJECTION and GL_TEXTURE, the depth is at least 2. The current matrix in any mode is the matrix on the top of the stack for that mode.

Aeluned
10-29-2004, 05:31 AM
you're right. I stand corrected :)

zombie
11-11-2004, 03:10 PM
hay, guys, I am wondering how to organize the openGL source code files. You know, I write a lot functions at separate files, but some variables need to be shared(for instance, winHeight, winWidth ect).