Results 1 to 6 of 6

Thread: How to Show model views with the keyboard or menu

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2011
    Location
    NY
    Posts
    20

    How to Show model views with the keyboard or menu

    Hi ,
    I am quite new to open GL.
    Right now I am generating different view by changing glLookAt values.
    Can some one help me to make it with the help of keyboard buttons or with menu.
    for example for top view press 'T' , for angular view press 'A'.
    The below is my code..
    ...............
    #include<iostream>
    #include<stdlib.h>
    #include <glut.h>
    using namespace std;

    void init(void)
    {
    glClearColor (1.0, 1.0, 1.0, 0.0);
    glShadeModel (GL_FLAT);
    }


    void display(void)
    {
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1,1.0);
    glLoadIdentity (); /* clear the matrix */
    /* viewing transformation */
    //...........................................
    // for front view
    //gluLookAt (0,0,12.0, 0.0, 0.0, 0.0, 0,1, 0);

    // for Angular view

    gluLookAt (-1, 10, 12.0, 2.0, 2.0, 3.0, 0.0, 1, 0.0);
    //.................................................. ....
    // for top veiw
    //gluLookAt (0,15,0, 0.0,0.0, 0.0, 0.0,0, 1.0);
    //.................................................. .

    glScalef (1,1,1); /* modeling transformation */


    glBegin(GL_QUADS);
    glColor3f (0,1,0);
    glVertex3f(10,-0.5, 4.0);
    glVertex3f(10, -0.5, -10.0);
    glVertex3f(-10,-0.5, -10.0);
    glVertex3f(-10,-0.5,4.0);

    glEnd();



    glPushMatrix();
    glColor3f (1,0.5,0.0);
    glTranslatef (5.0, 0.0, -0.5);
    glutSolidCube(1.5);
    glPopMatrix();

    glPushMatrix();

    glTranslatef (4.0, 1.0, -1.5);
    glutSolidCube(1.5);
    glPopMatrix();


    glPushMatrix();

    glTranslatef (3.0, 2.0, -2.5);
    glutSolidCube(1.5);
    glPopMatrix();

    glPushMatrix();

    glTranslatef (2.0, 3.0,-3.5);
    glutSolidCube(1.5);
    glPopMatrix();



    glPushMatrix();
    glColor3f (0.0, 1.0, 1.0);
    glTranslatef (0.0, 0.0, 0.0);
    glutSolidSphere(0.5,100,100);
    glTranslatef (1.0, 0.0, 0.0);
    glutSolidSphere(0.5,100,100);
    glTranslatef (1.0, 0.0, 0.0);
    glutSolidSphere(0.5,100,100);
    glTranslatef (1.0, 0.0, 0.0);
    glutSolidSphere(0.5,100,100);

    glPopMatrix();


    glPushMatrix();
    glTranslatef (0.5,0.866, 0.0);
    glutSolidSphere(0.5,100,100);

    glPopMatrix();


    glPushMatrix();
    glTranslatef (1.5,0.866, 0.0);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();

    glPushMatrix();
    glTranslatef (2.5,0.866, 0.0);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();

    glPushMatrix();
    glTranslatef (1.0,1.732, 0.0);
    glutSolidSphere(0.5,100,100);

    glPopMatrix();

    glPushMatrix();
    glTranslatef (2.0,1.732, 0.0);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();


    glPushMatrix();
    glTranslatef (1.5,2.598, 0.0);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();



    // THis is left side sphere balls building up



    glPushMatrix();
    glTranslatef (-1.0, 0.0, 0.0);
    glColor3f (1,0,0.0);
    glutSolidSphere(0.5,100,100);
    glTranslatef (0.0, 0.0, -1.0);
    glutSolidSphere(0.5,100,100);
    glTranslatef (0.0, 0.0, -1.0);
    glutSolidSphere(0.5,100,100);
    glTranslatef (0.0, 0.0, -1.0);
    glutSolidSphere(0.5,100,100);

    glPopMatrix();


    glPushMatrix();
    glTranslatef (-1.0, 0.866, -0.5);
    glutSolidSphere(0.5,100,100);

    glPopMatrix();

    glPushMatrix();
    glTranslatef (-1.0, 0.866, -1.5);
    glutSolidSphere(0.5,100,100);

    glPopMatrix();


    glPushMatrix();
    glTranslatef (-1.0, 0.866, -2.5);
    glutSolidSphere(0.5,100,100);

    glPopMatrix();

    glPushMatrix();
    glTranslatef (-1.0, 1.732, -1);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();

    glPushMatrix();
    glTranslatef (-1.0, 1.732, -2);
    glutSolidSphere(0.5,100,100);

    glPopMatrix();

    glPushMatrix();
    glTranslatef (-1.0, 2.598, -1.5);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();

    glutSwapBuffers();


    glFlush ();
    }


    void reshape (int w, int h)
    {
    glViewport (0,0, (GLsizei) w, (GLsizei) h);

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();

    glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
    //glOrtho(-10,20,-10,10,10,-20);
    //glOrtho(-15,20,0,0,-15,15);
    //glFrustum (-1.0, 1.0, -1.5, 1.2, 1.5, 20.0);

    glMatrixMode (GL_MODELVIEW);
    }
    int main(int argc, char** argv)
    {
    glutInit(&amp;argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    init ();

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    glutMainLoop();
    return 0;
    }

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: How to Show model views with the keyboard or menu

    Hi,
    Add a kayboard callback as follows,
    Code :
    void key(unsigned char key, int x, int y) {
    switch(key) {
      case 't':
      case 'T':			
         glLoadIdentity();
    	gluLookAt (0,15,0, 0.0,0.0, 0.0, 0.0,0, 1.0);
    	glutPostRedisplay();
      break;
      case 'a':
      case 'A':
         glLoadIdentity();
    	 gluLookAt (-1, 10, 12.0, 2.0, 2.0, 3.0, 0.0, 1, 0.0);
    	 glutPostRedisplay();
      break;
     
      case 'f':
      case 'F':
         glLoadIdentity();
    	 gluLookAt (0,0,12.0, 0.0, 0.0, 0.0, 0,1, 0);
    	 glutPostRedisplay();
       break;
    }
    }
    And in main attach this callback
    Code :
    glutKeyboardFunc(key);
    In the display function remove the glLoadIdentity and gluLookAt calls. Instead add, it into reshape handler as follows
    Code :
    void reshape (int w, int h)
    {
    glViewport (0,0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
    gluLookAt (-1, 10, 12.0, 2.0, 2.0, 3.0, 0.0, 1, 0.0);
    }
    So this is the final code.
    Code :
    #include<iostream>
    #include<stdlib.h>
    #include <gl/glut.h>
    using namespace std;
     
    void init(void)
    {
    glClearColor (1.0, 1.0, 1.0, 0.0);
    glShadeModel (GL_FLAT);
    }
     
     
    void display(void)
    {
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1,1.0);
     /* clear the matrix */
    /* viewing transformation */
    //...........................................
    // for front view 
    //gluLookAt (0,0,12.0, 0.0, 0.0, 0.0, 0,1, 0);
     
    // for Angular view
     
    //gluLookAt (-1, 10, 12.0, 2.0, 2.0, 3.0, 0.0, 1, 0.0);
    //......................................................
    // for top veiw 
    //gluLookAt (0,15,0, 0.0,0.0, 0.0, 0.0,0, 1.0);
    //...................................................
     
    glScalef (1,1,1); /* modeling transformation */
     
     
    glBegin(GL_QUADS);
    glColor3f (0,1,0);
    glVertex3f(10,-0.5, 4.0);
    glVertex3f(10, -0.5, -10.0);
    glVertex3f(-10,-0.5, -10.0);
    glVertex3f(-10,-0.5,4.0);
     
    glEnd();
     
     
     
    glPushMatrix();
    glColor3f (1,0.5,0.0);
    glTranslatef (5.0, 0.0, -0.5);
    glutSolidCube(1.5);
    glPopMatrix();
     
    glPushMatrix();
     
    glTranslatef (4.0, 1.0, -1.5);
    glutSolidCube(1.5);
    glPopMatrix();
     
     
    glPushMatrix();
     
    glTranslatef (3.0, 2.0, -2.5);
    glutSolidCube(1.5);
    glPopMatrix();
     
    glPushMatrix();
     
    glTranslatef (2.0, 3.0,-3.5);
    glutSolidCube(1.5);
    glPopMatrix();
     
     
     
    glPushMatrix();
    glColor3f (0.0, 1.0, 1.0);
    glTranslatef (0.0, 0.0, 0.0);
    glutSolidSphere(0.5,100,100);
    glTranslatef (1.0, 0.0, 0.0);
    glutSolidSphere(0.5,100,100);
    glTranslatef (1.0, 0.0, 0.0);
    glutSolidSphere(0.5,100,100);
    glTranslatef (1.0, 0.0, 0.0);
    glutSolidSphere(0.5,100,100);
     
    glPopMatrix();
     
     
    glPushMatrix();
    glTranslatef (0.5,0.866, 0.0);
    glutSolidSphere(0.5,100,100);
     
    glPopMatrix();
     
     
    glPushMatrix();
    glTranslatef (1.5,0.866, 0.0);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();
     
    glPushMatrix();
    glTranslatef (2.5,0.866, 0.0);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();
     
    glPushMatrix();
    glTranslatef (1.0,1.732, 0.0);
    glutSolidSphere(0.5,100,100);
     
    glPopMatrix();
     
    glPushMatrix();
    glTranslatef (2.0,1.732, 0.0);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();
     
     
    glPushMatrix();
    glTranslatef (1.5,2.598, 0.0);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();
     
     
     
    // THis is left side sphere balls building up
     
     
     
    glPushMatrix();
    glTranslatef (-1.0, 0.0, 0.0);
    glColor3f (1,0,0.0);
    glutSolidSphere(0.5,100,100);
    glTranslatef (0.0, 0.0, -1.0);
    glutSolidSphere(0.5,100,100);
    glTranslatef (0.0, 0.0, -1.0);
    glutSolidSphere(0.5,100,100);
    glTranslatef (0.0, 0.0, -1.0);
    glutSolidSphere(0.5,100,100);
     
    glPopMatrix();
     
     
    glPushMatrix();
    glTranslatef (-1.0, 0.866, -0.5);
    glutSolidSphere(0.5,100,100);
     
    glPopMatrix();
     
    glPushMatrix();
    glTranslatef (-1.0, 0.866, -1.5);
    glutSolidSphere(0.5,100,100);
     
    glPopMatrix();
     
     
    glPushMatrix();
    glTranslatef (-1.0, 0.866, -2.5);
    glutSolidSphere(0.5,100,100);
     
    glPopMatrix();
     
    glPushMatrix();
    glTranslatef (-1.0, 1.732, -1);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();
     
    glPushMatrix();
    glTranslatef (-1.0, 1.732, -2);
    glutSolidSphere(0.5,100,100);
     
    glPopMatrix();
     
    glPushMatrix();
    glTranslatef (-1.0, 2.598, -1.5);
    glutSolidSphere(0.5,100,100);
    glPopMatrix();
     
    glutSwapBuffers();
     
     
    glFlush ();
    }
     
     
    void reshape (int w, int h)
    {
    glViewport (0,0, (GLsizei) w, (GLsizei) h);
     
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
     
    glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
    //glOrtho(-10,20,-10,10,10,-20);
    //glOrtho(-15,20,0,0,-15,15);
    //glFrustum (-1.0, 1.0, -1.5, 1.2, 1.5, 20.0);
     
    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
    gluLookAt (-1, 10, 12.0, 2.0, 2.0, 3.0, 0.0, 1, 0.0);
    }
     
    void key(unsigned char key, int x, int y) {
     
    	switch(key) {
    		case 't':
    		case 'T':			
    			glLoadIdentity();
    			gluLookAt (0,15,0, 0.0,0.0, 0.0, 0.0,0, 1.0);
    			glutPostRedisplay();
    			break;
     
    		case 'a':
    		case 'A':
    			glLoadIdentity();
    			gluLookAt (-1, 10, 12.0, 2.0, 2.0, 3.0, 0.0, 1, 0.0);
    			glutPostRedisplay();
    			break;
     
    		case 'f':
    		case 'F':
    			glLoadIdentity();
    			gluLookAt (0,0,12.0, 0.0, 0.0, 0.0, 0,1, 0);
    			glutPostRedisplay();
    			break;
    	}
     
    }
     
    int main(int argc, char** argv)
    {
    glutInit(&amp;argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    init ();
     
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(key);
     
    glutMainLoop();
    return 0;
    }

    See if this helps,
    Mobeen
    Regards,
    Mobeen

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2011
    Location
    NY
    Posts
    20

    Re: How to Show model views with the keyboard or menu

    Thanks Mobeen ...
    I have one more question .
    what is Pespective view. As I am generating perspective view with the help of changing the glook AT values.
    Is there any standard way to generating perspective view.

    I have few more question I will really appreciate if you help me..
    1)How to add the zoom in and zoom out feature in this scene.
    2)Secondly How can I do the same top view and front view generation with the help of menu click for example menu should come on the scene which should ask the user for top view press the top view button and for front view click on Front view.

    I will really appreciate if you can help me in these issues.
    Regards

  4. #4
    Senior Member Frequent Contributor
    Join Date
    Mar 2009
    Location
    Karachi, Pakistan
    Posts
    810

    Re: How to Show model views with the keyboard or menu

    Well perspective view is a rendering of the world from any arbitrary viewpoint with perspective projection. Currently, you are generating all of your views using perspective projection (using glFrustum). If u want to generate views similar to any object modelling pakg like 3dsmax or maya, you need to set ortho graphic proj (glOrtho). for the top, front view and obly set perspective proj. for angular view. This is pretty straightforward to do. When u set the modelview matrix also setup the projection matrix.

    Like glFrustum, u may generate perspective proj using gluPerspective function. For adding zoom, you can change the fov value passed to gluPerspective function. If u want to use glFrustum, you would need to adjust the left/right/top/bottom values based on user input. I think its easier to do with fov param of gluPerspective.

    For menu handling , I would suggest u read these tutorials
    http://www.lighthouse3d.com/opengl/glut/index.php?10

    Regards,
    Mobeen
    Regards,
    Mobeen

  5. #5
    Senior Member Frequent Contributor
    Join Date
    Oct 2009
    Posts
    592

    Re: How to Show model views with the keyboard or menu

    Don't forget he can also load his matrix directly with glLoadMatrixf() or glLoadMatrixd().

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2011
    Location
    NY
    Posts
    20

    Re: How to Show model views with the keyboard or menu

    Hi Mobeen,
    I tried the way you told me but the problem is that.
    It changes only ones like .
    I need to make zoom in and out with the help of mouse scroller.
    and similarly pan movement with the help of mouse left click.

    I tried many combination but the only thing I am able to achieve is just one view. but not a continuous flow like it happens in 3d cad models.

Similar Threads

  1. The best views from -x, +x, -y, +y, -z and +z of a model
    By tiregarfio in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-12-2007, 06:01 AM
  2. Replies: 0
    Last Post: 08-02-2006, 09:24 PM
  3. How to export a model from 3D Studio MAX and show it in C++ using OpenGL
    By muhammadatharniaz in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 07-18-2003, 02:11 PM
  4. GLUI menu disables keyboard
    By in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 03-28-2003, 06:19 PM
  5. How to show a 3d model?
    By in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 06-27-2001, 06:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean