PDA

View Full Version : How to Show model views with the keyboard or menu



Scorpio
01-30-2011, 08:34 PM
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;
}

mobeen
01-30-2011, 08:47 PM
Hi,
Add a kayboard callback as follows,


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


glutKeyboardFunc(key);

In the display function remove the glLoadIdentity and gluLookAt calls. Instead add, it into reshape handler as follows


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.


#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

Scorpio
01-30-2011, 09:13 PM
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

mobeen
01-30-2011, 09:41 PM
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

ugluk
01-31-2011, 01:30 AM
Don't forget he can also load his matrix directly with glLoadMatrixf() or glLoadMatrixd().

Scorpio
01-31-2011, 06:29 PM
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.