Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Orthographic Projection

 

Graphic is not distorted - ideal for architectural drawings

 

glOrtho() takes 6 parameters:

    GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble near, GLdouble far
   left and right are x-axis coordinate bounds
   bottom and top are the y-axis coordinate bounds
   near and far are the z-axis coordinate bounds

gluOrtho2D()

 

    is just a special case of glOrtho() which automatically sets near and far (z-axis coordinate bounds) to be between -1.0 and 1.0.

Purpose of the functions

 

    These functions allow you to set up your own coordinate system for whatever drawing you want to do.

    Instead of drawing in actual screen coordinates, you would be drawing in your coordinate system, as you specified.

For example:


    Suppose you wanted to to draw a large square centered in the middle of a screen with the coordinate system specified as

     glOrtho(-30.0, 30.0, -30.0, 30.0, 30.0, -30.0);


    Here''s howyou could do it

     glBegin(GL_QUADS);
        glVertex3f(-25.0, -25.0, 0.0); //lower-left, z-axis at origin
        glVertex3f(25.0, -25.0, 0.0); //lower-right
        glVertex3f(25.0, 25.0, 0.0); //upper-right
        glVertex3f(-25.0, 25.0, 0.0); //upper-left
    glEnd();

    This would leave a 5 unit space around the square, because the bounds of the coordinate system extended between -30 and 30 for every axis.

 

Example Using Code

 

#include "stdafx.h"

#include <GL/glut.h>

 

void init(void)

{

   glClearColor (0.0, 0.0, 0.0, 0.0);

   glShadeModel (GL_FLAT);

}

 

void draw_triangle(void)   //Triangle definition - called numerous times

{

   glBegin (GL_LINE_LOOP);

      glVertex2f(0.0, 25.0);

      glVertex2f(25.0, -25.0);

      glVertex2f(-25.0, -25.0);

   glEnd();

}

void draw_line(void) //WHAT DOES THIS DO?

{

      glBegin (GL_LINES);

         glVertex2f(25.0, 0.0);

         glVertex2f(25.0, 50.0);

    glEnd();

}

 

void display(void)

{

   glClear (GL_COLOR_BUFFER_BIT);

   glColor3f (1.0, 1.0, 1.0);

 

   glLoadIdentity ();

   glColor3f (1.0, 1.0, 1.0);

   draw_triangle ();             //draw 1st triangle, centered at origin (default)

   draw_line();     //CALLING DRAW LINES FUNCTION

 

   glEnable (GL_LINE_STIPPLE);

   glLoadIdentity ();

   glTranslatef (-20.0, 0.0, 0.0);     //translate origin to x,y,z position listed

   draw_triangle ();         //draw second triangle, centered at x = -20, y = 0, z = 0

 

   glLoadIdentity ();

   glScalef (1.5, 0.5, 1.0);           //scale the triangle

   draw_triangle ();

 

   glLoadIdentity ();

   glRotatef (90.0, 0.0, 0.0, 1.0);   //rotate the triangle 90 degrees counter clockwise about z axis

   draw_triangle ();

   glDisable (GL_LINE_STIPPLE);

 

   glFlush ();

}

 

//The meaning of this code segment will be covered next week

void reshape (int w, int h)

{

   glViewport (0, 0, (GLsizei) w, (GLsizei) h);

   glMatrixMode (GL_PROJECTION);

   glLoadIdentity ();

   if (w <= h)

      glOrtho (-50.0, 50.0, -50.0*(GLfloat)h/(GLfloat)w,

         50.0*(GLfloat)h/(GLfloat)w, -1.0, 1.0);

   else

      glOrtho (-50.0*(GLfloat)w/(GLfloat)h,

         50.0*(GLfloat)w/(GLfloat)h, -50.0, 50.0,-1.0, 1.0);

   glMatrixMode(GL_MODELVIEW);

}

 

int main(int argc, char** argv)

{

   glutInit(&argc, argv);

   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

   glutInitWindowSize (500, 500);

   glutInitWindowPosition (100, 100);

   glutCreateWindow ("Translate and Rotate");

   init ();

   glutDisplayFunc(display);

   glutReshapeFunc(reshape);

   glutMainLoop();

   return 0;

}