CS 4465, Lab 1
Introduction to OpenGL - Visual C++ 6.0 Version
This lab was modified by Mr. Daniel Babel, the Instructional Assistance of this course in Spring 2004

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 disk or on a floppy disk.  If you do not save the images resulting from a run, the programs do not take up too much space.  Please bring at least one floppy disk with you to keep a backup copy of your files.  also, if you plan to work on a program from home, you can use ftp to transfer your file(s) to cs so you can access them from home or 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 is very important that you complete and run your programs on the hard disk.  The executable files take a good bit of disk space and can not be run from the floppy.  For this to work best, you may need to sit at the same machine every time we come to the lab.  This way you can create a directory with the same name as your ID on cs and keep it until the end of this semester.  

    1) Create a directory for yourself, best is to call is YourID,
    2) 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.

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
Lab Activity (1): 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,
        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.

When you run it, you will get the following screen.

line



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".

Lab Exercise (1): 
Change the program such that it displays a triangle with all sides 100 units. Note the screen name.

triangel


Lab Activity (2):  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.
 

Lab Exercise (2):  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:

Submit your source code as an attachment to en e-mail to me (rt) on cs before class on Wednesday, 1/28/04 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.