Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: help wanted...

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2004
    Posts
    8

    help wanted...

    hay, guys. I got a problem regarding reshapeFcn. Once I use glutReshapeFunc(winReshapeFcn), all the object disappare. Following is my code.

    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.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Feb 2004
    Location
    Long Island, New York
    Posts
    586

    Re: help wanted...

    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:
    Code :
    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.

  3. #3
    Junior Member Regular Contributor
    Join Date
    Mar 2000
    Location
    Germany
    Posts
    200

    Re: help wanted...

    Try this:

    Code :
    #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();
    }

  4. #4
    Junior Member Newbie
    Join Date
    Oct 2004
    Posts
    8

    Re: help wanted...

    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.

  5. #5
    Advanced Member Frequent Contributor
    Join Date
    Feb 2004
    Location
    Long Island, New York
    Posts
    586

    Re: help wanted...

    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:
    Code :
    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.

  6. #6
    Junior Member Newbie
    Join Date
    Oct 2004
    Posts
    8

    Re: help wanted...

    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?

  7. #7
    Junior Member Regular Contributor
    Join Date
    Mar 2000
    Location
    Germany
    Posts
    200

    Re: help wanted...

    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.

  8. #8
    Junior Member Newbie
    Join Date
    Oct 2004
    Posts
    8

    Re: help wanted...

    Please see the following snippet,

    Code :
    	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();
    Code :
            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?

  9. #9
    Junior Member Regular Contributor
    Join Date
    Mar 2000
    Location
    Germany
    Posts
    200

    Re: help wanted...

    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().

  10. #10
    Advanced Member Frequent Contributor
    Join Date
    Feb 2004
    Location
    Long Island, New York
    Posts
    586

    Re: help wanted...

    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •