INT32GP Graphics Programming: Tutorial #04
Star and Elephant

B1.11 3:00pm Tue 06 Aug 02
B1.11 12midday Wed 07 Aug 02

In tutorial #03 you worked out the coordinates of vertices representing a star and an elephant. In this tutorial you will develop programs to display outlines of these objects. You will also fill the star with colour using GL_TRIANGLE_FAN or your own algorithm.

The Star
Start with the example program, star1. This renders the outline of a star. However, only a small portion of the star is visible.

  1. The program uses the OpenGL default coordinate system. The middle of the window is the point (0.0,0.0), the bottom left-hand corner is (-1.0,-1.0) and the top right-hand corner is (1.0,1.0). This does not suit the coordinates of the vertices of the star as seen below.
    typedef GLdouble vertexType [2];
    int starSize=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. To better suit these vertices, set up a coordinate system where the bottom left-hand corner of the window is the point (0.0,0.0) and the top right-hand corner is (4.0,4.0).
  3. Change the values for the coordinate system so that xRight=5.0 and yTop=20.0. What happens to the star? Try different values of your own.
  4. In the array, star, change the coordinates of the vertices to those that you worked out in the tutorial.
  5. Work out a corresponding coordinate system and change the values of xLeft, xRight, yBottom and yTop to suit it.
  6. While the program is running resize the window. Notice that the star is pushed out of shape. One way to retain the proportion of the star is to change the viewport in the window so that it is always a square.
The Elephant
Copy your star program so that you have another program called elephant.c.
  1. Replace the star array with an elephant array containing the coordinates for the vertices that outline the elephant (worked out for tutorial #03).
  2. Make corresponding changes to the code in void renderView() so that the outline of the elephant will be displayed.
  3. Work out an appropriate coordinate system so that the elephant will be centred in the window and change the values of xLeft, xRight, yBottom and yTop to suit.
  4. Define arrays for the vertices that represent the outline of the foot, ear, tail and eye.
  5. Add code to render these features. Use GL_POINTS for the eye.
Filling the star with colour
Return to your star program and add code to fill the star with colour as discussed in tutorial #03. The following code fragment may be helpful if you wish to use GL_TRIANGLE_FAN.
   glBegin(GL_TRIANGLE_FAN);
      glVertex2d(2.0,2.0);
      for(i=0;i<starSize;i++)
         glVertex2dv(star[i]);
      glVertex2dv(star[0]);
   glEnd();
Can you add code that will render each triangle of the fan in a different colour?

Fran Soddell      last updated 05 August 2002 F.Soddell@bendigo.latrobe.edu.au