?? antilineview.cpp
字號:
// AntilineView.cpp : implementation of the CAntilineView class
//
#include "stdafx.h"
#include "Antiline.h"
#include "AntilineDoc.h"
#include "AntilineView.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
/////////////////////////////////////////////////////////////////////////////
// CAntilineView
IMPLEMENT_DYNCREATE(CAntilineView, CView)
BEGIN_MESSAGE_MAP(CAntilineView, CView)
//{{AFX_MSG_MAP(CAntilineView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_ERASEBKGND()
ON_WM_SIZE()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CAntilineView construction/destruction
CAntilineView::CAntilineView()
{
//add down
m_pDC = NULL;
//add up
}
CAntilineView::~CAntilineView()
{
}
BOOL CAntilineView::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);
}
/////////////////////////////////////////////////////////////////////////////
// CAntilineView drawing
void CAntilineView::OnDraw(CDC* pDC)
{
CAntilineDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//add down
DrawScene();
//add up
}
/////////////////////////////////////////////////////////////////////////////
// CAntilineView printing
BOOL CAntilineView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CAntilineView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CAntilineView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CAntilineView diagnostics
#ifdef _DEBUG
void CAntilineView::AssertValid() const
{
CView::AssertValid();
}
void CAntilineView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CAntilineDoc* CAntilineView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CAntilineDoc)));
return (CAntilineDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CAntilineView message handlers
int CAntilineView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); //初始化OpenGL
//add up
return 0;
}
void CAntilineView::OnDestroy()
{
//add down
HGLRC hrc;
hrc = ::wglGetCurrentContext();
::wglMakeCurrent(NULL, NULL);
if (hrc)
::wglDeleteContext(hrc);
if (m_pDC)
delete m_pDC;
//add up
CView::OnDestroy();
}
BOOL CAntilineView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CAntilineView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
//add down
int w=cx;
int h=cy;
//避免除數(shù)為0
if(h==0) h=1;
//設(shè)置視口與窗口匹配
glViewport(0,0,w,h);
//重新設(shè)置坐標(biāo)系統(tǒng)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//建立透視投影
gluPerspective(45.0,(GLfloat)w/(GLfloat)h,3.0,5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//將物體移到視窗內(nèi)
glTranslatef(0.0,0.0,-4.0);
//轉(zhuǎn)動物體
glRotatef(15.0,1.0,1.0,0.0);
//add up
}
//add down
void CAntilineView::Init()
{
PIXELFORMATDESCRIPTOR pfd;
int n;
HGLRC hrc;
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);
//實現(xiàn)反走樣和混合模式
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
//alpha模式下的反走樣
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE);
glLineWidth(5.0);
glShadeModel(GL_FLAT);
glClearColor(0.0,0.0,0.0,0.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
}
BOOL CAntilineView::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 CAntilineView::DrawScene(void)
{
//清除顏色緩沖區(qū)和深度緩沖區(qū)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//設(shè)置物體繪制顏色
glColor4f(0.0,0.6,1.0,1.0);
//繪制八面體
auxWireOctahedron(1.0);
//刷新屏幕,繪制圖形
glFinish();
SwapBuffers(wglGetCurrentDC());
}
//add up
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -