📄 teapotview.cpp
字号:
// TeapotView.cpp : CTeapotView 类的实现
//
#include "stdafx.h"
#include "Teapot.h"
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glaux.h"
#include "TeapotDoc.h"
#include "TeapotView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CTeapotView
IMPLEMENT_DYNCREATE(CTeapotView, CView)
BEGIN_MESSAGE_MAP(CTeapotView, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
// CTeapotView 构造/销毁
CTeapotView::CTeapotView()
{
// TODO: 在此处添加构造代码
m_pDC = NULL;
}
CTeapotView::~CTeapotView()
{
}
BOOL CTeapotView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改 CREATESTRUCT cs 来修改窗口类或
// 样式
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
return CView::PreCreateWindow(cs);
}
// CTeapotView 绘制
void CTeapotView::OnDraw(CDC* /*pDC*/)
{
CTeapotDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: 在此处为本机数据添加绘制代码
GLdouble eqn[4] = {1.0, 0.0, -1.0, 1.0};
GLfloat two_side_on[] = { GL_TRUE };
GLfloat two_side_off[] = { GL_FALSE };
GLfloat mat_diffuse[] = { 0.8, 0.8, 0.8, 1.0 };
GLfloat back_diffuse[] = { 0.8, 0.2, 0.8, 1.0 };
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
glClipPlane (GL_CLIP_PLANE0, eqn); /* slice objects */
glEnable (GL_CLIP_PLANE0);
glPushMatrix ();
glTranslatef (0.0, 2.0, 0.0);
auxSolidTeapot(1.0); /* one-sided lighting */
glPopMatrix ();
/* two-sided lighting, but same material */
glLightModelf (GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glMaterialfv (GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
glPushMatrix ();
glTranslatef (0.0, 0.0, 0.0);
auxSolidTeapot(1.0);
glPopMatrix ();
/* two-sided lighting, two different materials */
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv (GL_BACK, GL_DIFFUSE, back_diffuse);
glPushMatrix ();
glTranslatef (0.0, -2.0, 0.0);
auxSolidTeapot(1.0);
glPopMatrix ();
glLightModelf (GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
glDisable (GL_CLIP_PLANE0);
glPopMatrix ();
SwapBuffers(wglGetCurrentDC());
}
// CTeapotView 打印
BOOL CTeapotView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void CTeapotView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 打印前添加额外的初始化
}
void CTeapotView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 打印后添加清除过程
}
// CTeapotView 诊断
#ifdef _DEBUG
void CTeapotView::AssertValid() const
{
CView::AssertValid();
}
void CTeapotView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTeapotDoc* CTeapotView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTeapotDoc)));
return (CTeapotDoc*)m_pDocument;
}
#endif //_DEBUG
// CTeapotView 消息处理程序
int CTeapotView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: 在此添加您专用的创建代码
//初始化 OpenGL
PIXELFORMATDESCRIPTOR pfd;
int n;
HGLRC hrc;
m_pDC = new CClientDC(this);
ASSERT(m_pDC != NULL);
// if (!bSetupPixelFormat())
// return 0;
static PIXELFORMATDESCRIPTOR m_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(), &m_pfd)) == 0 )
{
MessageBox("ChoosePixelFormat failed");
return -1;
}
if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &m_pfd) == FALSE)
{
MessageBox("SetPixelFormat failed");
return -1;
}
n = ::GetPixelFormat(m_pDC->GetSafeHdc());
::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);
hrc = wglCreateContext(m_pDC->GetSafeHdc());
wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
/* light_position is NOT default value */
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glFrontFace (GL_CW);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
return 0;
}
void CTeapotView::OnDestroy()
{
CView::OnDestroy();
// TODO: 在此添加消息处理程序代码
HGLRC hrc;
hrc = ::wglGetCurrentContext();
::wglMakeCurrent(NULL, NULL);
if (hrc)
::wglDeleteContext(hrc);
if (m_pDC)
delete m_pDC;
//add up
}
void CTeapotView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: 在此添加消息处理程序代码
glViewport(0, 0, cx, cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (cx <= cy)
glOrtho (-4.0, 4.0, -4.0*(GLfloat)cy/(GLfloat)cx,
4.0*(GLfloat)cy/(GLfloat)cx, -10.0, 10.0);
else
glOrtho (-4.0*(GLfloat)cx/(GLfloat)cy,
4.0*(GLfloat)cx/(GLfloat)cy, -4.0, 4.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -