In-Class Exercise:  Drawing Points  SOLUTION


UNIX Version  (Windows Below)
/*#####################################*/
/*Description:   program to draw three red lines     */
/*   to a GLUT window  that is 500x500 pixels          */
/*Author:  L. Grewe                                                    */
/*Date: 1-1-99                                                            */
/*#####################################*/
 

#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>

#include <stdio.h>
#include <math.h>

/*Function prototypes*/
GLvoid display(GLvoid);
GLvoid myinit(GLvoid);
 
 

     void main(int argc, char **argv)  
     {  
       
         glutInit(&argc,argv); /*init.  GLUT window sys.*/  

         /*there will be a single buffered  window displaying RGB */ 
         glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);   

         glutInitWindowSize(500,500);   /*size in pixels  */
         glutInitWindowPosition(0,0);   /* located lower-left of screen */ 

         /*Create the window using the setup from the previous glut calls*/  
         glutCreateWindow("Lines Example");  /*title=Lines Example*/  

         /*tell system what function will be in charge of displaying*/  
         glutDisplayFunc(display);  
 

         /*Call another function to further initialize window contents*/  
         myinit();  

         /*this call causes to go in infinite loop waiting for window i/o */  
         glutMainLoop();  

     }  
       
       
       
        

     /*#####################################*/  
     /*myinit:  this function clears the background of the */  
     /*  graphics system and sets up the drawing color to */  
     /*  red.  Finally, viewing parameters are set.             */  
     /*#####################################*/  
     GLvoid myinit(GLvoid)  
     {  

          /*white background  */
          glClearColor(1.0,1.0,1.0,0.0);  

         /*drawing color red  */
         glColor3f(1.0,0.0,0.0);  

         /*Viewing Configuration  */
         glMatrixMode(GL_PROJECTION);  /*says will specify project.  */
         glLoadIdentity();   /*load identity matrix.*/  
         gluOrtho2D(0.0,500.0, 0.0, 500.0);  /*specifies Othographic proj*/ 

        
        /*Shift to Model Specification Model  */
        glMatrixMode(GL_MODELVIEW);  
     }  
 
 
 

     /*#####################################*/  
     /*display:  this function draws three lines to the screen*/  
     /*#####################################*/  
     GLvoid display(GLvoid)  
     {  

          glClear(GL_COLOR_BUFFER_BIT); /*clear screen*/
          glBegin(GL_LINES);  
             glVertex2f(100.0, 100.0); /*Line 1*/  
             glVertex2f(400.0, 400.0);  
             glVertex2f(100.0, 400.0); /*Line2 */
             glVertex2f(400.0, 100.0);  
             glVertex2f(100.0, 250.0); /*Line 3*/  
             glVertex2f(400.0, 250.0);  
           glEnd();  
        

           glFlush();  /*not needed but asks to draw ASAP.*/  

     }  
 
 
 
 

     
 

 
 
 

Windows Version
/*#####################################*/
/*Description:   program to draw three red lines     */
/*   to a GLUT window  that is 500x500 pixels          */
/*Author:  L. Grewe                                                    */
/*Date: 1-1-99                                                            */
/*#####################################*/
 

#include <windows.h>

#include <GL\Gl.h>
#include <GL\glut.h>
#include <stdlib.h>
 

#include <stdio.h>
#include <math.h>
 
 

/*Function prototypes*/
GLvoid display(GLvoid);
GLvoid myinit(GLvoid);
 
 

     void main(int argc, char **argv)  
     {  
       
         glutInit(&argc,argv); /*init.  GLUT window sys.*/  

         /*there will be a single buffered  window displaying RGB */ 
         glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);   

         glutInitWindowSize(500,500);   /*size in pixels  */
         glutInitWindowPosition(0,0);   /* located lower-left of screen */ 

         /*Create the window using the setup from the previous glut calls*/  
         glutCreateWindow("Lines Example");  /*title=Lines Example*/  

         /*tell system what function will be in charge of displaying*/  
         glutDisplayFunc(display);  
 

         /*Call another function to further initialize window contents*/  
         myinit();  

         /*this call causes to go in infinite loop waiting for window i/o */  
         glutMainLoop();  

     }  
       
       
       
        

     /*#####################################*/  
     /*myinit:  this function clears the background of the */  
     /*  graphics system and sets up the drawing color to */  
     /*  red.  Finally, viewing parameters are set.             */  
     /*#####################################*/  
     GLvoid myinit(GLvoid)  
     {  

          /*white background  */
          glClearColor(1.0,1.0,1.0,0.0);  

         /*drawing color red  */
         glColor3f(1.0,0.0,0.0);  

         /*Viewing Configuration  */
         glMatrixMode(GL_PROJECTION);  /*says will specify project.  */
         glLoadIdentity();   /*load identity matrix.*/  
         gluOrtho2D(0.0,500.0, 0.0, 500.0);  /*specifies Othographic proj*/ 

        
        /*Shift to Model Specification Model  */
        glMatrixMode(GL_MODELVIEW);  
     }  
 
 
 

     /*#####################################*/  
     /*display:  this function draws three lines to the screen*/  
     /*#####################################*/  
     GLvoid display(GLvoid)  
     {  

          glClear(GL_COLOR_BUFFER_BIT); /*clear screen*/
          glBegin(GL_LINES);  
             glVertex2f(100.0, 100.0); /*Line 1*/  
             glVertex2f(400.0, 400.0);  
             glVertex2f(100.0, 400.0); /*Line2 */
             glVertex2f(400.0, 100.0);  
             glVertex2f(100.0, 250.0); /*Line 3*/  
             glVertex2f(400.0, 250.0);  
           glEnd();  
        

           glFlush();  /*not needed but asks to draw ASAP.*/  

     }