博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在多文档应用程序中使用OpenGL绘图
阅读量:6268 次
发布时间:2019-06-22

本文共 3263 字,大约阅读时间需要 10 分钟。

 
1、在视图类的头文件中添加两个成员变量:
1: public:
2:     int m_GLPixelIndex;
3:     HGLRC m_hGLContext;

2、在视图类的构造函数中对这两个成员进行初始化:

1: m_GLPixelIndex = 0;
2: m_hGLContext = NULL;

3、在视图类声明中添加两个成员函数:

1: BOOL SetWindowPixelFormat(HDC hDC);
2: BOOL CreateViewGLContext(HDC hDC);

4、在视图类的实现文件中添加这两个成员函数的实现:

1: BOOL CXXXView::CreateViewGLContext(HDC hDC)
2: {
3:     m_hGLContext = wglCreateContext(hDC);
4: 
5:     if(m_hGLContext==NULL)
6:         return FALSE;
7: 
8:     if(wglMakeCurrent(hDC,m_hGLContext)==FALSE)
9:         return FALSE;
10: 
11:     return TRUE;
12: }

以及

1: BOOL CXXXView::SetWindowPixelFormat(HDC hDC)
2: {
3:     PIXELFORMATDESCRIPTOR pixelDesc;
4: 
5:     pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
6:     pixelDesc.nVersion = 1;
7: 
8:     pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
9:         PFD_DOUBLEBUFFER | PFD_STEREO_DONTCARE;
10: 
11:     pixelDesc.iPixelType = PFD_TYPE_RGBA;
12:     pixelDesc.cColorBits = 32;
13:     pixelDesc.cRedBits = 8;
14:     pixelDesc.cRedShift = 16;
15:     pixelDesc.cGreenBits = 8;
16:     pixelDesc.cGreenShift = 8;
17:     pixelDesc.cBlueBits = 8;
18:     pixelDesc.cBlueShift = 0;
19:     pixelDesc.cAlphaBits = 0;
20:     pixelDesc.cAlphaShift = 0;
21:     pixelDesc.cAccumBits = 64;
22:     pixelDesc.cAccumRedBits = 16;
23:     pixelDesc.cAccumGreenBits = 16;
24:     pixelDesc.cAccumBlueBits = 16;
25:     pixelDesc.cAccumAlphaBits = 0;
26:     pixelDesc.cDepthBits = 32;
27:     pixelDesc.cStencilBits = 8;
28:     pixelDesc.cAuxBuffers = 0;
29:     pixelDesc.iLayerType = PFD_MAIN_PLANE;
30:     pixelDesc.bReserved = 0;
31:     pixelDesc.dwLayerMask = 0;
32:     pixelDesc.dwVisibleMask = 0;
33:     pixelDesc.dwDamageMask = 0;
34: 
35:     m_GLPixelIndex = ChoosePixelFormat(hDC,&pixelDesc);
36:     if(m_GLPixelIndex == 0) // Choose default
37:     {
38:         m_GLPixelIndex = 1;
39:         if(DescribePixelFormat(hDC,m_GLPixelIndex,
40:             sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0)
41:             return FALSE;
42:     }
43: 
44:     if(!SetPixelFormat(hDC,m_GLPixelIndex,&pixelDesc))
45:         return FALSE;
46: 
47:     return TRUE;
48: }

在视图类的OnCreate成员函数中添加:

1: HWND hWnd = GetSafeHwnd();
2: HDC hDC = ::GetDC(hWnd);
3: 
4: if(SetWindowPixelFormat(hDC)==FALSE)
5:     return -1;
6: 
7: if(CreateViewGLContext(hDC)==FALSE)
8:     return -1;

添加OnPaint消息响应函数,在函数中加入:

1: HWND hWnd = GetSafeHwnd();
2: HDC hDC = ::GetDC(hWnd);
3: wglMakeCurrent(dc.m_ps.hdc,m_hGLContext);
4: 
5: ....
6: 
7: // Double buffer
8: SwapBuffers(dc.m_ps.hdc);
9: glFlush();
10: 
11: // release
12: ::ReleaseDC(hWnd,hDC);

添加OnSize消息响应函数。这里的内容可以根据需要进行修改:

1: glViewport(0,0,(GLsizei)cx,(GLsizei)cy);
2: glMatrixMode(GL_PROJECTION);
3: glLoadIdentity();
4: gluPerspective(60.0,(GLfloat)cx/(GLfloat)cy,0.1,100.0);
5: glMatrixMode(GL_MODELVIEW);
6: glLoadIdentity();

添加虚函数OnActivateView,在其中添加:

1: HWND hWnd = GetSafeHwnd();
2: HDC hDC = ::GetDC(hWnd);
3: wglMakeCurrent(hDC,m_hGLContext);
4: ::ReleaseDC(hWnd,hDC);

添加WM_DESTROY消息响应函数,在其中添加:

1: if(wglGetCurrentContext() != NULL)
2:     wglMakeCurrent(NULL,NULL);
3: 
4: if(m_hGLContext != NULL)
5: {
6:     wglDeleteContext(m_hGLContext);
7:     m_hGLContext = NULL;
8: }

Over。

转载于:https://www.cnblogs.com/youthlion/archive/2012/01/20/2327973.html

你可能感兴趣的文章
Mono for Android 优势与劣势
查看>>
服务器端开发技术
查看>>
Python3中urllib详细使用方法(header,代理,超时,认证,异常处理)
查看>>
ajax提交多个对象,使用序列化表单和FormData
查看>>
深入分析由前序和中序重构二叉树问题
查看>>
leetcode 题解 || Valid Parentheses 问题
查看>>
将图片转成base64字符串并在JSP页面显示的Java代码
查看>>
什么是WeakHashMap--转
查看>>
js 面试题
查看>>
第二十二节,三元运算
查看>>
Yacc 与 Lex 快速入门
查看>>
Unity中HDR外发光的使用
查看>>
Flume负载均衡配置
查看>>
Ajax详解
查看>>
Ubuntu C/C++开发环境的安装和配置
查看>>
百世汇通快递地区选择插件,单独剥离
查看>>
Linux系统调用---同步IO: sync、fsync与fdatasync【转】
查看>>
【MyBatis学习06】输入映射和输出映射
查看>>
[LeetCode] Decode String 解码字符串
查看>>
数字逻辑的一些基本运算和概念
查看>>