www.pudn.com > VC中OpenGL编程实现虚拟现实.rar > VRDemoView.cpp, change:2003-04-07,size:6987b
// VRDemoView.cpp : implementation of the CVRDemoView class
//
#include "stdafx.h"
#include "VRDemo.h"
#include "VRDemoDoc.h"
#include "VRDemoView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CVRDemoView
IMPLEMENT_DYNCREATE(CVRDemoView, CView)
BEGIN_MESSAGE_MAP(CVRDemoView, CView)
//{{AFX_MSG_MAP(CVRDemoView)
ON_WM_MOUSEMOVE()
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_CREATE()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CVRDemoView construction/destruction
CVRDemoView::CVRDemoView()
{
// OpenGL
m_hGLRC = NULL;
// Mouse
m_nOldMx=0;
m_nOldMy=0;
}
CVRDemoView::~CVRDemoView()
{
m_cHouse.FreeHouseTexture();
m_cHouse.FreeHouseData();
}
BOOL CVRDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CVRDemoView drawing
void CVRDemoView::OnDraw(CDC* pDC)
{
CVRDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CVRDemoView printing
BOOL CVRDemoView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CVRDemoView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CVRDemoView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CVRDemoView diagnostics
#ifdef _DEBUG
void CVRDemoView::AssertValid() const
{
CView::AssertValid();
}
void CVRDemoView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CVRDemoDoc* CVRDemoView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CVRDemoDoc)));
return (CVRDemoDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CVRDemoView message handlers
// WM_MOUSEMOVE消息处理函数,该函数实现了鼠标移动漫游房屋
void CVRDemoView::OnMouseMove(UINT nFlags, CPoint point)
{
switch(nFlags)
{
case(MK_LBUTTON): // 鼠标左键按下时,场景前后移动
m_cHouse.ChangeViewPos(FORWARD,(GLfloat)(m_nOldMy-point.y)/5.0f);
break;
case(MK_RBUTTON): // 鼠标右键按下时,场景左右移动
m_cHouse.ChangeViewPos(LOOKHORIZONTAL, (GLfloat)(m_nOldMx-point.x));
break;
case(MK_MBUTTON): // 鼠标中键按下时,场景上下移动
m_cHouse.ChangeViewPos(LOOKVERTICAL, (GLfloat)(m_nOldMy-point.y));
break;
}
m_nOldMx = point.x;
m_nOldMy = point.y;
Invalidate(FALSE); // 引发WM_PAINT,调用OnPaint()重新绘制场景
CView::OnMouseMove(nFlags, point);
}
// WM_PAINT消息处理函数,负责绘制窗口
void CVRDemoView::OnPaint()
{
// 绘制用设备环境
CPaintDC dc(this);
// 对应RGB(164,200,238)
glClearColor(0.643f,0.784f,0.933f,1.0f);
glPushMatrix();
glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_ACCUM_BUFFER_BIT);
m_cHouse.InitDraw(); // 房屋对象设置绘制房屋所需参数
if(m_cGround.InitGround()) // 雪地对象设置绘制雪地所需参数
m_cGround.Draw(); // 绘制雪地
m_cHouse.Draw(); // 绘制房屋
// 交换前台和后台绘制缓冲区
SwapBuffers(dc);
}
// WM_SIZE消息处理函数,当窗口大小改变时重绘场景
void CVRDemoView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// 重新绘制场景
CSize size(cx,cy);
double aspect;
aspect = (cy == 0) ? (double)size.cx : (double)size.cx/(double)size.cy;
glViewport(0, 0, (GLsizei) cx, (GLsizei) cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) cx/(GLfloat) cy, 1.0f, 500.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (m_cHouse.m_ViewPos.ex, m_cHouse.m_ViewPos.ey, m_cHouse.m_ViewPos.ez, m_cHouse.m_ViewPos.cx, m_cHouse.m_ViewPos.cy, m_cHouse.m_ViewPos.cz, 0.0f, 1.0f, 0.0f);
}
// WM_CREATE消息处理函数,创建RC和DC并初始化雪地和房屋对象
int CVRDemoView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
HWND hWnd = GetSafeHwnd();// 得到当前窗口句柄
HDC hDC = ::GetDC(hWnd); // 得到设备环境
if(SetWindowPixelFormat(hDC)==FALSE) // 设置显示参数
return 0;
m_hGLRC = wglCreateContext(hDC); // 创建OpenGL设备环境
if(m_hGLRC==NULL)
return 0;
if(wglMakeCurrent(hDC,m_hGLRC)==FALSE) // 设置当前OpenGL设备环境
return 0;
m_cHouse.GetHouseData(); // 房屋对象加载房屋三维数据
m_cHouse.LoadHouseTexture(); // 房屋对象加载纹理位图数据
// 设置OpenGL绘制模式
glPolygonMode(GL_FRONT,GL_FILL);
glPolygonMode(GL_BACK,GL_FILL);
glShadeModel(GL_FLAT);
return 0;
}
// 设置显示分辨率、颜色数等参数
BOOL CVRDemoView::SetWindowPixelFormat(HDC hDC)
{
int m_nPixelIndex=0;
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER | PFD_STEREO_DONTCARE;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 32;
pixelDesc.cRedBits = 8;
pixelDesc.cRedShift = 16;
pixelDesc.cGreenBits = 8;
pixelDesc.cGreenShift = 8;
pixelDesc.cBlueBits = 8;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 64;
pixelDesc.cAccumRedBits = 16;
pixelDesc.cAccumGreenBits = 16;
pixelDesc.cAccumBlueBits = 16;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 32;
pixelDesc.cStencilBits = 8;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
m_nPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);
if(m_nPixelIndex == 0) // Choose default
{
m_nPixelIndex = 1;
if(DescribePixelFormat(hDC,m_nPixelIndex,
sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
return FALSE;
}
if(!SetPixelFormat(hDC,m_nPixelIndex,&pixelDesc))
return FALSE;
return TRUE;
}
// WM_DESTROY消息处理函数,释放资源
void CVRDemoView::OnDestroy()
{
CView::OnDestroy();
wglDeleteContext(m_hGLRC);
}