📄 minglview.cpp
字号:
// MinGlView.cpp : implementation of the CMinGlView class
//
#include "stdafx.h"
#include "MinGl.h"
#include "MinGlDoc.h"
#include "MinGlView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#pragma comment(lib, "OpenGL32.lib")
#pragma comment(lib, "glu32.lib")
#include "GL\gl.h"
#include "GL\glu.h"
/////////////////////////////////////////////////////////////////////////////
// CMinGlView
IMPLEMENT_DYNCREATE(CMinGlView, CView)
BEGIN_MESSAGE_MAP(CMinGlView, CView)
//{{AFX_MSG_MAP(CMinGlView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMinGlView construction/destruction
CMinGlView::CMinGlView()
{
// TODO: add construction code here
m_pDC = NULL;
}
CMinGlView::~CMinGlView()
{
}
BOOL CMinGlView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
//设置 OpenGL 窗口风格, windows NT 下是必须的
cs.style |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMinGlView drawing
void CMinGlView::OnDraw(CDC* pDC)
{
DrawScene();
}
/////////////////////////////////////////////////////////////////////////////
// CMinGlView diagnostics
#ifdef _DEBUG
void CMinGlView::AssertValid() const
{
CView::AssertValid();
}
void CMinGlView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMinGlDoc* CMinGlView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMinGlDoc)));
return (CMinGlDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMinGlView message handlers
//设置像素格式
BOOL CMinGlView::SetupPixelFormat()
{
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL, // support OpenGL
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
};
//选择硬件所支持的像素格式与 pfd 最接近的一个
int nPixelformat;
if ( (nPixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )
{
MessageBox("ChoosePixelFormat failed");
return FALSE;
}
//设置已经匹配的像素格式
if (SetPixelFormat(m_pDC->GetSafeHdc(), nPixelformat, &pfd) == FALSE)
{
MessageBox("SetPixelFormat failed");
return FALSE;
}
return TRUE;
}
//初始化OpenGL
void CMinGlView::Init()
{
PIXELFORMATDESCRIPTOR pfd;
m_pDC = new CClientDC(this);
ASSERT(m_pDC != NULL);
//设置像素格式
if(!SetupPixelFormat())return;
//测试并描述像素格式
int nIndex = GetPixelFormat(m_pDC->GetSafeHdc());
DescribePixelFormat(m_pDC->GetSafeHdc(), nIndex, sizeof(pfd), &pfd);
//创建着色描述表
HGLRC hrc = wglCreateContext(m_pDC->GetSafeHdc());
//当前化着色描述表
wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);
//允许深度测试, 深度初始值为远剪切面深度(规格化坐标系中)
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
}
int CMinGlView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
//初始化
Init();
return 0;
}
void CMinGlView::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
//获取当前着色描述表句柄
HGLRC hrc = wglGetCurrentContext();
//撤消与着色描述表关联的着色任务
wglMakeCurrent(NULL, NULL);
//释放
if(hrc)wglDeleteContext(hrc);
if(m_pDC) delete m_pDC;
}
void CMinGlView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
//启动透视变换矩阵
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//设置视景体
glFrustum(-1.0, 1.0, -1.0, 1.0, 3.0, 7.0);
//设置视口
glViewport(0, 0, (GLsizei)cx, (GLsizei)cy);
}
void CMinGlView::DrawScene()
{
//启动模型变换矩阵, 并单位化
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//将景物移至视景体之中
glTranslatef(0.0f, 0.0f, -4.5f);
//颜色缓冲区清除颜色为白色
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
//同时清除颜色缓冲区和深度缓冲区
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//绘制一条直线
glBegin(GL_LINES);
//颜色
glColor3f(1.0f, 0.0f, 0.0f);
//直线两端点坐标
glVertex3f(-0.8f, 0.8f, 0.5f);
glVertex3f(0.8f, -0.8f, 0.5f);
glEnd();
//完成绘制指令后交换缓冲区
glFinish();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -