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.
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.
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.
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. First, click on Configuration Properties in the left side dialog box to expand it. Then click on Linker to expand it and finally click on Input.
On the top you'll find a pull-down menu labeled Configuration. Change that from Active(Debug) to All Configurations. Put the following into the Additional Dependencies box:
opengl32.lib glu32.lib glaux.lib
I usually also add winmm.lib so I have support for multimedia timers, the best way in Windows to add timing to applications such as animations.
Your Property Page should look something like this (circles added):

Press ok. Your program should compile at this point. It won't do anything different, yet.
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 as a 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.
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.
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.
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 parameter. You enter "CDC *" as the Parameter Type and "pDC" as the Parameter Name. Leave the function set 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.
|
If the screen continues to be Cyan, you probably got the parameters wrong on the function. The function should look like this: void CChildView::OnGLDraw(CDC *pDC)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glFlush();
}
|
The following can be ignored during step 1. You can add this in later if you need it.
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.
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);
Call this function to determine the width and height of the display window. Example:
int width, height;
GetSize(width, height);
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.