CS 4465, Lab 1
Introduction to OpenGL
Our goals in this lab are: Getting Ready

Throughout the semester, we will have several labs.  You are required to save some of your files on the hard drive or on a floppy disk.  Please bring at least one floppy disk with you to keep a backup copy of your files.  If you plan to work on a program from home, use ftp to transfer your file(S) to cs so you can access it from other places.  Please use ASCII mode when you move your C++ files around, otherwise you will get an ^M at the end of each line, which is pain in the neck to remove it.
If you wish to save your files locally and then copy them on a floppy disk, skip Steps (1-6) and create a directory lab1 on the local machine.

Now, it is time to create a directory in which you always keep your files.  You will create this directory on the server, so you can use it from different machines in the lab.
    1) Click on the MyNetwork icon on the screen, 
    2) Click on the Computers Near Me,
    3) Once you get the list of computers, find the cs064454, and double click on it.  You will get the shared printer and a directory called cs-share,
    4) Double click on the cs-share to get another directory called cs4465,
    5) Double click on cs4465,
    6) Once you are there, under the File Tab, click on the New, and create a directory called YourID.  Example: My user ID is rt, so I have created a directory rt there.  From now on, you will save all your files under appropriate directories there,
    7) Create the lab1 directory for today's lab.

In order to run OpenGL programs, we will use Visual C++ compiler which is installed on the PCs in the PC Lab (Room 439).  To run the OpenGL programs, we also need to link some of the OpenGL libraries in each program that we wish to run.  During this lab, we may run to some problems when we compile the programs.   These problems, will be resolved by including appropriate files in the include or lib directories.  To make sure, we have the library files in place, here is what you need to do before starting the lab.

You will need to make sure the following files are in the appropriate directories.  Check first and if any of the following files are missing copy them by clicking on the links given here.  Put glut.h in the Visual C++ include\GL directory, glut32.dll in the \windows\system or  \winnt\system directory, and glut32.lib in the Visual C++ lib directory. The Visual C++ Software is commonly saved under the Microsoft Visual Studio then under VC98 directory.

To compile OpenGL programs under Visual C++:
Use the Start button at the bottom left side of the screen to go to the Programs and bring the Visual C++ compile up.  Create a New C++ Source Code by choosing that option under the Files Tab.  Once the blank screen "Microsoft Visual C++ [cpp]" window appeared,  use the File, Save As option to save the blank file as line.cpp in the lab1 directory.  Please note that it is extremely important that you copy the file under appropriate directory.

A Simple Example: Line Program
Cut an paste the following program into the line.cpp file that you had saved in the previous step.  Click on Save to save the changes.

/* Program line.C. Draws a single line from lower left
to upper right corner of the window */

#include <GL/glut.h>
void myinit(void)
{

        /* attributes */
        glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */
        glColor3f(0.0, 0.0, 0.0); /* draw in black */
        /* set up viewing */
        /* 50 x 50 window with origin lower left */
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(0.0, 50.0, 0.0, 50.0);
        glMatrixMode(GL_MODELVIEW);
}

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

        glBegin(GL_LINES);
                glVertex2f(0,0); 
                glVertex2f(50,50);
        glEnd();

        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); /* place window top left on display */
        glutCreateWindow("Test"); /* window title */
        glutDisplayFunc(display); /* display callback invoked when window opened */
        myinit(); /* set attributes */
        glutMainLoop(); /* enter event loop */
}
Now, you need to link the libraries that you need to compile and run this program.    At the top of the screen, click on Project, then Setting to get the Project Setting window.  Click on the Link tab and go to the end of the line under Object/library Modules and at the end of that line after a blank space add:  glut32.lib  opengl32.lib  glu32.lib.   If you wish, you can cut these (highlight, then Ctrl-C) and paste them (Ctrl-V) there.  Once this is done press OK.  You can compile and run the program three different ways,
    1) by using the options given under the Build Tab,
    2) by using the radio buttons at the top right of the program screen, or
    3) by using the shortcuts for each task.

Compiling
Here is the three methods for compiling the program, you pick one:
        1) Click on Build, then Compile line.cpp
        2) Click on the button, at the top right of the screen, that looks like the first one on Figure (1) bellow.

Figure (1) - C++ Radio Buttons

        3) By using Ctrl-F7.

Once you compile the program, at the bottom, you will get a message:
line.cpp
line.obj - 0 error(s), 0 warning(s)

Building the Project
The next step is to build the project.  You can do this in three different ways.  I am sure you have made up you mind on which way is better for you.  In case you want to use the short cut, it is F7.

Once you build the project, at the bottom, you will get a message:
Linking...
line.exe - 0 error(s), 0 warning(s)

Executing the Program
At the end of previous step, you created an executable file called line.exe.  You can run the program several different ways:
    1) by using the Start, Run option and running the file line.exe that is saved under the Debug directory of your lab1 directory,
    2) by using the Execute line.exe under the Project tab,
    3) by clicking on the fourth button (the red ! ) at the top right of the screen, or
    4) by pressing Ctrl-F5.

You decide about the method. The program that you just ran makes a 500 x 500 pixel window, Figure 2.



The name of this screen is Test. The line;

gluOrtho2D(0.0, 50.0, 0.0, 50.0);
maps world coordinates in the range [0,50] x [0,50] into this window, with (0,0) at the lower left. This is a parallel projection in which the z-coordinates are thrown away. The most significant block of code for the purposes of this lab is
        glBegin(GL_LINES);
                glVertex2f(0,0); 
                glVertex2f(50,50);
        glEnd();
which draws a single line. The "2f" in "glVertex2f"says that we specify points using two floating point parameters. The z-coordinate is assumed to be zero, thus we are working in the a 2D plane, x-y plane.

You can add other lines in the glBegin .. glEnd block; for example,

        glBegin(GL_LINES);
                glVertex2f(0,0); 
                glVertex2f(50,50);
                glVertex2f(50,0); 
                glVertex2f(0,50);
        glEnd();
would draw a big "X". Change the display function to:
void display( void )
{
        float t=0,tnew,x,y,xnew,ynew;
        x=40*cos(t);
        y=40*sin(t);
        glClear(GL_COLOR_BUFFER_BIT);  /*clear the window */

        while(t<6.3){
                t+=.1;
                xnew=40*cos(t);
                ynew=40*sin(t);
                glBegin(GL_LINES);
                        glVertex2f(x,y);
                        glVertex2f(xnew,ynew);
                glEnd();
                x=xnew;
                y=ynew;
        }

        glFlush(); /* clear buffers */
}
Since you have used the sin and cos functions, you need to add:
#include <math.h>
at the top of the program. Recompile and run. You should get part of a circle.
 

Exercise:  Change the display code so that the entire circle fits in the window.

Homework
Your homework assignment is to further modify the program so that it prints a sine curve (at least two cycles) centered vertically in the window. The result should look something like this:

E-mail your source code to me (rt) on cs before class on Wednesday, 2/5/03 and turn in a printout at the start of class.  Make sure to include a header with your name, course number, and date at the top of your program.  If there is any special instructions for running your program, please include it in the header of the program.