INT32GP Graphics Programming

Some students have reported they were unable to download freecommandLinetools.exe from Borland. This is probably a temporary problem.

Window Size and Position

Use the program you were working on in the last tutorial, square.c, and specify the size and position of the GLUT window.

  1. Place the following code in the global section of the program.
    /*
    ** manage GLUT window
    */
    int width  = 300;
    int height = 300;
    int xPosition = -1;
    int yPosition = -1;
    
  2. Add the following highlighted code to the function setUpGLUT. The position of the code is important.
    void setUpGLUT(int argc,char ** argv){
       glutInit(&argc,argv);
       glutInitWindowSize(width, height);
       glutInitWindowPosition(xPosition, yPosition);
       glutCreateWindow("Square");
       /*
       ** Register callbacks.
       */
       glutDisplayFunc(display);
       glutKeyboardFunc(keyboard);
    }
    
  3. The values for width, height, xPosition, yPosition are the default values, so there will be no change in the appearance of the application. What is the significance of the negative values for xPosition, yPosition?
  4. Change the values of the global variables. Use positive values. Observe what happens.
    • What happens to the appearance of the square when the width of the GLUT window is greater than the height?
    • What happens to the appearance when the height is greater than the width?

World Coordinates

Use the same program and experiment changing the world coordinate system.

  1. Place the following code in the global section of the program.
    /*
    ** manage world coordinates
    */
    GLdouble xLeft   = -1.0;
    GLdouble xRight  =  1.0;
    GLdouble yBottom = -1.0;
    GLdouble yTop    =  1.0;
    
  2. The following function sets the OpenGL coordinate system to the specified values, where xLeft, yBottom is the bottom left hand corner of your virtual 2D world and xRight, yTop is the top right hand corner. C requires that you position functions above functions that call them. Place setProjection above display.
    void setProjection(void){
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       gluOrtho2D(xLeft, xRight, yBottom, yTop);
       glMatrixMode(GL_MODELVIEW);
    }
    
  3. Add appropriate comments to describe what the function does.
  4. Call the function from the top of display (as in the highlighted code below).
    void display(void){
       setProjection();
       /*
       ** Clear the window with the background colour before
       ** (re)rendering.
       */
       glClear(GL_COLOR_BUFFER_BIT);
    
       glColor3fv(colour[orange]);
       render(square,NUM_VERTICES_FOR_SQUARE,GL_POLYGON);
       /*
       ** Cause all OpenGL commands to be executed.
       */
       glFlush();
    }
    
  5. The given values for xLeft, xRight, yBottom, yTop are the defaults, so there will be no change in the appearance. Change the values and observe what happens.
  6. Record your observations for the following values.
    xLeft   -2.0, -0.5, 0.0
    xRight   2.0,  0.5, 1.0
    yBottom -2.0, -0.5, 0.0
    yTop     2.0,  0.5, 1.0
    

Render a Star

Use the same program, add code to render the image of a star. Change the world coordinate system so that the full image can be seen.

  1. Place the following code in the manage model(s) section of the global section of the program.
    #define STAR_VERTICES 10
    vertexType star[]={
       {0.8, 0.1}, {1.95,1.1}, {3.2, 0.4},
       {2.6, 1.9}, {3.5, 2.9}, {2.2, 2.7},
       {1.6, 3.8}, {1.3, 2.6}, {0.1, 2.3},
       {1.1, 1.6}
    };
    
  2. Add the following code in the appropriate place in display. Make the star and the square different colours.
       render(star, STAR_VERTICES, GL_LINE_LOOP);
    
  3. Adjust the OpenGL virtual world coordinate system to achieve the following.
    Specify a "square" system - that is, the square should appear to be square.
    • View the full star but not much of the square.
    • View both the full star and the full square.

In Lecture #02 Week #02, you recorded the coordinates of the star.

  1. Use your coordinates to specify the vertices.
  2. Adjust the virtual world coordinate system so that you can view the full image.
  3. Work out a way of filling the star with colour. Description of the OpenGL primitives can be found in Lecture #01 Week #02.
  4. Change the title in the GLUT window.

Assignment 1

Start working on Assignment 1, which has been available since 31st July 06. If you did not receive a handout in the lecture, ask me for one .

Copyright © 2006 Fran Soddell
last updated 31 July 2006 F.Soddell@latrobe.edu.au