C++ OpenGL MFC Projects.

The control flow-chart of the standard OpenGL application with the steps numerated as per items of the sections accordingly.

4.1. Create GL Window

The procedure CreateGLwindow is common to all the lessons and located in GLRoutine.cpp file in the common GlobUse directory.

The procedure CreateGLWindow to be called by virtual procedure OnCreate:

int CChildView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	CreateGLWindow(this, 32);             //borrowed from NeHe site; 
                                          //located in GlobUse\GLRoutine.cpp
	_tgetcwd(m_rootDir, _MAX_PATH);       //root directory identification
	CreateListLessons();                  //list box of DlgLesson dialog content; 
                                          //located in GlobUse\GLRoutine.cpp
	m_bPlay = TRUE;                       //flag of autoplay
  	SetTimer(ID_TIMER_PLAY, 15, NULL);    //timer of the scene to change in 15 milliseconds
	return 0;
}

The CreateGLWindow procedure almost completely has been borrowed from the original NeHe lessons and as much as possible, I’ve tried not to change the authoring codes.

 

4.2. Init Application

Initialization procedure to be called by the procedure CChildView::InitChildView:

void CChildView::InitChildView(void)
{
	m_bInitGL = FALSE;          //initialization flag
	m_pView = this;             //identification of the window 
                                //to be called in global procedures
	InitMRU();                  //initialization of the resent lessons list
	InitGLn(m_strNum);          //initialization of the OpenGL window
                                //(located in GlobUse\GlobDrawGL.cpp)
	//Main Frame Text:
	GetMainFrameGlob->SetWindowText( CString(AfxGetApp()->m_pszAppName) + 
                        _T(" : ") + (m_strLesson = GetLessonName(m_strNum)));
}

InitGLn initialization of the OpenGL window procedure (located in GlobUse\GlobDrawGL.cpp) refers to the initialization procedure InitGL_XX(GLvoid) of the current lesson located in the NeHeDrawInit\XX_*DrawInit.cpp. These initialization procedures have been borrowed from the NeHe site. I’ve tried not to change these codes as far as possible.

The only big difference is that I’ve taken the liberty to exclude Glaux.lib library from all the lessons used for the texture initialization. Nevertheless, I hope the authors of the original codes 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 procedures for the texture initialization instead of Glaux.lib are located in the Globuse\GlobTexture.cpp. The instructions for the texture initialization may be obtained from my former CodeProject articles “Texture Mapping in OpenGL using Class CImage” and “Masking Texture in OpenGL using Class CImage“.

4.3. Resize GL Window

Procedure ResizeGLScene common to all the lessons to be called by virtual procedure OnSize:

void CChildView::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);
	ReSizeGLScene(cx, cy);        //base borrowed from NeHe site; 
                                  //located in GlobUse\GLRoutine.cpp
	SetTitlePos();                //title of the lesson  in Main Frame and in Full Screen mode
}

In distinct from the NeHe original base some global variables inserted:

GLfloat  fNearPlane = 0.1f, //Frustum Near Plane
         fFarPlane = 100.f, //Frustum Far Plane
         fViewAngle = 45.f, //Frustum View Angle
	     fAspect;           //Frustum Width/Height ratio
RECT m_viewRect;            //current View rect size in pixel

In some lessons (30,31,34,36,44,45,46,X4), the Frustum values set differ from default ones in the InitGL_XX procedure.

In lessons 21 and 24, the glOrtho presentation is used:

if(m_strNum == _T("21")||m_strNum == _T("24"))
	 glOrtho(0.0f,width,height,0.0f,-1.0f,1.0f);    // Create Ortho widthxheight View 
                                                    // (0,0 At Top Left)
else	                                            // Calculate The Aspect Ratio Of The Window
	gluPerspective(fViewAngle,(GLfloat)width/(GLfloat)height,fNearPlane,fFarPlane);

4.4. Draw GL Scene

OpenGL drawing procedure to be called by virtual procedure OnPaint:

void CChildView::OnPaint() 
{
	CPaintDC dc(this);               // device context for painting
	if (m_bInitGL)InitChildView();   //Initialization of the GL window if first call
	DrawGLSceneN();                  //Global drawing procedure
	// Do not call CWnd::OnPaint() for painting messages
}

DrawGLSceneN drawing of the OpenGL window procedure (located in GlobUse\GlobDrawGL.cpp) refers to the drawing procedure DrawGLScene_XX(GLvoid) of the current lesson located in the NeHeDrawInit\XX_*DrawInit.cpp. These drawing procedures have been borrowed from the NeHe site. I’ve tried not to change these codes as far as possible.

4.5. Messages Handling

Keyboard procedures handling to be called by virtual procedures OnKeyDown and OnKeyUp:

void CChildView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	ProcessKeyboardN(nChar);                 //Global procedure of nChar handling
	CWnd::OnKeyDown(nChar, nRepCnt, 1nFlags);
}
void CChildView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	VkKeyUpN(nChar, nRepCnt, nFlags);        //Global procedure of nChar handling
	CWnd::OnKeyUp(nChar, nRepCnt, nFlags);
}

ProcessKeyboardN and VkKeyUpN keyboard commands handling procedures (located in GlobUse\GlobDrawGL.cpp) refer to the keyboard procedure ProcessKeyboard_XX(int idKey) and VkKeyUp_XX of the current lesson located in the NeHeDrawInit\XX_*DrawInit.cpp. These keyboards commands have been borrowed from the NeHe site. I’ve tried not to change these commands as far as possible. Just F1 command I’ve changed for Help performance and Ctrl+Enter command changed to FullScreen Mode handling.

Mouse procedures handling to be called by virtual procedures OnMouseMoveOnLButtonDown and OnRButtonDown:

 void CChildView::OnMouseMove(UINT nFlags, CPoint point)
{
	mouse_x = point.x; mouse_y = point.y;  //global mouse position
	MouseMoveN(nFlags, point);             //Global procedure of Mouse Move handling  
	CWnd::OnMouseMove(nFlags, point);
}
void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
	LButtonDownN(nFlags, point);           //Global procedure of Mouse Left Button handling    
	CWnd::OnLButtonDown(nFlags, point);
}
void CChildView::OnRButtonDown(UINT nFlags, CPoint point)
{
	RButtonDownN(nFlags, point);           //Global procedure of Mouse Right Button handling
	CWnd::OnRButtonDown(nFlags, point);
}

MouseMoveN, LButtonDownN and RButtonDownN mouse commands handling procedures (located in GlobUse\GlobDrawGL.cpp) refer to the mouse handling procedures MouseMove_XX, LButtonDown_XX and RButtonDown_XX of the current lesson located in the NeHeDrawInit\XX_*DrawInit.cpp. These mouse commands have been borrowed from the NeHe site. I’ve tried not to change these commands as far as possible.

Timer procedure handling to be called by virtual procedure OnTimer:

void CChildView::OnTimer(UINT_PTR nIDEvent)
{
	switch (nIDEvent)
	{
	case ID_TIMER_PLAY:
		if (!m_bPlay) return; //if not play mode do nothing
		UpdateGLScene();      //Global procedure for one step image change
		DrawGLSceneN();       //Global drawing procedure
		break;
	}
	CWnd::OnTimer(nIDEvent);
}

UpdateGLScene procedure for one step image change (located in GlobUse\GlobDrawGL.cpp) refers to the procedure Update_XX of the current lesson located in the NeHeDrawInit\XX_*DrawInit.cpp. These update procedures have been borrowed from the NeHe site. I’ve tried not to change these procedures as far as possible.

4.6. Kill GL Window

If the new lesson in the Lesson Dialog is selected to start at the same window, the memory must be released and the OpenGL window to be closed. Also, before the application exit also the memory must be released and the OpenGL window to be closed:

    void CChildView::OnAppExit()
{
	DeinitializeN();			//Clear memory
	KillGLWindow(this);			//Close OpenGL window
}

DeinitializeN procedure for clear memory (located in GlobUse\GlobDrawGL.cpp) refers to the procedure Deinitialize_XX of the current lesson located in the NeHeDrawInit\XX_*DrawInit.cpp. These clear memory procedures have been borrowed from the NeHe site. I’ve tried not to change this procedure as far as possible.

The procedure KillGLwindow is common to all the lessons and located in GLRoutine.cpp file in the common GlobUse directory. The KillGLWindow procedure almost completely has been borrowed from the original NeHe lessons and as much as possible, I’ve tried not to change the authoring codes.

5. Joystick Implementation

Joystick handling is available in lessons 09, 10, 32, 40, X4, X5 in projects MFC_GL and MFC_GL1. In the project MFC_GL0, the Joystick functions are not available. Next two steps require to make MFC_GL0 working with the Joystick as in MFC_GL1:

  • Select menu Project->Add Existing Item… and insert GlobUse\JoystickMFCDlg.cpp and GlobUse\JoystickHelpDlg.cpp into Solution Explorer window
  • Open MFC_GL0\MFC_GL0\SetName0.cpp and put in comments (or just remove) the empty procedure void InitJoystickDlg(void){}.

And that’s all (point 2.1 of this article conditions understood). Now the project should be working with the Joystick.

Joystick procedures handling to be called by virtual procedure CJoystickMFCDlg::OnRawInput.

HandleJoystickN procedure for Joystick Controls handling (located in GlobUse\GlobDrawGL.cpp) refers to the procedure HandleJoystick_XX of the current lesson located in the NeHeDrawInit\XX_*DrawInit.cpp. These procedures have been developed by the author for demo purposes. Lesson 40 Joystick handling, e.g.:

    void HandleJoystick_40(int nx, int ny, int nz, int nzr, int nh, 
                           BOOL bButtonStates[], BOOL bButtonPrev[])
{
	ropeConnectionVel = Vector3D(-0.003f*nzr, 0.003f*nx, 0.003f*ny);
}

You may change these existing HandleJoystick_XX codes or create yourself as you like.

Publicado por djalmabina

WordPress and Google Maps developer,Blogger, Article Writer,Freelance Writer. Please look my recent Blogs: https://geatland.wordpress.com/

%d blogueiros gostam disto: