Installing and Using COpenGLWnd

This document describes how to install and utilize the COpenGLWnd superclass.  COpenGLWnd is derived from CWnd and can be utiilized a superclass for windows in Microsoft Foundation Class that will utilize OpenGL.  

Installing COpenGLWnd

It is assumed you are starting with a fresh MFC appwizard application that has been created as single document interface without the document/view architecture.  The only classes you should have in your application when you start are:  CAboutDlg, CChildView, CTutorialApp, and CMainFrame.  

Step 1

You'll find the files you need in graphics.zip.  I recommend unzipping this and keeping the files in a directory called graphics, then you can easily update them if necessary.  There are two files:  OpenGLWnd.h and OpenGLWnd.cpp.  Then, select:  Project/Add existing item.. and select the .h file.  Repeat this process for the .cpp file.  Visual Studio automates the creation of Make Files. You are adding these two components to your program.  Note that it won't work to compile the program, yet.  When you are done, you should see COpenGLWnd in the ClassView window if you expand lab classes.

Step 2

Now we need to tell your program to link to the OpenGL libraries.  Select the project in the solution explorer, then Project/Properties.  A dialog box will come up.  On the top you'll find a pull-down menu labeled Configuration.  Change that from Active(Debug) to All Configurations.  Now, on the left side, click on the Linker property, then the Input option under that property.  Put the following into the Additional Dependencies box:

    opengl32.lib glu32.lib glaux.lib 

Press ok.  Your program should compile at this point.  It won't do anything different, yet.  

Step 3

Now we need to change the superclass for CChildView to COpenGLWnd.  Open the file ChildView.h.  You can do this by double-clicking on CChildView in the class browser, by selecting it a s Header File in FileView, or by File/Open.   

Add the following line before the definition of the CChildView class:

    #include "graphics/OpenGLWnd.h"

Then, change the following line:

    class CChildView : public CWnd

to:

    class CChildView : public COpenGLWnd

This is simply making CChildView be derived from COpenGLWnd rather than CWnd.  

Open ChildView.cpp.  Change the following line from:

    BEGIN_MESSAGE_MAP(CChildView,CWnd )

to:

    BEGIN_MESSAGE_MAP(CChildView,COpenGLWnd )

Make sure your program compiles at this point.

Step 4

The default version of CChildView actually contains a member function we want to get rid of.  Remove both the declaration of CChildView::OnPaint in ChildView.h and the definition in ChildView.cpp. 

Step 5

Go to the CChildView::PreCreateWindow() member function.  You can do this by double-clicking on the function in ClassView.  Change:

    if (!CWnd::PreCreateWindow(cs))
        return FALSE;

to:

    if (!COpenGLWnd::PreCreateWindow(cs))
        return FALSE;

Compile and run your program.  You should see a Cyan screen.  This is the default OpenGL draw function I have supplied.

Step 6

Now we'll put in a function we can use for OUR OpenGL code.  Right-click on CChildView in ClassView and select Add/Add Function in the menu.  Enter void for the Return Type, OnGLDraw for the function name, and CDC *pDC as a CDCparameter.  Leave it as Public.  When you press Finish, the function is created.  Put this in the function body:

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glFlush();

When you run this you should see a black screen. You now have an OnGLDraw function you can utilize.

The following can be ignored during lab 1.  You should add this in later.

Making COpenGLWnd save your image as a BMP file

You need to add a menu option to make COpenGLWnd save your file as a BMP file.  Create a menu option under the File menu called Save BMP File.  Create a handler in CChildView.  In that handler, call the function:  OnSaveImage().  That's all it takes.

COpenGLWnd public functions

void SetDoubleBuffer(bool p_doublebuffer)

By default, COpenGLWnd does not use double buffering.  If you want to create a window using double buffering, call this function with a TRUE parameter from the CChildView constructor:

    SetDoubleBuffer(true);

void GetSize(int &p_width, int &p_height);

Call this function to determine the width and height of the display window.  Example:

    int width, height;
    GetSize(width, height);

void OnSaveImage()

Call this function to save the current display window to a file.  Note that this function may induce another rendering of the window.  If part of the window is occluded, it may not save correctly.