今天寫了個基於MFC對話框的OpenGL類:COpenGL,可以在對話框程序中使用OpenGL了,並且提供了全屏與非全屏轉換的兩個函數,很容易使用,速度快。
使用方法:在對話框上加一個Static控件(或者其他的也可以),在OnInitDialog()中加人下面這段代碼(假設控件ID為IDC_OPENGL,m_openGL是類COpenGL的對象):
Code: CRect rect;
GetDlgItem(IDC_OPENGL)->GetWindowRect(rect);
ScreenToClient(rect);
m_openGL.Create(rect, this);
然後在適當的地方調用m_openGL.RenderGLScene()就可以了。
以下是類代碼(OpenGL.h和OpenGL.cpp):
Code:#if !defined(AFX_OPENGL_H__38B5D1C8_2DFF_4A7D_9A99_3AC401C19D72__INCLUDED_)
#define AFX_OPENGL_H__38B5D1C8_2DFF_4A7D_9A99_3AC401C19D72__INCLUDED_
#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(LPCREATESTRUCT lpCreateStruct);
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_H__38B5D1C8_2DFF_4A7D_9A99_3AC401C19D72__INCLUDED_)
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(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &m_DMsaved);
GLuint PixelFormat; // Holds 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