next up previous contents
Next: Introduction to KDevelop Up: The Nude OpenGL Previous: The Nude OpenGL   Contents

Viewing: ModelView/Projection Transformation/Matrices and Viewport Transformation

The Chapter 3 of The Red Book clarifies the relationship among modeling, viewing, projection, viewport transformations that often confuse the OpenGL beginners. Please be patient in going through following steps one by one:

  1. Spend at least 5 minutes on Figure3-1 in Chapter 3 of The Red Book at: http://fly.cc.fer.hr/~unreal/theredbook/chapter03.html
  2. Look at Figure3-2 while reading only the one paragraph above Figure3-2 and the four paragraphs thereafter.
    If not understanding some part, just skip it.
  3. Copy the following Makefile:
    		INC=-I/usr/local/include -I/usr/X11R6/include -I/usr/include/Xft2 -O2
    		LIB=-L/usr/local/lib -L/usr/X11R6/lib -L$(QTDIR)/lib
    		LIBS=-lGL -lX11 -lGLU -lXmu -lm -lXext -lglut -lXi
    		.c:
    	        	g++ $@.c $(LIB) $(INC) -v -o $@  $(LIBS)
    
  4. Copy the following code and name it as cube.c
    		#include <GL/glut.h>
    		#include <stdlib.h>
    		
    		void init(void) 
    		{
    		   glClearColor (0.0, 0.0, 0.0, 0.0);
    		   glShadeModel (GL_FLAT);
    		}
    		
    		void display(void)
    		{
    		   glClear (GL_COLOR_BUFFER_BIT);
    		   glColor3f (1.0, 1.0, 1.0);
    		   glLoadIdentity ();             /* clear the matrix */
    		                                  /* viewing transformation  */
    		   gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    		   glScalef (1.0, 2.0, 1.0);      /* modeling transformation */ 
    		   glutWireCube (1.0);
    		   glFlush ();
    		}
    		
    		void reshape (int w, int h)
    		{
    		   glViewport (0, 0, (GLsizei) w, (GLsizei) h); /* viewport transformation */
    		   glMatrixMode (GL_PROJECTION);
    		   glLoadIdentity ();
    		   glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 50.0); /* projection transformation */
    		   glMatrixMode (GL_MODELVIEW);
    		}
    	
    		void keyboard(unsigned char key, int x, int y)
    		{
    		   switch (key) {
    		      case 27:
    		         exit(0);
    		         break;
    		   }
    		}
    
    		int main(int argc, char** argv)
    		{
    		   glutInit(&argc, argv);
    		   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    		   glutInitWindowSize (500, 500); 
    		   glutInitWindowPosition (100, 100);
    		   glutCreateWindow (argv[0]);
    		   init ();
    		   glutDisplayFunc(display); 
    		   glutReshapeFunc(reshape);
    		   glutKeyboardFunc(keyboard);
    		   glutMainLoop();
    		   return 0;
    		}
    
  5. Run the code after make cube.
    And change some values in those transformation functions to see the effect.
  6. Now, once more, Look at Figure3-2 while reading only the one paragraph above Figure3-2 and the four paragraphs thereafter.
    Please remember by heart and mind:
  7. Try to do those parts marked Try This in Chapter 3


next up previous contents
Next: Introduction to KDevelop Up: The Nude OpenGL Previous: The Nude OpenGL   Contents
Beifang Yi 2003-09-24