分类: C/C++
2008-05-31 14:43:19
----6.由于OpenGL作图时需要长时间占用DC,所以最好把作图窗口类设成CS_OWNDC。MFC缺省的窗口类风格中没有设这一属性,程序中在主窗口C++
类的PreCreateWindow方法中自己注册了一个窗口类,除了设定了CS_OWNDC属性以外,还设定了CS_HREDRAW、CS_VREDRAW和CS_SAVEBITS。设定
CS_HREDRAW、CS_VREDRAW是为了让窗口缩放时产生WM_PAINT消息,修正OpenGL视口和作图尺寸;由于OpenGL作图需要很多计算,设定CS_SAVEBITS是
为了在OpenGL窗口被遮盖后显现出来时,不产生WM_PAINT消息,用内存的图象来填充,从而用空间消耗换取计算时间。
----7.本程序中没有对OpenGL函数的出错情况作出处理。OpenGL出错后返回错误码,不会抛出异常;而且在某一个函数出错以后,后继函数也一般
不会出现异常,只是返回错误码,一不小心就可能忽略某些错误。而对每一个OpenGL函数都做出错与否的判断比较麻烦,所以编程序时对OpenGL的
函数应当非常小心。
----参考书籍:
----《OpenGLProgrammer\'sGuide》SGIinc.
----《OpenGL三维图形程序设计》廖朵朵、张华军著,星球地图出版社
----《VisualC++5.0联机帮助》
----附程序:
----程序运行时必须确定OpenGL32.dll、glu.dll、glaux.dll在的System目录下。如果找不到这些文件,可以从95OSR2的机器上面
将这些文件拷贝过来即可。OpenGL运行不需要注册库信息。在VC的STUDIO中运行程序时,工程文件中必须加入OpenGL.H、glu.h、glaux.h以及
OpenGL.lib、glu.lib、glaux.lib,这些文件由VC自带。
----主窗口类定义(OpenGLWnd.h):
s#if !defined(AFX_OPENGLWND_H__3FB1AB28_0E70
_11D2_9ACA_48543300E17D__INCLUDED_)
#define AFX_OPENGLWND_H__3FB1AB28_0E70_11D2
_9ACA_48543300E17D__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
#include < afxwin.h >
#include \"SimpleGLApp.h\"
#include \"resource.h\"
// OpenGLWnd.h : header file
//
///////////////////////////////////////
//////////////////////////////////////
// COpenGLWnd frame
class COpenGLWnd : public CFrameWnd
{
DECLARE_DYNCREATE(COpenGLWnd)
public:
COpenGLWnd();
// protected constructor used by dynamic creation
protected:
HGLRC m_hrc;
CClientDC *m_pDC;
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(COpenGLWnd)
protected:
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~COpenGLWnd();
// Generated message map functions
//{{AFX_MSG(COpenGLWnd)
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnDestroy();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnPaint();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
#endif // !defined(AFX_OPENGLWND_H__3FB1AB28_
0E70_11D2_9ACA_48543300E17D__INCLUDED_)
主窗口类的实现(OpenGLWnd.cpp):
// OpenGLWnd.cpp : implementation file
//
#include \"stdafx.h\"
#include \"OpenGLWnd.h\"
#include \"SimpleGLApp.h\"
#include \"gl\\glu.h\"
#include \"gl\\gl.h\"
#include \"gl\\glaux.h\"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////
//////////////////////////////////////
// COpenGLWnd
IMPLEMENT_DYNCREATE(COpenGLWnd, CFrameWnd)
COpenGLWnd::COpenGLWnd()
{
m_pDC = NULL;
m_hrc = 0;
LoadFrame(IDR_MAINFRAME,WS_OVERLAPPEDWINDOW
| WS_CLIPCHILDREN | WS_CLIPSIBLINGS
,NULL,NULL );
}
COpenGLWnd::~COpenGLWnd()
{
}
BEGIN_MESSAGE_MAP(COpenGLWnd, CFrameWnd)
//{{AFX_MSG_MAP(COpenGLWnd)
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL COpenGLWnd::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized
code here and/or call the base class
cs.lpszClass = AfxRegisterWndClass( CS_DBLCLKS |
CS_HREDRAW |
CS_VREDRAW |
CS_SAVEBITS |
CS_NOCLOSE |
CS_OWNDC
,AfxGetApp( )-
> LoadStandardCursor(IDC_ARROW), 0 ,
AfxGetApp( )- >LoadStandardIcon(IDI_APPLICATION));
return CFrameWnd::PreCreateWindow(cs);
}
int COpenGLWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
int pixelformat;
m_pDC = new CClientDC(this);//在客户区作图
ASSERT(m_pDC != NULL);
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), //固定值
1, //固定值
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_TYPE_RGBA, // RGBA模式,不用调色板
16, //程序在16位色彩下运行
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};if ( (pixelformat = ChoosePixelFormat
(m_pDC- >GetSafeHdc(), &pfd)) == 0 )
{
MessageBox(\"在该DC上找不到与PFD接近的位图结构\");
return -1;
}
if (SetPixelFormat(m_pDC- >
GetSafeHdc(), pixelformat, &pfd) == FALSE)
{
MessageBox(\"无法在该DC上设置位图结构\");
return -1;
}
m_hrc = wglCreateContext(m_pDC- >GetSafeHdc());
wglMakeCurrent(m_pDC- >GetSafeHdc(), m_hrc);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
return 0;//OpenGL窗口构造成功
}
void COpenGLWnd::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
if(cy > 0)
{
glViewport(0, 0, cx, cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (cx < = cy)
glOrtho(-3.0,3.0,-3.0 * (GLfloat)cx/(GLfloat)cy,
3.0 * (GLfloat)cx/(GLfloat)cy,-3.0,3.0);
else
glOrtho(-3.0,3.0,-3.0 * (GLfloat)cy/(GLfloat)cx,
3.0 * (GLfloat)cy/(GLfloat)cx,-3.0,3.0);
glMatrixMode(GL_MODELVIEW);
}
}
void COpenGLWnd::OnDestroy()
{
CFrameWnd::OnDestroy();
::wglMakeCurrent(NULL, NULL);
if (m_hrc)
::wglDeleteContext(m_hrc);
if (m_pDC)
delete m_pDC;
// TODO: Add your message handler code here
}
BOOL COpenGLWnd::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message
handler code here and/or call default
return TRUE;
//return CFrameWnd::OnEraseBkgnd(pDC);
}
void COpenGLWnd::OnPaint()
{
CPaintDC dc(this); // device context for painting
GLfloat light_position[]={2.0f,0.0f,4.0f,0.0f};
// TODO: Add your message handler code here
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0f, 0.0f, -2.0f);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
auxSolidSphere(1.0);
glPopMatrix();
glFinish();
// Do not call CFrameWnd::OnPaint() for painting messages
}
应用程序类的定义(SimpleGLApp.h):
#if !defined(AFX_SIMPLEGLAPP_H__3FB1AB29
_0E70_11D2_9ACA_48543300E17D__INCLUDED_)
#define AFX_SIMPLEGLAPP_H__3FB1AB29_0E70
_11D2_9ACA_48543300E17D__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// SimpleGLApp.h : header file
//
#include < afxwin.h >
#include \"OpenGLWnd.h\"
#include \"resource.h\"///////////////////////////////////////
//////////////////////////////////////
// CSimpleGLApp thread
class CSimpleGLApp : public CWinApp
{
DECLARE_DYNCREATE(CSimpleGLApp)
public:
CSimpleGLApp();
// protected constructor used by dynamic creation
// Attributes
public:
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CSimpleGLApp)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CSimpleGLApp();
// Generated message map functions
//{{AFX_MSG(CSimpleGLApp)
afx_msg void OnAppExit();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
///////////////////////////////////////
//////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert
additional declarations
immediately before the previous line.
#endif // !defined(AFX_SIMPLEGLAPP_H__3FB1AB29_
0E70_11D2_9ACA_48543300E17D__INCLUDED_)
应用程序类的实现(SimpleGLApp.cpp):
// SimpleGLApp.cpp : implementation file
//
#include \"stdafx.h\"
#include \"SimpleGLApp.h\"
#include \"OpenGLWnd.h\"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////////////////////////////
//////////////////////////////////////
// CSimpleGLApp
IMPLEMENT_DYNCREATE(CSimpleGLApp, CWinApp)
CSimpleGLApp::CSimpleGLApp()
{
}
CSimpleGLApp::~CSimpleGLApp()
{
}
BOOL CSimpleGLApp::InitInstance()
{
// TODO: perform and per-thread initialization here
m_pMainWnd = new COpenGLWnd();
m_pMainWnd- >ShowWindow(m_nCmdShow);
m_pMainWnd- >UpdateWindow();
return TRUE;
}
int CSimpleGLApp::ExitInstance()
{
return CWinApp::ExitInstance();
}
BEGIN_MESSAGE_MAP(CSimpleGLApp, CWinApp)
//{{AFX_MSG_MAP(CSimpleGLApp)
ON_COMMAND(ID_APP_EXIT, OnAppExit)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
///////////////////////////////////////
//////////////////////////////////////
// CSimpleGLApp message handlers
void CSimpleGLApp::OnAppExit()
{
// TODO: Add your command handler code here
CWinApp::OnAppExit();
}
CSimpleGLApp SimpleGLApp;