在多文档应用程序中使用OpenGL绘图
2012-01-20 15:59
381 查看
1、在视图类的头文件中添加两个成员变量:
[code] [code] public:
int m_GLPixelIndex;
HGLRC m_hGLContext;
[/code]
[/code]
2、在视图类的构造函数中对这两个成员进行初始化:
[code] [code] m_GLPixelIndex = 0;
m_hGLContext = NULL;
[/code]
[/code]
3、在视图类声明中添加两个成员函数:
[code] [code] BOOL SetWindowPixelFormat(HDC hDC);
BOOL CreateViewGLContext(HDC hDC);
[/code]
[/code]
4、在视图类的实现文件中添加这两个成员函数的实现:
[code] [code] BOOL CXXXView::CreateViewGLContext(HDC hDC)
{
m_hGLContext = wglCreateContext(hDC);
if(m_hGLContext==NULL)
return FALSE;
if(wglMakeCurrent(hDC,m_hGLContext)==FALSE)
return FALSE;
return TRUE;
}
[/code]
[/code]
以及
[code] [code] BOOL CXXXView::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER | PFD_STEREO_DONTCARE;
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) // Choose default
{
m_GLPixelIndex = 1;
if(DescribePixelFormat(hDC,m_GLPixelIndex,
sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
return FALSE;
}
if(!SetPixelFormat(hDC,m_GLPixelIndex,&pixelDesc))
return FALSE;
return TRUE;
}
[/code]
[/code]
在视图类的OnCreate成员函数中添加:
[code] [code] HWND hWnd = GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);
if(SetWindowPixelFormat(hDC)==FALSE)
return -1;
if(CreateViewGLContext(hDC)==FALSE)
return -1;
[/code]
[/code]
添加OnPaint消息响应函数,在函数中加入:
[code] [code] HWND hWnd = GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);
wglMakeCurrent(dc.m_ps.hdc,m_hGLContext);
....
// Double buffer
SwapBuffers(dc.m_ps.hdc);
glFlush();
// release
::ReleaseDC(hWnd,hDC);
[/code]
[/code]
添加OnSize消息响应函数。这里的内容可以根据需要进行修改:
[code] [code] glViewport(0,0,(GLsizei)cx,(GLsizei)cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0,(GLfloat)cx/(GLfloat)cy,0.1,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
[/code]
[/code]
添加虚函数OnActivateView,在其中添加:
[code] [code] HWND hWnd = GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);
wglMakeCurrent(hDC,m_hGLContext);
::ReleaseDC(hWnd,hDC);
[/code]
[/code]
添加WM_DESTROY消息响应函数,在其中添加:
[code] [code] if(wglGetCurrentContext() != NULL)
wglMakeCurrent(NULL,NULL);
if(m_hGLContext != NULL)
{
wglDeleteContext(m_hGLContext);
m_hGLContext = NULL;
}
[/code]
[/code]
Over。
相关文章推荐
- 在QT和SDL搭建的框架中使用OPENGL在SDL窗口上进行绘图
- 在Xcode中使用GLUT开发OpenGL应用程序
- Android官方开发文档Training系列课程中文版:OpenGL绘图之环境配置
- MFC的对话框中使用OpenGL绘图
- 在BlackBerry上使用OpenGL绘图(六):3D变换介绍
- 在office 2007中无法使用粘贴功能, 系统提示:该工程中的宏被禁止,请参阅联机帮助或主应用程序的文档来决定如何激活宏
- iOS 使用Quartz和OpenGL绘图
- 在Xcode中使用GLUT开发OpenGL应用程序
- iPhone开发基础教程笔记(十二)--第十二章 使用Quartz和OpenGL绘图
- 在BlackBerry上使用OpenGL绘图(七):视图变换:观察点的运动
- Android官方开发文档Training系列课程中文版:OpenGL绘图之响应触摸事件
- Windows桌面应用程序(1-2-4-6th) 使用Direct2D绘图
- QT使用GDI和OpenGL绘图,相比QPainter绘图降低CPU占用率
- Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作
- OpenGL学习脚印: 使用索引绘图(index drawing)
- 文档:使用Visula Studio 2008 创建Ogre应用程序
- 在MFC单文档的对话框中进行opengl绘图的框架!
- 使用opengl在windows窗体中绘图时闪烁的问题
- OpenGL的单文档应用程序框架
- •关于vc多文档应用中opengl的使用-(2013/09/18)