Perspective Projections

Using Perspective Projections

To see your objects, you need to make sure they are in the frustum.

   gluPerspective( 45.0, 1.0, 3.0, 7.0 );
   ...
   /* sometime before drawing scene */
   glTranslatef( 0, 0, -5.0 );

 

   1:  /* matrixmodes.c - draw a triangle under the effect of a rotation,
   2:   *              a translation and a scale; use the matrix stack
   3:   *              to ensure that the scene is properly redrawn.
   4:   *
   5:   *     Escape Key      - exit program
   6:   */
   7:   
   8:  #include <GL/glut.h>    /* includes gl.h, glu.h */
   9:   
  10:  #include <stdio.h>
  11:   
  12:  #include "axes.h"
  13:   
  14:  /*  Function Prototypes  */
  15:   
  16:  GLvoid  initgfx( GLvoid );
  17:  GLvoid  keyboard( GLubyte, GLint, GLint );
  18:  GLvoid  drawScene( GLvoid );
  19:   
  20:  void drawTriangle( GLfloat red, GLfloat green, GLfloat blue );
  21:   
  22:  void printHelp( char * );
  23:   
  24:  /* Global Definitions */
  25:   
  26:  #define KEY_ESC        27      /* ascii value for the escape key */
  27:   
  28:  void
  29:  main( int argc, char *argv[] )
  30:  {
  31:     GLsizei width, height;
  32:   
  33:     glutInit( &argc, argv );
  34:   
  35:     width = glutGet( GLUT_SCREEN_WIDTH ); 
  36:     height = glutGet( GLUT_SCREEN_HEIGHT );
  37:     glutInitWindowPosition( (width / 2) + 2, height / 4 );
  38:     glutInitWindowSize( width / 2, height / 2 );
  39:     glutInitDisplayMode( GLUT_RGBA );
  40:     glutCreateWindow( argv[0] );
  41:   
  42:     initgfx();
  43:   
  44:     glutKeyboardFunc( keyboard );
  45:     glutDisplayFunc( drawScene ); 
  46:   
  47:     printHelp( argv[0] );
  48:   
  49:     glMatrixMode( GL_PROJECTION );
  50:     gluPerspective( 45.0, (GLdouble) width/ (GLdouble)height, 3.0, 7.0 );
  51:     glMatrixMode( GL_MODELVIEW );
  52:   
  53:     glutMainLoop();
  54:  }
  55:   
  56:  void
  57:  printHelp( char *progname )
  58:  {
  59:     fprintf(stdout, 
  60:             "\n%s - demonstrates use of matrix modes and stacks\n\n"
  61:             "Escape Key         - exit the program\n\n",
  62:             progname);
  63:  }
  64:   
  65:  GLvoid
  66:  initgfx( GLvoid )
  67:  {
  68:     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  69:     glShadeModel( GL_FLAT );
  70:  }
  71:   
  72:  GLvoid 
  73:  keyboard( GLubyte key, GLint x, GLint y )
  74:  {
  75:     switch (key) {
  76:     case KEY_ESC:   /* Exit whenever the Escape key is pressed */
  77:         exit(0);
  78:     }
  79:  }
  80:   
  81:  GLvoid
  82:  drawTriangle( GLfloat red, GLfloat green, GLfloat blue )
  83:  {
  84:     glColor3f( red, green, blue );
  85:     glBegin( GL_TRIANGLES );
  86:         glVertex2f( -0.5, -0.5 );
  87:         glVertex2f( 0.5, -0.5 );
  88:         glVertex2f( 0.0, 0.5 );
  89:     glEnd();
  90:  }
  91:   
  92:  GLvoid
  93:  drawScene( GLvoid )
  94:  {
  95:     glClear( GL_COLOR_BUFFER_BIT );
  96:   
  97:     glPushMatrix();
  98:         /* Translate the origin so that it is between the near and far
  99:          *    clipping planes.
 100:          */
 101:         glTranslatef( 0.0, 0.0, -5.0 );
 102:   
 103:         /* draw a black triangle centered at the origin */
 104:         drawTriangle( 0.0, 0.0, 0.0 );
 105:   
 106:         /* Draw x and y axes to show the origin */
 107:         XYaxes();
 108:   
 109:         /* move over a bit and draw another triangle */
 110:         glTranslatef( 0.5, 0.0, 0.0 );
 111:         drawTriangle( 0.3, 0.3, 0.3 ); 
 112:   
 113:         /* move over a bit and draw a rotated triangle */
 114:         glTranslatef( 0.5, 0.0, 0.0 );
 115:   
 116:         /* Rotate 15 degrees counter-clockwise around the positive Z axis */
 117:         glRotatef( -15.0, 0.0, 0.0, 1.0 );
 118:         drawTriangle( 0.7, 0.7, 0.7 );
 119:   
 120:         /* move over a bit and draw a scaled triangle */
 121:         glTranslatef( 0.5, 0.0, 0.0 );
 122:   
 123:         /* Scale the triangle by one-half in all dimensions */
 124:         glScalef( -0.5, 0.5, 0.5 );
 125:         drawTriangle( 1.0, 1.0, 1.0 );  /* draw green triangle */
 126:         XYaxes();
 127:   
 128:     glPopMatrix();
 129:   
 130:     glFlush();
 131:  }

你可能感兴趣的:(Perspective Projections)