当前位置:编程学习 > C/C++ >>

制作一个基于MFC对话框的OpenGL类

今天写了个基于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

补充:软件开发 , C语言 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,