Introduction
Some three months ago I published the article of the Lessons integrated in Visual C++ MFC project. With a little bit surprise I've discovered that a lot of people feel something like panic with the words MFC (somebody even called it as a disgust). Nevertheless the idea of OpenGL lessons is fine and the codes this site provided from are applicable in any version of software. Therefore I've done the same job for Win32 Visual C++ Visual Studio 2010-2012 pro.
I believe that concentrations of all the lessons in one common project should be very helpful for all the beginners to commence the OpenGL study. The codes of the lessons arranged closely one by one demonstrate the progression of the procedures from the previous stage to the next one. Also the integration of all the lessons in one common project makes it possible to use and combine procedures from separate lessons and also to develop new applications based on original lessons.
While adapting the code to the modern versions of
Visual C++, I’ve found some conflicts occurred in projects modified from one
version of MS Visual Studio to another. Finally for demonstration purposes, I’ve
developed the project OpenGLWin32_2010
. Here 2010 means that project has been
developed for Microsoft Visual Studio Professional 2010. Nevertheless, all the
projects easily jump into version 2012, but for this version Windows 7 is
required while version 2010 is available for Windows XP SP3 and higher.
The project above has been developed with the great help of the editor of the
Russian site of NeHe Sergey Anisimov
whom I thank very much for his kind assistance.
The code above may give a great help for people who want to study Visual
C++ from the beginning. Needless to say that the best way to
study any compiler is to start learning with some working projects.
Presentation of this lesson refers to Visual Studio 2010 pro. However, there
will not appear too many differences in version 2012.
The content of the OpenGLWin32_2010 directory is as follows:
OpenGLWin32
- the empty project containing the projects of all the lessons Lesson01..X5
- projects of every separate lesson - GLSummary - project containing all the lessons
Data
- source data for all the lessons - NeHeProgs - the main and auxiliary codes of the lessons
- GlobUse - procedures common to all the lessons
- DemoLessons - all executable files of all the lessons
- HelpText - help and info files for all the lessons
For code view and edition OpenGLWin32.sln file to be started. Even if you have no
experience of using Visual C++, you may commence to learn it by starting this
(or any other enclosed) project.
The list of the procedures used in the project to be found by menu View->Solution
Explorer. Just select any *.h or *.cpp file and
you may look at it and edit. At first sight, it may seem that there are too
many files enclosed but further consideration shows that everything is not too
complicated.
To build all the projects, just start building (menu Build->Build Solution).
If everything is done right, the report in the Output window your results must
be: 49 succeeded, 3 failed, 0 skipped.
The failed projects are OpenGLWin32
itself (it has no entry point) and
Lesson43/Lesson47. To my regret, I have not mu?h
experience OpenGL handling and two of the lessons I’ve failed to adapt to
modern versions of the Visual C++. I hope that for the respected authors of the
original codes will not be difficult to improve my codes to fit them completely
to their original intentions.
To start any project, just appoint one as starting (right mouse click – menu Appoint
As Starting Project) and select menu Debug->Start
Without Debugging. If everything is done right, the demonstration of the
corresponding lesson will commence. If you appoint the GLSummary
project as starting one of the lessons to be commenced (the lesson whose name
is stored in LastNum.txt file in LessonsGL/GLSummary
directory). For any other lesson demonstration, select menu File->Lesson...
The Select Lesson
dialog to be appeared:
You may select any other lesson from the list for demonstration. If you start working from the single lesson the program calls the lesson selected and after that kills itself. If you start from GLSummary the program just calls the selected lesson contained. Lesson43/Lesson47 also available to start, but you can not call other lessons from them: these are original .exe files.
For the independent application creation:
- Create
a new directory (say, GLTest);
- copy all
*. exe
programs (located in ../lesson*/Release
and ../GLSummary/Release
directory) into this new directory; - copy Data and HelpText directories into this new directory and the applications
are ready for use and they must work exactly as the programs in DemoLessons directory.
The procedures CreateGLWindow
, ResizeGLScene
, WndProc
and GlobWinMain
are common to all the lessons and located
in the GlobUse/GLRoutine.cpp file.
The procedure CreateGLWindow
is almost the same as the original in NeHe's tutorials.
In the procedure ResizeGLScene
a few modifications occurred:
GLvoid ReSizeGLScene(GLsizei width, GLsizei height) {
if (height==0) {
height=1; }
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
if(m_bOrtho)
glOrtho(0.0f,width,height,0.0f,-1.0f,1.0f); else
gluPerspective(fViewAngle,(GLfloat)width/(GLfloat)height,fNearPlane,fFarPlane);
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
HWND hWin = GetDlgItem(hWnd, IDC_MAIN_TOOL);
if(hWin != NULL)
{ RECT rect;
GetWindowRect(hWin, &rect);
BOOL bVisible = IsWindowVisible(hWin);
MoveWindow(hWin, rect.left, rect.top, rect.bottom - rect.top, width, TRUE);
}
HWND hSt = GetDlgItem(hWnd, IDC_MAIN_STATUS);
if(hSt != NULL)
{ RECT rect;
GetWindowRect(hSt, &rect);
BOOL bVisible = IsWindowVisible(hSt);
MoveWindow(hSt, rect.left, height + rect.top - rect.bottom, rect.bottom - rect.top, width, TRUE);
}
m_boxWidth = width; m_boxHeight = height;
}
In the gluPerspective
procedure, the global variables fViewAngle,fNearPlane,fFarPlane
appeared. In most lessons, the
default values:
fViewAngle = 45; fNearPlane = <st1:metricconverter
ProductID="0.1f" w:st="on">0.1f</st1:metricconverter>; fFarPlane = 100;
are to be used. However in the lessons
30,31,34,36,44,45,46, these values are to be changed.
Also the global variable m_bOrtho
arranges the glOrtho
performance if
required (lessons 21 and 24).
The TollBar
and StatusBar
are adjusted to main window while ResizeGLScene
.
The procedures WndProc
and GlobWinMain
also have had some changes due to extra dialogs and bars handling. You may take
a look at these changes in the GlobUse/GLRoutine.cpp - nothing special, just routine.
Full screen view in difference with the original lessons occurred by
simultaneously pressing Ctrl+Enter.
F1 key in difference with the original lessons is used for standard purpose for
help call (menu Help->Help Dialog...)
Help Dialog
window is non-modal, thus the program handling may be performed as from
this window and/or by the other ways. All help information is stored in HelpText directory as simple text files.
With regards to the respected authors of the original code, the About
Dialog (menu Help->About...) contains the original NeHe's
ReadMe texts:
This About Dialog
is also available from another Lesson
and
Help
Dialogs
with the About... button.
One more difference with the original lessons is that all the texture
procedures are integrated in GlobGLTexture.cpp file in GlobUse directory. Just
instead of auxDIBImageLoad
for texture loading I've arranged
procedures as follows using class CImage
:
HBITMAP GetImgBitmap(CImage * nImg)
{
if(nImg == NULL)
return NULL;
CImage * pImg = new CImage;
pImg->Create(nImg->GetWidth(),nImg->GetHeight(), 24);
nImg->BitBlt(pImg->GetDC(),CPoint(0,0));
HDC hdc = CreateCompatibleDC(pImg->GetDC());
HBITMAP hbm = CreateCompatibleBitmap(pImg->GetDC(), pImg->GetWidth(), pImg->GetHeight());
HBITMAP bmOld = (HBITMAP) SelectObject(hdc,hbm);
pImg->BitBlt(hdc, CPoint(0,0));
pImg->ReleaseDC();
delete pImg;
return hbm;
}
HBITMAP LoadMyBitmap(char * fName)
{
if(!isFileExist(fName))
{
char strk[_MAX_PATH];
_getcwd(str, _MAX_PATH);
sprintf_s(strk, "LoadMyBitmap\nNo File exist:\n%s\nin Directory:\n%s", fName, str);
MessageBox(NULL, strk, "No File:", MB_OK | MB_ICONINFORMATION);
return NULL;
}
CImage * pImg = new CImage;
pImg->Load(fName);
HBITMAP bm = GetImgBitmap(pImg);
delete pImg;
pImg = NULL;
return bm;
}
You may copy GlobGLTexture.cpp file and use it for
any projects using OpenGL
development with all kind of image files that Windows
has available.
The lesson's codes themselves are concentrated in NeHeProgs
directory and commenced from "01_", "02_",..."X5_"
in correspondence with the lesson number. These are the codes presented in NeHe’s GameDev site (Grant James)
and as far as possible I’ve tried not to change the code while adapting to
modern versions of Visual Studio.
The only big difference is that I dare to exclude Glaux.lib
library from all the projects. Nevertheless, I hope the authors of the original
code will not complain. Sure they could not expect from MS VS developers that Glaux.lib library to be excluded from 10th version onwards. And
if it is still possible to include Glaux.lib in the MS VS 10th version in the 11th
version, it is not working.
The concentrations of all the procedures in one common directory GlobUse make it possible to use and combine common
procedures in separate lessons and also to develop new applications based on
original lessons. The samples of application development are presented in
projects lessonX4/UFOgame and lessonX5/BoxMan. Lesson X4.UFO Games are combined
from the lessons 07, 11, 30, 32, 34. Lesson X5.BoxMan demonstrates the
possibility of figures and their movements creation in
outer text files and the user need not know programmer details, space
imagination enough.
And this LessonX5 shows one problem due to simultaneous use of <windows.h> and <afxwin.h>.
The conflict occurred was deprecated in Properties
Window Configuration Propeties->Linker->General->Force
Output. The error is gone but the warning still remains and EXE file
occurred to be very large. In MFC platform, no problems occurred.
I think that the idea of OpenGL study with the help of the ready working
projects completely fit to the people’s tutorial on OpenGL. I hope that the
projects provided should arrange a good support for the tutorials and lessons
on OpenGL.