Lesson 1

Start Microsoft Developer .NET 

From the "File" menu choose "New" and "Project"

On the New Project Window choose "MFC Application"

In the "Name" box type "Lesson1" then choose the location where you would like to store you tutorials.

The MFC Application Wizard will start.

Click on the Application type. Select "Dialog Based" and select "Use MFC in a static library". Using a static compile makes distribution easier and prevents DLL version hell.

Now press the "Finish" Button and MS Developer will construct a skeleton program for you.

You should now see something like this.

From the "Debug" menu choose "Start"

Now you have a GUI application and you didn't even write a single line of code. Not very exciting, but lets start adding a few things.

Take the "OK" button and drag it down to the lower right. Right click on it and choose properties (if the properties sheet is not already up). Change the caption property to "Play". This is going to our "Play" button that will start the OpenGL part of the program. Left click on the TODO text then hit delete. We won't be needing it. Change the properties of the cancel button to visible "False" (Right click and choose properties if the property sheet is not already up) Hint: You can toggle a Boolean property by double clicking on it in the property sheet for a control, Try it. Your app should look like this now.

Now let add some code. Double click on the "Play" button. The IDE will create a function for you and map the buttons click event to it automatically.

This is where we will put our OpenGL and glut code.

Next click on the class tab on the right. Right click on the project and choose Add Class.

Choose Generic C++ Class. Press the "Open" button.

Type in MFCopenGL for the class name. This is where we will encapsulate our openGL code. Then press the "Finish" button.

You should have this now.

Lets add Glut now.

Double click on MFCopenGL. This will open the header file for our class. On the secound line right after pragma once line add this:

#include <gl\glut.h> // glut.h includes gl.h and glu.h

This is the glut header file. Do a compile and run now. If you get an error saying the compiler can't find the glut header then you don't have the glut libraries installed correctly in your compiler. Refer to here for instructions on correctly installing glut.

Now lets add our callback functions. The callbacks are predefined functions that Glut has that we will be writing the code for. For a list of all the callbacks in glut look here.

Right click on MFCopenGL and choose add and add function. The function wizard will open. Make the return type void and give it no parameters. Make the function name "display". Put "The openGL callback function" in the comment box. Hit finish.

Right click on MFCopenGL and choose add and add function. The function wizard will open. Make the return type void and give it the int parameters of width and height. Make the function name "resize". Put "The openGL resize callback function" in the comment box. Hit finish.

You should see this:

Expand the class tree for MFCopenGL and doubleclick on display. Put the following code in the display function:

glClearColor(0,0,0,0);//set the background color to black
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glColor3f(1.0f, 0.0f, 0.0f);
//set cube color
glutWireCube(30);
//use the premade glut function to draw a wire cube of size 30
glutSwapBuffers();

Now put the following code in the resize function:

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (
float)width / (float)height, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Set the viewing position and orientation
gluLookAt(
0.0, 0.0, 200.0,
// eye location
0.0, 0.0, 0.0,
// center location
0.0, 1.0, 0.0);
// up vector

You should see this:

Now we will hook glut to the functions we have made.

Expand the CLesson1Dlg class and doubleclick on OnBnClickedOk.

Erase everything in the function and replace it with:

glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);//set out options to RGB, double buffered and depth
glutCreateWindow("MFC Glut Lesson1");
//give the glut window a title
// set glut callback functions

glutDisplayFunc(display);
//set our display callback
glutReshapeFunc(resize);
//set out resize callback
this->ShowWindow(SW_HIDE);// hide the mfc starter when the glut window opens
glutMainLoop();
//start the glut main loop

Now scroll to the top and add the header for our opengl class.

#include "MFCopenGL.h"

After the other headers.

Now add the global instance of our openGL class

MFCopenGL gl;

Now add the global callbacks:

void display(void)
{

gl.display();

}

void resize(int w, int h)
{

gl.resize(w, h);

}

 

You should see this:

Compile and run, then hit the play button. You should see this:

Congratulations you have made your first of many OpenGL programs!

You can download the source code for this lesson here.