📄 clipplaneview.cpp
字号:
// ClipPlaneView.cpp : implementation of the CClipPlaneView class
//
#include "stdafx.h"
#include "ClipPlane.h"
#include "ClipPlaneDoc.h"
#include "ClipPlaneView.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
/////////////////////////////////////////////////////////////////////////////
// CClipPlaneView
IMPLEMENT_DYNCREATE(CClipPlaneView, CView)
BEGIN_MESSAGE_MAP(CClipPlaneView, CView)
//{{AFX_MSG_MAP(CClipPlaneView)
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()
/////////////////////////////////////////////////////////////////////////////
// CClipPlaneView construction/destruction
CClipPlaneView::CClipPlaneView()
{
//add dowm
m_pDC = NULL;
//add up
}
CClipPlaneView::~CClipPlaneView()
{
}
BOOL CClipPlaneView::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);
}
/////////////////////////////////////////////////////////////////////////////
// CClipPlaneView drawing
void CClipPlaneView::OnDraw(CDC* pDC)
{
CClipPlaneDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//add down
DrawScene();
//add up
}
/////////////////////////////////////////////////////////////////////////////
// CClipPlaneView printing
BOOL CClipPlaneView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CClipPlaneView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CClipPlaneView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CClipPlaneView diagnostics
#ifdef _DEBUG
void CClipPlaneView::AssertValid() const
{
CView::AssertValid();
}
void CClipPlaneView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CClipPlaneDoc* CClipPlaneView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CClipPlaneDoc)));
return (CClipPlaneDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CClipPlaneView message handlers
int CClipPlaneView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); // initialize OpenGL
//add up
return 0;
}
void CClipPlaneView::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 CClipPlaneView::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(45.0f, (GLdouble)cx/cy, 3.0f, 7.0f);
glMatrixMode(GL_MODELVIEW);
}
//add up
}
BOOL CClipPlaneView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
//add down
void CClipPlaneView::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 = 3.0f;
fFarPlane = 7.0f;
fMaxObjSize = 3.0f;
m_fRadius = fNearPlane + fMaxObjSize / 2.0f;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//根据客户区的初始值定透视投影
gluPerspective(45.0f, fAspect, fNearPlane, fFarPlane);
glMatrixMode(GL_MODELVIEW);
}
BOOL CClipPlaneView::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 CClipPlaneView::DrawScene(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0f, 0.0f, -m_fRadius);
//定义数组,定义了一个裁减平面:x+y<0
double equation[4]={1.0,1.0,0.0,0.0};
//指定图形的裁剪平面
glClipPlane(GL_CLIP_PLANE0,equation);
//使裁剪平面生效
glEnable(GL_CLIP_PLANE0);
//绘制茶壶,但是该图形被上面定义的裁减平面给裁减去了下面的部分
glColor3f(1.0f, 0.0f, 1.0f);
auxWireTeapot(0.5f);
glPopMatrix();
glFinish();
SwapBuffers(wglGetCurrentDC());
}
//add up
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -