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)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 20, 0, 20);

    glMatrixMode(GL_MODELVIEW);
    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");
   glutDisplayFunc(display);
   myinit ();
   glutDisplayFunc (display);
   glutMainLoop();
   return 0;
}

This produces the following screen:
BigO

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

Q
 

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 */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 120.0, 0.0, 120.0);
    glMatrixMode(GL_MODELVIEW);

}

void display( void )
{
    glClear(GL_COLOR_BUFFER_BIT);
    /*clear the window */

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

    sin.open("infile.txt");
    if(sin.fail())
    {
        cout << "Error openning file \n";
        exit(1);
    }

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

        glBegin(GL_LINE_STRIP);
           glVertex2fv(v1);
           glVertex2fv(v2);
        glEnd();
        i++;
    }

    glFlush();
    /* clear buffers */
}

void main(int argc, char** argv)
{

    /* Standard GLUT initialization */
    glutInit(&argc,argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    /* default, not needed */
    glutInitWindowSize(500,500);
    /* 500 x 500 pixel window */
    glutInitWindowPosition(0,0);
    /* plce window top left on display */
    glutCreateWindow("MyTest");
    /* window title */
    glutDisplayFunc(display);
    /* 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:

data

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:
4
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.