CS 4465, Lab 3
Display List & Drawing Using Input Data

Our goals in this lab are:

Display List
The following program uses a display list to create letter "O" on the display.

 *  Textlist.cpp -
 *  This program uses a display list to display Letter O on the screen
 *  This program demonstrates how to make and execute a

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

using namespace std;

GLuint listName;

static void myinit (void)
    gluOrtho2D(0.0, 20, 0, 20);

    glClearColor (1, 1.0, 1.0, 1.0);
     float angle;
      listName = glGenLists (1);
      glNewList (listName, GL_COMPILE);
      glTranslatef(5, 10, 0.0);
      glColor3f (1.0, 0.0, 0.0);  /*  current color red  */
      glBegin (GL_QUAD_STRIP);
      for(int i = 0; i <= 12; i++)
          angle = 3.14159/6.0 * i;
          glVertex2f (4*cos(angle), 4*sin(angle));
            glVertex2f (5*cos(angle), 5*sin(angle));
      glEnd ();
      glEndList ();
      glShadeModel (GL_FLAT);


void display(void)
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (0.0, 1.0, 0.0);  /*  current color green  */
    glCallList (listName);

   glFlush ();

int main(int argc, char** argv)
   glutInit(&argc, argv);
   glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
   glutInitWindowSize(500, 500);

   glutCreateWindow("Big O");
   myinit ();
   glutDisplayFunc (display);
   return 0;

This produces the following screen:

Activity (1)
Modify the above program to create letter Q.  Something like this maybe:


Activity (2)
Try to modify your program such that it displays OQO on the screen.  You decide about the size of the and location of the

Reading Object Data from a File
The following program shows how we can read data points from a file then display them.

/* The following program reads data points from an input file and
will display lines.  Input file format:
Number_of_lines    (this refers to number of lines)
data   (note that for each line you need 4 data start(x,y) and end(x,y)

The input file is fixed to infile.txt but you can change that

Rahman Tashakkori - CS4465 Spring 2004

/* IF you get an error for the following lines use <iostream> <fstream> and <cstdlib> */
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <GL/glut.h>

using namespace std;

typedef   GLfloat  point2[2];

void myinit(void)
    /* attributes */
    glClearColor(1.0, 1.0, 1.0, 1.0);

    /* white background */
    glColor3f(1.0, 0.0, 0.0);
    /* draw in black */
    /* set up viewing */
    gluOrtho2D(0.0, 120.0, 0.0, 120.0);


void display( void )
    /*clear the window */

    point2 v1, v2, v3, v4;
    int i = 0, npairs;
    ifstream sin;

        cout << "Error openning file \n";

    sin >> npairs;  // One line -- one pair
    while(i < npairs )
        sin >> v1[0] >> v1[1];
        sin >> v2[0] >> v2[1];


    /* clear buffers */

void main(int argc, char** argv)

    /* Standard GLUT initialization */
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    /* default, not needed */
    /* 500 x 500 pixel window */
    /* plce window top left on display */
    /* window title */
    /* display callback invoked when window opened */
    myinit(); /* set attributes */
    glutMainLoop(); /* enter event loop */

If you run this program, you will get a screen that looks like this:


Activity (3)
You can use a similar method to draw a function, such as sin or cos.  To do so, you need to obtain some data points and store them in a file, something like:
Number of Data Point
x0 y0
x1 y1
An example input file for a sin function:
0 0
30 0.5
90 1
120 0.5
You need to read this file in and plot x and y.

Bonus - 100 points toward assignments - If you decided to do it (DUE Feb 25).
As I mentioned in class, you can use a similar technique to draw a different things. For example: houses and trees.  One of the thing I want you to do in this assignment is to read different data files to create different objects.  For example to create a house your program will read data from a house file and will draw a house based on the data that is read. Then inside the program you will resize and relocated the house as you wish. You can do the same thing to create trees. 
You only have one file for a house and will do resizing and relocating inside the program using techniques we have learned in this lab to create many houses of different sizes in different locations on the screen. Similarly, you will draw many trees of different sizes in different places.