?? renderview.cpp
字號:
// RenderView.cpp
// Created : 05/17/1999
// Modified: 10/26/1999
#include "stdafx.h"
#include "MyTemplate.h"
#include <GL/glaux.h>
#include <math.h>
#include "MyTemplateDoc.h"
#include "RenderView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// CRenderView
IMPLEMENT_DYNCREATE(CRenderView, CView)
BEGIN_MESSAGE_MAP(CRenderView, CView)
//{{AFX_MSG_MAP(CRenderView)
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_PAINT()
ON_WM_TIMER()
ON_WM_CREATE()
//}}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()
// CRenderView構(gòu)造函數(shù)/destruction
CRenderView::CRenderView()
{
// OpenGL
m_hGLContext = NULL;
m_GLPixelIndex = 0;
// Mouse
m_LeftButtonDown = FALSE;
m_RightButtonDown = FALSE;
m_CursorRotation = AfxGetApp()->LoadCursor(IDC_CURSOR_ROTATION);
// Colors
CMyTemplateApp *pApp = (CMyTemplateApp *)AfxGetApp();
InitGeometry();
}
// CRenderView析構(gòu)函數(shù)
CRenderView::~CRenderView()
{
}
// 初始化一些全局變量
void CRenderView::InitGeometry(void)
{
m_xRotation = 0.0f;
m_yRotation = 0.0f;
m_xTranslation = 0.0f;
m_yTranslation = 0.0f;
m_zTranslation = -3.0f;
m_xScaling = 0.5f;
m_yScaling = 0.5f;
m_zScaling = 0.5f;
}
BOOL CRenderView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
void CRenderView::OnDraw(CDC* pDC)
{
CMyTemplateDoc *pDoc = GetDocument();
ASSERT(pDoc);
}
// CRenderView打印
BOOL CRenderView::OnPreparePrinting(CPrintInfo* pInfo)
{
return DoPreparePrinting(pInfo);
}
// 開始打印
void CRenderView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}
// 結(jié)束打印
void CRenderView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
}
// CRenderView診斷
#ifdef _DEBUG
void CRenderView::AssertValid() const
{
CView::AssertValid();
}
void CRenderView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyTemplateDoc* CRenderView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyTemplateDoc)));
return (CMyTemplateDoc*)m_pDocument;
}
#endif //_DEBUG
// 類的創(chuàng)建函數(shù)
// 可以在其中創(chuàng)建OpenGL的繪制描述表
int CRenderView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
HWND hWnd = GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);
if(SetWindowPixelFormat(hDC)==FALSE)
return 0;
if(CreateViewGLContext(hDC)==FALSE)
return 0;
// 設(shè)置多邊形繪制的缺省模式
glPolygonMode(GL_FRONT,GL_LINE);
glPolygonMode(GL_BACK,GL_LINE);
glShadeModel(GL_FLAT);
glEnable(GL_NORMALIZE);
// 設(shè)置光照及材質(zhì)的屬性
GLfloat ambientProperties[] = {0.5f, 0.5f, 0.5f, 1.0f};
GLfloat diffuseProperties[] = {0.8f, 0.8f, 0.8f, 1.0f};
GLfloat specularProperties[] = {0.0f, 0.8f, 0.2f, 1.0f};
glClearDepth( 1.0f );
m_ClearColorRed=1.0f;
m_ClearColorGreen=1.0f;
m_ClearColorBlue=1.0f;
glLightfv( GL_LIGHT0, GL_AMBIENT, ambientProperties);
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseProperties);
glLightfv( GL_LIGHT0, GL_SPECULAR, specularProperties);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0f);
// 缺省情況下加光照
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(m_ClearColorRed,m_ClearColorGreen,m_ClearColorBlue,1.0f);
// 設(shè)置定時(shí)器為0.1秒
SetTimer( 0, 100, NULL);
return 0;
}
// 設(shè)置窗口的象素格式
BOOL CRenderView::SetWindowPixelFormat(HDC hDC)
{
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_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);
if(m_GLPixelIndex == 0) // Choose default
{
m_GLPixelIndex = 1;
if(DescribePixelFormat(hDC,m_GLPixelIndex,
sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
return FALSE;
}
if(!SetPixelFormat(hDC,m_GLPixelIndex,&pixelDesc))
return FALSE;
return TRUE;
}
// 創(chuàng)建一個(gè)OpenGL繪制描述表
BOOL CRenderView::CreateViewGLContext(HDC hDC)
{
m_hGLContext = wglCreateContext(hDC);
if(m_hGLContext==NULL)
return FALSE;
if(wglMakeCurrent(hDC,m_hGLContext)==FALSE)
return FALSE;
return TRUE;
}
// 清除所有OpenGL繪制描述表
// 銷毀函數(shù)
void CRenderView::OnDestroy()
{
if(wglGetCurrentContext() != NULL)
wglMakeCurrent(NULL,NULL);
if(m_hGLContext != NULL)
{
wglDeleteContext(m_hGLContext);
m_hGLContext = NULL;
}
CView::OnDestroy();
}
// 窗口大小變化響應(yīng)函數(shù)
void CRenderView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// 設(shè)置OpenGL投影、視口和矩陣模式
CSize size(cx,cy);
glViewport(0,0,size.cx,size.cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0f, 1.0f, 128.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDrawBuffer(GL_BACK);
glEnable(GL_DEPTH_TEST);
}
void CRenderView::OnLButtonDown(UINT nFlags,
CPoint point)
{
m_LeftButtonDown = TRUE;
m_LeftDownPos = point;
CView::OnLButtonDown(nFlags, point);
}
void CRenderView::OnLButtonUp(UINT nFlags,
CPoint point)
{
m_LeftButtonDown = FALSE;
CView::OnLButtonUp(nFlags, point);
}
void CRenderView::OnMouseMove(UINT nFlags,
CPoint point)
{
if(m_LeftButtonDown)
{
m_xRotation += (float)(point.y - m_LeftDownPos.y) / 3.0f;
m_yRotation += (float)(point.x - m_LeftDownPos.x) / 3.0f;
m_LeftDownPos = point;
InvalidateRect(NULL,FALSE);
}
CView::OnMouseMove(nFlags, point);
}
// 繪制函數(shù)
void CRenderView::OnPaint()
{
// 用于繪制的設(shè)備描述表
CPaintDC dc(this);
// Useful in singledoc templates
HWND hWnd = GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);
wglMakeCurrent(hDC,m_hGLContext);
DrawWithOpenGL();
// 使用雙緩存
SwapBuffers(hDC); //dc.m_ps.hdc
}
// 用定時(shí)器實(shí)現(xiàn)一段簡單的動畫,每幀繞X軸、Y軸分別旋轉(zhuǎn)1.0度
void CRenderView::OnTimer(UINT nIDEvent)
{
switch(nIDEvent)
{
case 0:
break;
case 1:
m_xRotation+=1.0f;
m_yRotation+=1.0f;
InvalidateRect(NULL,FALSE);
break;
default:
{}
}
}
void CRenderView::DrawWithOpenGL()
{
// 清除背景
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(m_ClearColorRed,m_ClearColorGreen,m_ClearColorBlue,1.0f);
glPushMatrix();
// 縮放、平移、旋轉(zhuǎn)變換
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(m_xScaling,m_yScaling,m_zScaling);
glTranslatef(m_xTranslation,m_yTranslation,m_zTranslation);
glRotatef(m_xRotation,1.0f,0.0f,0.0f);
glRotatef(m_yRotation,0.0f,1.0f,0.0f);
auxSolidSphere(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0f, 1.0f, 128.0);
glPopMatrix();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -