📄 matrixview.cpp
字号:
// MatrixView.cpp : implementation of the CMatrixView class
//
#include "stdafx.h"
#include "Matrix.h"
#include "MatrixDoc.h"
#include "MatrixView.h"
//add down
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glaux.h"
//add up
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMatrixView
IMPLEMENT_DYNCREATE(CMatrixView, CView)
BEGIN_MESSAGE_MAP(CMatrixView, CView)
//{{AFX_MSG_MAP(CMatrixView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CMatrixView construction/destruction
CMatrixView::CMatrixView()
{
//add down
m_pDC = NULL;
//add up
}
CMatrixView::~CMatrixView()
{
}
BOOL CMatrixView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
//add down
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
//add up
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMatrixView drawing
void CMatrixView::OnDraw(CDC* pDC)
{
CMatrixDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//add down
DrawScene();
//add up
}
/////////////////////////////////////////////////////////////////////////////
// CMatrixView printing
BOOL CMatrixView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMatrixView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMatrixView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMatrixView diagnostics
#ifdef _DEBUG
void CMatrixView::AssertValid() const
{
CView::AssertValid();
}
void CMatrixView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMatrixDoc* CMatrixView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMatrixDoc)));
return (CMatrixDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMatrixView message handlers
int CMatrixView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); //初始化OpenGL
//add up
return 0;
}
void CMatrixView::OnDestroy()
{
//add down
HGLRC hrc;
hrc = ::wglGetCurrentContext();
::wglMakeCurrent(NULL, NULL);
if (hrc)
::wglDeleteContext(hrc);
if (m_pDC)
delete m_pDC;
//add up
CView::OnDestroy();
}
void CMatrixView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
//add down
//设置投影剪切体随客户窗口的变化而变化
if(cy > 0)
{
glViewport(0, 0, cx, cy);
if((m_oldRect.right > cx) || (m_oldRect.bottom > cy))
RedrawWindow();
m_oldRect.right = cx;
m_oldRect.bottom = cy;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0f, (GLdouble)cx/cy, 1.0f, 200);
glMatrixMode(GL_MODELVIEW);
}
//add up
}
BOOL CMatrixView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
//add down
void CMatrixView::Init()
{
PIXELFORMATDESCRIPTOR pfd;
int n;
HGLRC hrc;
GLfloat fMaxObjSize, fAspect;
GLfloat fNearPlane, fFarPlane;
m_pDC = new CClientDC(this);
ASSERT(m_pDC != NULL);
if (!bSetupPixelFormat())
return;
n = ::GetPixelFormat(m_pDC->GetSafeHdc());
::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);
hrc = wglCreateContext(m_pDC->GetSafeHdc());
wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);
//根据客户区的初始值定透视投影
GetClientRect(&m_oldRect);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
if (m_oldRect.bottom)
fAspect = (GLfloat)m_oldRect.right/m_oldRect.bottom;
else // 如果m_oldRect.bottom为0
fAspect = 1.0f;
fNearPlane = 1.0f;
fFarPlane = 200;
fMaxObjSize = 3.0f;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0f, fAspect, fNearPlane, fFarPlane);
glMatrixMode(GL_MODELVIEW);
}
BOOL CMatrixView::bSetupPixelFormat()
{
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
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
};
int pixelformat;
if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )
{
MessageBox("ChoosePixelFormat failed");
return FALSE;
}
if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
{
MessageBox("SetPixelFormat failed");
return FALSE;
}
return TRUE;
}
void CMatrixView::DrawScene(void)
{
//使用黑色清屏
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//使用当前定义的清屏颜色,清除颜色缓存和深度缓存
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
//旋转、平移一定的程度
glTranslatef(0.0,0.0,-80);
glPushMatrix();
glColor3f(0.0,1.0,0.0);
glRotatef(45.0,1.0,0.0,0.0);
//绘制一个茶壶
auxWireTeapot(15.0);
//弹出当前的矩阵
glPopMatrix();
//压入另外一个矩阵堆栈
glPushMatrix();
//旋转、平移一定的程度
glTranslatef(-40.0,0.0,0.0);
glColor3f(1.0,0.0,0.0);
glRotatef(45.0,1.0,1.0,1.0);
//绘制另外一个茶壶
auxWireTeapot(15.0);
//弹出当前的矩阵
glPopMatrix();
glPopMatrix();
glFinish();
SwapBuffers(wglGetCurrentDC());
}
//add up
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -