📄 viewportview.cpp
字号:
// ViewPortView.cpp : implementation of the CViewPortView class
//
#include "stdafx.h"
#include "ViewPort.h"
#include "ViewPortDoc.h"
#include "ViewPortView.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
/////////////////////////////////////////////////////////////////////////////
// CViewPortView
IMPLEMENT_DYNCREATE(CViewPortView, CView)
BEGIN_MESSAGE_MAP(CViewPortView, CView)
//{{AFX_MSG_MAP(CViewPortView)
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()
/////////////////////////////////////////////////////////////////////////////
// CViewPortView construction/destruction
CViewPortView::CViewPortView()
{
//add down
m_pDC = NULL;
//add up
}
CViewPortView::~CViewPortView()
{
}
BOOL CViewPortView::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);
}
/////////////////////////////////////////////////////////////////////////////
// CViewPortView drawing
void CViewPortView::OnDraw(CDC* pDC)
{
CViewPortDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//add down
DrawScene();
//add up
}
/////////////////////////////////////////////////////////////////////////////
// CViewPortView printing
BOOL CViewPortView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CViewPortView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CViewPortView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CViewPortView diagnostics
#ifdef _DEBUG
void CViewPortView::AssertValid() const
{
CView::AssertValid();
}
void CViewPortView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CViewPortDoc* CViewPortView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CViewPortDoc)));
return (CViewPortDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CViewPortView message handlers
int CViewPortView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); //初始化OpenGL
//add up
return 0;
}
void CViewPortView::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 CViewPortView::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 CViewPortView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
//add down
void CViewPortView::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 CViewPortView::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 CViewPortView::DrawScene(void)
{
glPushMatrix();
//使用黑色清屏
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
//使用当前定义的清屏颜色,清除颜色缓存和深度缓存
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//设置视口的尺寸到窗口的大小
glViewport(50,50,150,150);
//重新设置坐标系统
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//建立剪切体
gluPerspective(90.0,1.0,1.0,200);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//保存矩阵的状态
glPushMatrix();
//旋转、平移一定的程度
glTranslatef(0.0,0.0,-100);
glRotatef(45.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0);
//绘制一个茶壶
auxWireTeapot(40.0);
//弹出当前的矩阵
glPopMatrix();
//同上面建立另一个物体的视口
glPopMatrix();
glPushMatrix();
glViewport(200,200,100,100);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//建立剪切体
gluPerspective(90.0,1.0,1.0,200);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//保存矩阵的状态
glPushMatrix();
//旋转、平移一定的程度
glTranslatef(0.0,0.0,-40);
glRotatef(45.0,1.0,1.0,1.0);
glColor3f(0.0,1.0,0.0);
//绘制一个球体
auxWireSphere(20.0);
//弹出当前的矩阵
glPopMatrix();
glPopMatrix();
glFinish();
SwapBuffers(wglGetCurrentDC());
}
//add up
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -