Today I wrote an OpenGL Class Based on the MFC dialog box: COpenGL, which can be used in the dialog box program. It also provides two functions for full-screen and non-full-screen conversion, which are easy to use and fast.
Usage: Add a Static control (or others) to the dialog box, and add the following code in OnInitDialog () (assuming that the Control ID is IDC_OPENGL, m_openGL is a COpenGL-class object):
Code: CRect rect;
GetDlgItem (IDC_OPENGL)-> GetWindowRect (rect );
ScreenToClient (rect );
M_openGL.Create (rect, this );
Then call m_openGL.RenderGLScene () in the appropriate place.
The following is the class code (OpenGL. h and OpenGL. cpp ):
Code: # if! Defined (afx_opengl_h000038b5d1c8_2dff_4a7d_9a99_3ac401c19d7200000000ded _)
# Define afx_opengl_h000038b5d1c8_2dff_4a7d_9a99_3ac401c19d7200000000ded _
# If _ MSC_VER> 1000
# Pragma once
# Endif // _ MSC_VER> 1000
// OpenGL. h: header file
//
//////////////////////////////////////// /////////////////////////////////////
// COpenGL window
Class COpenGL: public CWnd
{
// Construction
Public:
COpenGL ();
// Attributes
Public:
// Operations
Public:
// Overrides
// ClassWizard generated virtual function overrides
// {AFX_VIRTUAL (COpenGL)
//} AFX_VIRTUAL
// Implementation
Public:
BOOL SetNormScreen ();
BOOL SetFullScreen (int width, int height, int depth );
Virtual void RenderGLScene ();
Void Create (CRect rect, CWnd * parent );
Virtual ~ COpenGL ();
// Generated message map functions
Protected:
CRect m_rect;
CWnd * m_parent;
BOOL m_bFullScreen;
DEVMODE m_DMsaved;
BOOL m_bInit;
Int InitGL ();
Void KillGLWindow ();
HDC m_hDC;
HGLRC m_hRC;
// {AFX_MSG (COpenGL)
Afx_msg int OnCreate (maid );
Afx_msg void OnPaint ();
Afx_msg void OnSize (UINT nType, int cx, int cy );
//} AFX_MSG
DECLARE_MESSAGE_MAP ()
};
//////////////////////////////////////// /////////////////////////////////////
// {AFX_INSERT_LOCATION }}
// Microsoft Visual C ++ will insert additional declarations immediately before the previous line.
# Endif //! Defined (afx_opengl_h000038b5d1c8_2dff_4a7d_9a99_3ac401c19d7200000000ded _)
Code: // OpenGL. cpp: implementation file
//
# Include "stdafx. h"
# Include "DialogOpenGL. h"
# Include "OpenGL. h"
# Include <gl/gl. h>
# Include <gl/glu. h>
# Ifdef _ DEBUG
# Define new DEBUG_NEW
# Undef THIS_FILE
Static char THIS_FILE [] = _ FILE __;
# Endif
//////////////////////////////////////// /////////////////////////////////////
// COpenGL
COpenGL: COpenGL (): m_bInit (FALSE), m_bFullScreen (FALSE ),
M_hDC (NULL), m_hRC (NULL), m_parent (NULL)
{
}
COpenGL ::~ COpenGL ()
{
KillGLWindow (); // Shutdown
}
BEGIN_MESSAGE_MAP (COpenGL, CWnd)
// {AFX_MSG_MAP (COpenGL)
ON_WM_CREATE ()
ON_WM_PAINT ()
ON_WM_SIZE ()
ON_WM_KEYDOWN ()
//} AFX_MSG_MAP
END_MESSAGE_MAP ()
//////////////////////////////////////// /////////////////////////////////////
// COpenGL message handlers
Void COpenGL: Create (CRect rect, CWnd * parent)
{
If (m_bInit) return;
ASSERT (rect );
ASSERT (parent );
M_rect = rect;
M_parent = parent;
CString className = AfxRegisterWndClass (
CS_HREDRAW | CS_VREDRAW | CS_OWNDC, NULL, (HBRUSH) GetStockObject (BLACK_BRUSH), NULL );
CreateEx (0, className, "OpenGL", WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, rect, parent, 0 );
}
Int COpenGL: OnCreate (maid)
{
If (CWnd: OnCreate (lpCreateStruct) =-1)
Return-1;
// TODO: Add your specialized creation code here
EnumDisplaySettings (NULL, ENUM_CURRENT_SETTINGS, & m_DMsaved );
GLuint PixelFormat; // The Results After Searching For A Match
Static PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be
{
Sizeof (PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
M_DMsaved.dmBitsPerPel, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer