Friday, May 28, 2010

P1. Using OpenGL in Microsoft Visual C++ 2008

This post describes how to implement OpenGL in Microsoft Visual C++ 2008. I am sure that there are other ways to do the same but this is the way I managed to learn. In order to do the same, follow these steps:

1. Create a MFC dialog

Open Visual Studio C++ 2008 and select the option to create a new project. A new window will pop up. Select MFC as project type (left column) and MFC Application as template (right column). Choose whatever project name you like. In this occasion I am choosing projectOpenGL.



Click OK. In the new window select the application type Dialog Based. Click on Finish.



Then, you should get the main project's window where you probably can see a tree of the project's files, the project's dialog and the properties window.



2. Drag and drop a picture control

Drag and drop a picture control onto your dialong window. To do this, open the Toolbox dialog editor, click on Picture control and drag onto the dialog windonw. Adjust its position and size.



Open the properties window of the picture control and set the following two properties as indicated:

Visible: False
ID: IDC_OPENGL1

3. Add the class COpenGLControl1 to the project

That is, insert files COpenGLControl.h and COpenGLControl.h.cpp inside the project's main folder. Add these two files into your project in the following way: under the Solution Explorer view, right click on the Header Files folder, choose the option Add Existing Item and select the file COpenGLControl1.h. Repeat the same process for the Source Files folder and COpenGLControl1.cpp. Now, go into the file projectOpenGLDlg.h (nameoftheprojectDlg.h)and include the line:

#include "COpenGLControl1.h"

4. Include the header of your project into the openGL class

Ensure that in file COpenGLControl1.cpp there is the line:

#include "projectOpenGL.h"

If you have chosen another project's name rather than projectOpenGL, then ensure that you add: #include "yourProjectName.h".



5. Solve library and linking dependencies.

You need to indicate your project where to find the openGL libraries. Click on the Project tab and then on projectOpenGL Properties...



A new window will pop up:



Navigate through your hard disk until you indicate the path of the openGL Include folder. In my case, this is C:\Program Files\Microsoft SDKs\Windows\v6.0A\Include.

Repeat the same process to add the Library folder. In my case, this path is C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib.



Specify the additional dependencies by indicating the files opengl32.lib and glu32.lib



6. Customize the MFC Dialog class

Go to the header file of the class of the dialog control, in this example, projectOpenGLDlg.h. Ensure that the following line is included at the top of the file:

#include "COpenGLControl1.h"

and add the following variable:

private:
COpenGLControl1 m_oglWindow1;

just before the last line:

DECLARE_MESSAGE_MAP()

7. Modify the OnInitDialog function

The OnInitDialog function is part of the file projectOpenGLDlg.cpp. Inside this function, add the following lines just after the comment // TODO: Add extra initialization here:

CRect rect;

// Get size and position of the picture control
GetDlgItem(IDC_OPENGL1)->GetWindowRect(rect);

// Convert screen coordinates to client coordinates
ScreenToClient(rect);

// Create OpenGL Control window
m_oglWindow1.oglCreate(rect, this);

// Setup the OpenGL Window's timer to render
m_oglWindow1.m_unpTimer = m_oglWindow1.SetTimer(1, 1, 0);

Build and run the program. If everything is OK, you should get a window like this:



It is possible to rotate the frame by clicking the mouse's left button and moving the mouse. It is also possible to zoom in or out the frame by clicking the mouse's right button while moving the mouse.



References:

[1] http://www.codeguru.com/cpp/g-m/opengl/openfaq/article.php/c10975/