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 your work-station's hard disk, a floppy disk, or on memory sticks for later use.  If you do not save the images resulting from a run, the C++ programs (OpenGL programs) themselves do not take up too much disk space.  So make sure to either save the program files (.cpp file) on a disk or transfer the files on Student or CS machines.   In genral, I recommend that you use the same machine everytime we come to the lab.  You may use the U directory on the machine you are using and create a subdirectory, perhaps you can call it your CS ID, to keep your files.  Please note that there is no guarantee that your files do not get deleted.

It is very important that you complete and run your programs on the hard disk of the machine you are using.  Executable files take a large amount of disk space.  For this to work best, you may need to use the same machine every time we come to the lab.  This way, you can create a directory with the same name as your CS ID and keep it until the end of this semester.  

    1) Under the U directory, create a directory for yourself, best is to call it Your CS ID,
    2) Create the lab1 directory for today's lab.

In order to run OpenGL programs, we will use Visual C++ compiler under .Net which is installed on the PCs in the PC Lab (Room 439 or Ex-Term Lab).  To run the OpenGL programs, we also need to link some of the OpenGL libraries in each program that we wish to run.  We alreay have done this for you.  So, when you are in the lab, you can skip the Step 0. But at home, you have to go through Step 0 to set up OpneGl.

Step 0

The lab machines are set up, so you can skip this part.  However, you have to go through this step to set your machine at home.

To make sure, we have the OpenGL files in place, here is what you need to do.  First we need to install GLUT onto the computers. The files you will need are as follows:

  • glut32.dll
  • glut.h
  • glut32.lib

  • Place glut32.dll into C:\Windows\System32.
    Place glut.h into C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\gl
    Place glut32.lib into C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Lib

    It is perhaps best to download these files from the web at: Nate's GLUT Page . The file is called glut-3.7.6-bin.zip (117 KB) which is a zip file. Save the zip file under you directory. Then go to that directory and unzip the file. It gives you the above files. Save them under corresponding directories as instructed above.

    End of Step 0

    Activity (1) - Creating a Project
    Now we need to create a new project in which to place our source files.
  • Open MS Visual Studio .NET
  • Select File->New->Project
  • Expand Visual C++ Projects on the left-hand side
  • Select the folder Win32
  • On the right, select Win32 Console Project
  • Set the location to save the project (Example, U:\ab23456\graphics\labs\lab1)
  • Give the project a name (line)
  • Click OK
  • In the Win32 Application Wizard, Click on 'Application Settings', on the left
  • On the next screen, Application Type: Console application, Additional Option:  'Empty Project'
  • Click on Finish
    This creates the project environment without any files in it. There should be an empty workspace, and a window on the side labeled Solution Explorer. Follow the following steps to insert a new blank file.
  • Right click on the folder 'Source Files'
  • Select Add->Add New Item
  • Under Code, Select a C++ (.cpp) file
  • Name the file (line.cpp)
    Click on Open
    An icon should appear in the solution explorer indicating this new file (line.cpp), which should also be opened in the main part of the window. At this point you have finished setting up MS Visual Studio .NET and have a blank file where you can type your program.  Below, there is an example you will use for your first program.

  • 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.cpp. 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 */
    }

    To Compile, Build, and Run it all at once you can use Ctrl-F5 (debug).

    To compile an OpenGL programs under .Net:
    Use the Build tab, click on Compile, or while you are in the program do Ctrl-F7. If your program does not have any error, you will get:

    Build: 1 Suceeded, 0 Failed, 0 Skipped

    Please note that it is extremely important that you copy the file under appropriate directory.


    When you run the program, you will get the following screen. Note that upon correct compilation, you will also get an executable file that will be stored in your working directory and can be run to produce this screen.

    line
    The line appeared broken on this image, but it should be a solid line on your screen.

    The name of this screen is Test. Make a change so it has your name at the top.

    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 drawing purposes of this line is:
            glBegin(GL_LINES);
                    glVertex2f(0,0); 
                    glVertex2f(50,50);
            glEnd();
    which draws a single line between the two given vertices. 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".   Note every line segment connects two vertices (points).

    Lab Exercise (1): 
    Change the program such that it displays a triangle with all sides of size 100 units. You need a little bit of calculations to get this exactly as shown.  Note that all sides are 100 in length, so you have to place the TOP point at a location that gives you the sides with size 100 as well.  Make the Title of the screen be Triangle instaed of Lab1.

    triangel

    Lab Exercise (2):  Change the program such that it displays a square with sides of 100 units.

    Post-lab - Due Fri Feb 6
    You are required to e-mail two separate programs (.cpp) for the following two parts of the postlab assignment.  My e-mail address is rt@cs.appstate.edu.  Please at the Subject line indicate which partPlease make sure you have your name on all your programs I will deduct 10 points for each program without proper header (Name in particular).

    Part (1) (30 points):
    Modify the above program so that it displays a house similar to the one shown below.   Make your program such that if we decide to add another house we don't have to type many lines of code.  Instead, by giving the location and size (width, height, etc...), it draws a house at the indicated location.  Send this program as an attachement.  At the subject line put Part1-Post-Lab1.

    OneHouse

    Part(2) (70 points):
    Modify the above program further such that it displays a drawing similar to the one shown below.  Sent this program as an attachement.  At the subject line put Part2-Post-Lab1.

    TwoHouse

    If there is any special instructions for running your program, please include it in the header of the program.