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

Viewing

 

Examples

 

Chapter Notes

1: Translation and Camera Position

2: Perspective Viewing and Camera Position

3: Frustum Parameters

4: Perspective Viewing with Axes

5. Loops, Point Size, Separate Function for Primitives

6. Depth Buffer

Exercises

Trouble Shooting Transformations

Perspective and Orthographic

Example 4

Perspective Viewing With Axes (more on scientific plotting later)

#include <GL/glut.h>

#include <stdlib.h>

void init(void)

{

   glClearColor (0.0, 0.0, 0.0, 0.0);     //background color: black

}

void display(void)

{

   glClear (GL_COLOR_BUFFER_BIT);

   glColor3f (1.0, 0.0, 1.0);     //foreground color: 

   glLoadIdentity ();             

   //location of camera: position, where looking, up direction

   gluLookAt (0.0, 5.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);  //viewing transformation

 

   glLineWidth(1.0);

   glColor3f(1.0, 1.0, 1.0);

   glBegin(GL_LINES);

     glVertex3f(0.0, 0.0, 0.0);

     glVertex3f(5.0, 0.0, 0.0);

             glVertex3f(0.0, 0.0, 0.0);

             glVertex3f(0.0, 5.0, 0.0);

             glVertex3f(0.0, 0.0, 0.0);

             glVertex3f(-5.0, -5.0, 0.0);

   glEnd();

 

   glutWireSphere (0.5, 16, 16);

   glTranslatef(5.0, 0.0, -5.0);       

 

   glColor3f (0.0, 0.0, 1.0);

   glutWireSphere (0.5, 16, 16);   

   glTranslatef(10, 0.0, -20.0);

 

   glFlush ();   //cause above code to execute

}

void reshape (int w, int h)

{

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

   glMatrixMode (GL_PROJECTION);

   glLoadIdentity ();

   //glFrustum(left, right, bottom, top, near, far)

   glFrustum (-1.0, 1.0, -1.0, 1.0, 1.0, 25.0); 

   glMatrixMode (GL_MODELVIEW);

}

int main(int argc, char** argv)

{

   glutInit(&argc, argv);     //initialize GLUT

   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);     

   glutInitWindowSize (500, 500);        //width and height of window in pixels

   glutInitWindowPosition (100, 100);   //location of window in pixels

   glutCreateWindow ("Perspective viewing with Axes");     

   init ();    //call init function

   glutDisplayFunc(display);                //call display function

   glutReshapeFunc(reshape);          

   glutMainLoop();                             //start things - must be last item

   return 0;

}

5. Loops, Point Size, Separate Function for Primitives

#include <GL/glut.h>                 

void init()

{

    glClearColor (0.0, 0.0, 0.0, 0.0);       

    glShadeModel (GL_FLAT);                  

}

doPrimitiveOne()                                            //separate function for handling the primitive

{

            glPointSize(4);                                  //setting point size of first line

            glColor3f (1.0, 0.0, 0.0);                     //setting color for first line

            for(float i = 10.0; i<600.0;i = i + 5.0)

    {

       glBegin(GL_POINTS);    

        glVertex2f(i,i);                                      //plotting a straight line, y = x

       glEnd();

    }

            glPointSize(2);                                //setting point size of second line

            glColor3f (0.0, 1.0, 0.0);                   //setting color for second line 

    for(float j = 0; j<600; j = j + 5.0)                //plotting a straight horizontal line

    {

       glBegin(GL_POINTS);    

        glVertex2f(j,300);                                 //plotting a straight line, y = x

       glEnd();

    }

}

void display()                                              //called by GLUT DisplayFunc

{

    glClear (GL_COLOR_BUFFER_BIT);

    doPrimitiveOne();                                    //calling separate function to handle the primitive

    glFlush ();   

}

void reshape (int w, int h)                            //called initially and each time the window is resized

{

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

    glMatrixMode (GL_PROJECTION);

    glLoadIdentity ();

    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);

}

int main(int argc, char** argv)   

{

    glutInit(&argc, argv);             

    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);  

    glutInitWindowSize (600, 600);

    glutInitWindowPosition (100,100);

    glutCreateWindow ("Loops, Lines, Point Size, Separate Function for Primitives");

    init ();                                      //Note 1 below

    glutDisplayFunc(display);          //Note 2 below

    glutReshapeFunc(reshape);       //Note 3 below

    glutMainLoop();                        // Note 4 below

    return 0;

}

Review

Note 1: init();

Used to initialize parameters - the 2 listed are covered later in the course

 

Note 2: glutDisplayFunc(display);

Whenever GLUT determines that the contents of the window need to be redisplayed, this function is executed.

Therefore, you should place all the routines you need to redraw the scene in this function - or calls to needed functions as illustrated above

 

Note 3: glutReshapeFunc(reshape);

Indicates what action should be taken when the window is resized

 

Note 4: glutMainLoop();

This should be placed last. All windows are shown and all rendering to those windows is executed.