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

Our goals in this lab are:

Preparations
It is best to use the same machine you have used in Lab (1).  If you are using the same machine as you did in the first lab, all your OpenGL files are supposed to be there.  Otherwise, you need to copy the files before you can start using OpenGL.  To copy the OpenGL files, follow the instructions in Lab (1).  The machine you are using should have Microsoft Visual Studio.     Log into cs so you can send your file as you complete each activity.  If by any chance, you didn't finish the lab during the lab hour, complete the lab and send me the files as I have requested in each activity by Friday Feb. 10 (next Friday).

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>

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

Lab Activity (1)
Modify the above program to create letter Q.  Something like what I have shown below. it does not have to be exactly like this, as long as I can tell it is a Q :-).   Email your program as an attachment, on the subject line put Activity 1.

Q
 

Lab Activity (2)
Modify your program to display OQO on the screen.  You decide about the size of the and location of the letters.  Submit the program for this activity as an attachement.  At the Subject Line, write Activity 2.

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 2006
 */

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

typedef   GLfloat  point2[2];

void myinit(void)
{
    /* attributes -   white background */
    glClearColor(1.0, 1.0, 1.0, 1.0);
    glColor3f(1.0, 0.0, 0.0);  //   draw in black
 
    /* set up viewing */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, 500.0, 0.0, 500.0);
    glMatrixMode(GL_MODELVIEW);

}

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

    point2 v1, v2, v3, v4;
    int i = 0, npairs;
    float r, g, b, width;
    ifstream in_s;

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

    in_s >> npairs;  // One line -- one pair
    while(i < npairs )
    {
        //read the two pairs for the endpoints of the line
        //read the r, g, and b for color, and width
        in_s >> v1[0] >> v1[1] >> v2[0] >> v2[1] >> r >> g >> b >> width;
        glColor3f(r, g, b);
        glLineWidth(width);
        glBegin(GL_LINES);
           glVertex2fv(v1);
           glVertex2fv(v2);
        glEnd();
        i++;
    }

    glFlush();
    /* clear buffers */
}

int 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);
    /* place window top left on display */
    glutCreateWindow("Few Lines");
    /* window title */
    glutDisplayFunc(display);
    /* display callback invoked when window opened */
    myinit(); /* set attributes */
    glutMainLoop(); /* enter event loop */

    return 0;

}

Copy and paste the program and run it.  Since you don't have the input file, you won't get anything.  Create a file called input.txt and store the data in red font in that file and save the file. The file must be saved in the same directory as the .cpp file for this to work.

4
150.0 250.0 200.0 300.0  1.0 0.0 0.0 2.0
200.0 300.0 250.0 250.0  1.0 0.0 0.0 2.0
160.0 260.0 160.0 130.0  1.0 0.0 1.0 8.0
240.0 260.0 240.0 130.0  1.0 0.0 1.0 8.0

which has the following format:
Number of line = 4   -- This has to follow with four lines each representing one line.
x and y of the start point, then x and y of the end point, then r,g,b  for color of the line, and then the width of the line.  So the  first line starts at x = 150.0 and y=250.0, ends at x = 200.0 and y = 300.0, with r = 1.0, g = 0.0, and b = 0.0 for its color, and width = 2.0.

Now you can run the program to produce the output.  It is a surprise. 
If you run this program using the input.txt file and you will get a screen that looks like this:

Lab Activity (3)
You can use a similar method to draw (plot) the points for a function.
Number of Data Point
x0 y0
x1 y1
..
An example of input data file for a sin function:
11
-5  29
-4  20
-3  13
-2  8
-1  5
0  4
1   5
2  8
3  13
4  20
5  29

You need to read this file into your program and draw the data points a Line Strip.  Can you tell what the function is? hint


You will get something that looks like this:

One Quarter Cycle of Sin Function.

y = ? * x ? + ?

Submit the program for this activity as an attachment.  At the Subject Line, write Activity 3.