配置OpenGL运行环境
新建MFC对话框程序OpenGL,新建类MyOpenGL(继承于CWnd)
MyOpenGL.h
#pragma comment(lib,"glut32.lib")
#include "glut.h"
class MyOpenGL :
public CWnd
{
public:
MyOpenGL(void);
~MyOpenGL(void);
DECLARE_MESSAGE_MAP()
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
afx_msg void OnSize(UINT nType, int cx, int cy);
public:
void display();
void reflesh();
CWnd *cwnd;
float m_tranlate[3];//用于平移,对应X,Y,Z平移量,按键W上
float m_rorate[3];
float m_scale;
protected:
HGLRC m_hRC;//opengl上下文OpenGL绘制描述表
HDC m_hDC;
HPALETTE m_hPalette;//OpenGL调色板
BOOL SetupPixelFormat(HDC hdc);//设置像素格式
BOOL InitializeOpenGL(/*CDC* pDC*/);
void SetLogicalPalette(void);
void InitLightAndMaterial(void);
bool m_bWheel;
CPoint m_MouseDownPT;
bool m_bMouseDown;
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
//virtual BOOL PreTranslateMessage(MSG* pMsg);
afx_msg void OnDestroy();
};
MyOpenGL.cpp
#include "StdAfx.h"
#include "MyOpenGL.h"
#include <iostream>
#include <math.h>
#include <fstream>
using namespace std;
MyOpenGL::MyOpenGL(void)
{
m_hRC=NULL;
m_hDC=NULL;
cwnd=NULL;
m_tranlate[0] = 0;
m_tranlate[1] = 0;
m_tranlate[2] = -10;
m_rorate[0]=-45;
m_rorate[1]=0;
m_rorate[2]=-134;
m_scale = 0.5;
m_bWheel=false;
m_MouseDownPT.x = 0;
m_MouseDownPT.y = 0;
m_bMouseDown = false;
}
MyOpenGL::~MyOpenGL(void)
{
wglMakeCurrent(NULL,NULL);
wglDeleteContext(m_hRC);//删除渲染描述表
::ReleaseDC(m_hWnd,m_hDC);//释放设备描述表
}
BEGIN_MESSAGE_MAP(MyOpenGL, CWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_MOUSEWHEEL()
ON_WM_DESTROY()
END_MESSAGE_MAP()
int MyOpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创