1. MFC框架
2. 在WM里面去掉右键菜单
3. 注意开始时候视角后移...
4. 鼠标事件后要刷新图形 InvalidateRect
5. GetClientDC
6. 相应头文件放在stdafx.h里
7. 去掉Windows刷新
8. 防止内存泄漏
9.#pragma comment(lib,"opengl32.lib") 没有把对应的lib文件设置进去
----------------------------------------------------------------------------------------------------------------
以下转载
----------------------------------------------------------------------------------------------------------------
以下全部的操作均在CView类中完成
1.首先添加一个WM_CREATE消息, 相应消息代码如下
HWND hWnd = GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);if (SetWindowPixelFormat(hDC)==FALSE) {
return 0;
}
if (CreateViewGLContext(hDC)==FALSE) {
return 0;
}
2.我们可以看到其中有两个函数SetWindowPixelFormat和CreateViewGLContext, 他们的作用分别是设置窗口像素格式以及创建OpenGL上下文环境, 代码如下
BOOL CCal3DPlatView::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_STEREO_DONTCARE |
PFD_DOUBLEBUFFER;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 32;
pixelDesc.cRedBits = 8;
pixelDesc.cRedShift = 16;
pixelDesc.cGreenBits = 8;
pixelDesc.cGreenShift = 8;
pixelDesc.cBlueBits = 8;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 64;
pixelDesc.cAccumRedBits = 16;
pixelDesc.cAccumGreenBits = 16;
pixelDesc.cAccumBlueBits = 16;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 32;
pixelDesc.cStencilBits = 8;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
m_GLPixelIndex = ChoosePixelFormat(hDC, &pixelDesc);
if (m_GLPixelIndex == 0) {
m_GLPixelIndex = 1;
if (DescribePixelFormat(hDC,
m_GLPixelIndex,
sizeof(PIXELFORMATDESCRIPTOR),
&pixelDesc)==0) {
return FALSE;
}
}
if (SetPixelFormat(hDC,
m_GLPixelIndex,
&pixelDesc)==FALSE) {
return FALSE;
}
return TRUE;
}
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_STEREO_DONTCARE |
PFD_DOUBLEBUFFER;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 32;
pixelDesc.cRedBits = 8;
pixelDesc.cRedShift = 16;
pixelDesc.cGreenBits = 8;
pixelDesc.cGreenShift = 8;
pixelDesc.cBlueBits = 8;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 64;
pixelDesc.cAccumRedBits = 16;
pixelDesc.cAccumGreenBits = 16;
pixelDesc.cAccumBlueBits = 16;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 32;
pixelDesc.cStencilBits = 8;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
m_GLPixelIndex = ChoosePixelFormat(hDC, &pixelDesc);
if (m_GLPixelIndex == 0) {
m_GLPixelIndex = 1;
if (DescribePixelFormat(hDC,
m_GLPixelIndex,
sizeof(PIXELFORMATDESCRIPTOR),
&pixelDesc)==0) {
return FALSE;
}
}
if (SetPixelFormat(hDC,
m_GLPixelIndex,
&pixelDesc)==FALSE) {
return FALSE;
}
return TRUE;
}
BOOL CCal3DPlatView::CreateViewGLContext(HDC hDC)
{
m_hGLContext = wglCreateContext(hDC);
if (m_hGLContext == NULL) {
return FALSE;
}
if (wglMakeCurrent(hDC, m_hGLContext) == FALSE) {
return FALSE;
}
return TRUE;
}
{
m_hGLContext = wglCreateContext(hDC);
if (m_hGLContext == NULL) {
return FALSE;
}
if (wglMakeCurrent(hDC, m_hGLContext) == FALSE) {
return FALSE;
}
return TRUE;
}
3.最后, 只要添加ON_PAINT消息并在相应函数中编写普通的OpenGL代码即可, 以下代码中SwapBuffers函数是关键的一步, 由于使用了双缓存技术; 其余的函数均有各位编写, 功能如函数名所示
void CCal3DPlatView::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CView::OnPaint()
InitGLParameter();
CheckOrtho();
RenderScene();
SwapBuffers(dc.m_ps.hdc);
}
{
CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CView::OnPaint()
InitGLParameter();
CheckOrtho();
RenderScene();
SwapBuffers(dc.m_ps.hdc);
}