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

Our goals in this lab are:

• Learn how to draw letters (postscrip style text)
• Learn about display lists and how we can use it to draw objects in OpenGL
• Learn to draw using input data points
Preparations
It is best to use the same machine you had used in Lab (1).  If you are using the same machine as you did in the first lab, the files you had saved on U:\ drive are supposed to be there.  If by any chance, you are unable to finish this lab during the lab hour, complete the lab and send me the files as I have requested in each activity by Friday Feb. 13 (next Friday).

Activity (1) - Drawing a Circle
As you have learned in your introductory math courses, to draw a circle, you need to know where the center of the circle is and its radius.  I think the first thing to do is to find how to put a single vertex at the center of the screen you will draw.   The following program draws a single dot. Use the x and y coordinates to draw that dot at the center of your screen.  What are the x, y coordinates corresponding to the center of the screen?

/* Draws a single dot  */
#include <stdlib.h>
#include <GL/glut.h>

GLuint listName;

static void myinit (void)
{
glClearColor (1.0, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 20.0, 0.0, 20.0);
glMatrixMode(GL_MODELVIEW);
}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 1.0, 0.0);  /*  current color green  */
float x = 5, y = 5;
glPointSize(10);
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
glFlush ();
}

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

glutCreateWindow("Vertex");
glutDisplayFunc(display);
myinit ();
glutMainLoop();
return 0;
}

Activity (1), Part 2 - Drawing a Circle
Now that you find out where you are in the screen, let's try to draw a circle.  The following program will draw a circle by connecting the dots that fall a radius away from the center.   The calculation for this part is done at:
glVertex2f (5*cos(angle), 5*sin(angle));

which is based on x = radius*COS(theta) and y = radius*SIN(theta).

/*  Draws a Circle*/
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
float PI = 3.14159;

GLuint listName;

static void myinit (void)
{
glClearColor (1, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 20, 0, 20);
glMatrixMode(GL_MODELVIEW);
}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 1.0, 0.0);  /*  current color green  */
float angle;
glTranslatef(5, 10, 0.0);
glColor3f (1.0, 0.0, 0.0);  /*  current color red  */
glBegin (GL_LINE_LOOP);
int noOfSegments = 12;
int segmentAngle = 360.0/noOfSegments;
for(int i = 0; i <= noOfSegments; i++)
{
angle = (segmentAngle)*(PI/180) * i; //convert the segment to Radian
glVertex2f (5*cos(angle), 5*sin(angle));
}
glEnd();
glShadeModel (GL_FLAT);

glFlush();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Nice Circle");
glutDisplayFunc(display);
myinit ();
glutMainLoop();
return 0;
}

Practice - Make the initial circle look smoother.
Practice - Make a square using the above program.

Activity (2) - Drawing an O using Display List
In class we talked about two different ways to create text.  One way was to use bitmap font and another was to draw letters.  Here we will practice drawing an O using the above program and to save coding we will use the Display List method.

The following program uses a display list to create letter "O" on the display./*  Draws a Circle */
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>

GLuint listName;

static void myinit (void)
{
glClearColor (1, 1.0, 1.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 20, 0, 20);
glMatrixMode(GL_MODELVIEW);
}

void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 1.0, 0.0);  /*  current color green  */
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);

glCallList (listName);
glFlush ();
}

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

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

This produces the following screen:

In Lab Assignment (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 (.cpp file only) as an attachment, on the subject line put: Yourname, Lab 2 - Inlab Assignment (1).

Lab Activity (2)
Modify your program to display OQO on the screen.  You decide about the size of the and location of the letters.
Email your program (.cpp file only) as an attachment, on the subject line put: Yourname, Lab 2 - Inlab Assignment (2).

Post Lab - Due Friday Feb 15
Drawing objects usign 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 2008 */

/* 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:
x and y of the start point, then x and y of the end point, then r, g, 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.

Post-Lab Assignment
Modify the above program such that it reads data from an input file to produce one house similar to that you created in the Post Lab Problem (1) in Lab (1).

Email your program (.cpp file only) as an attachment, on the subject line put: Yourname, Lab 2 - Postlab Assignment.