在MFC中使用OpenGL相关配置的整理总结
来源:互联网 发布:网络与新媒体概论考研 编辑:程序博客网 时间:2024/05/21 05:20
本文参考了 【1】 Megabyte 的OpenGL教程 http://www.mbsoftworks.sk/index.php?page=tutorials&series=1
HGLRC m_hGLContext;//OpenGL的RC句柄BOOL bGlewInitialized;//初始化glew的判断标志BOOL InitGLEW(HDC);//首先初始化glewBOOL CreateViewGLContext(HDC);//建立GL的RC</span>
在CPP文件中,为函数添加代码,首先是glew的初始化函数。需要注意的是,glew的初始化需要有已经建立的RC,所以我们先设置相关像素格式建立RC,初始化glew后再销毁掉。如果没有销毁的话,此时已经建立起RC可以使用OpenGL1.1了,我们需要使用更高版本的GL特性,则需要销毁掉,重新建立。
BOOL GLWnd::InitGLEW(HDC hDC){ PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize= sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 32; pfd.iLayerType = PFD_MAIN_PLANE; int iPixelFormat = ChoosePixelFormat(hDC, &pfd); if (iPixelFormat == 0)return false; if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false; // Create the false, old style context (OpenGL 2.1 and before) HGLRC hRCFake = wglCreateContext(hDC); wglMakeCurrent(hDC, hRCFake); bool bResult = true; if(!bGlewInitialized) { if(glewInit() != GLEW_OK) { MessageBox(L"Couldn't initialize GLEW!", L"Fatal Error", MB_ICONERROR); bResult = false; } bGlewInitialized = true; } wglMakeCurrent(NULL, NULL); wglDeleteContext(hRCFake); return bResult;}
接着,我们为CreateViewGLContext(HDC)创建代码
BOOL GLWnd::CreateViewGLContext(HDC hDC){//首先初始化,glew库。if(!InitGLEW(hDC))return false; bool bError = false; PIXELFORMATDESCRIPTOR pfd; int GL_Version=4;//要使用的OpenGL版本//设置像素格式,注意OpenGL 2.1以前的设置方法,与更新版本的设置方式是不同的if(GL_Version<=2){memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nSize= sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 32; pfd.cDepthBits = 32; pfd.iLayerType = PFD_MAIN_PLANE; int iPixelFormat = ChoosePixelFormat(hDC, &pfd); if (iPixelFormat == 0)return false; if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false; printf("Create the old style context (OpenGL 2.1 and before)\n");// Create the old style context (OpenGL 2.1 and before)m_hGLContext = wglCreateContext(hDC); if(m_hGLContext)wglMakeCurrent(hDC, m_hGLContext); else bError = true; }else if(WGLEW_ARB_create_context && WGLEW_ARB_pixel_format){const int iPixelFormatAttribList[] = { WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_SUPPORT_OPENGL_ARB, GL_TRUE, WGL_DOUBLE_BUFFER_ARB, GL_TRUE, WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, WGL_COLOR_BITS_ARB, 32, WGL_DEPTH_BITS_ARB, 24, WGL_STENCIL_BITS_ARB, 8, 0 // End of attributes list }; int iContextAttribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, WGL_CONTEXT_MINOR_VERSION_ARB, 1, WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, 0 // End of attributes list }; int iPixelFormat, iNumFormats; wglChoosePixelFormatARB(hDC, iPixelFormatAttribList, NULL, 1, &iPixelFormat, (UINT*)&iNumFormats); // PFD seems to be only redundant parameter now if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false; m_hGLContext = wglCreateContextAttribsARB(hDC, 0, iContextAttribs); // If everything went OK if(m_hGLContext) {wglMakeCurrent(hDC, m_hGLContext); printf("创建GL渲染环境");} else bError = true; }else bError = true;if(bError) { // Generate error messages char sErrorMessage[255], sErrorTitle[255]; sprintf(sErrorMessage, "OpenGL %d.%d is not supported! Please download latest GPU drivers!", 3, 1); sprintf(sErrorTitle, "OpenGL %d.%d Not Supported", 3, 1); MessageBoxA(NULL,sErrorMessage, sErrorTitle, MB_ICONINFORMATION); return false; }return TRUE;}
这样就可以建立OpenGL的环境了,在窗口的OnCreate()函数中,使用如下代码:
<span style="white-space:pre"></span>CClientDC dc(this);CreateViewGLContext(dc.mhdc);
0 0
- 在MFC中使用OpenGL相关配置的整理总结
- OpenGL在MFC中配置
- OpenGL在MFC中的使用总结(二)——派生的OpenGL类
- MFC中配置OpenGL的步骤
- MFC中使用OPENGL
- 在VS集成IDE中使用OpenGL的配置方法
- mfc中使用OpenGL的方法
- MFC的对话框中使用OpenGL绘图
- MFC的对话框中使用OpenGL绘图
- OpenGL的配置以及在MFC框架上的使用(附上源代码)
- opencv中在静态库中使用MFC的配置
- MFC 中配置OpenGL环境
- 在MFC中的picture control 中使用opengl作图
- 在MFC应用程序中遇到的字符集问题(整理总结)
- java在SAE中使用的相关问题总结
- MFC中配置OpenGL所遇到的一些问题
- 在MFC下使用OpenGL的一个简单的例子
- 在MFC下使用OpenGL的一个简单的例子
- Node.JS核心模块之全局对象和常用工具
- 网络策略(networkpolicy随笔记)
- 百度地图开发(三)之地图控制 + 定位
- 三点顺序acm68
- 编辑PDF文件内容的方法
- 在MFC中使用OpenGL相关配置的整理总结
- 关于并发编程的疑问解析
- postgresql查看死锁
- 作用取得客户端的ip、地理信息、浏览器、本地真实IP
- windows 系统 oracle监听无法启动。
- CentOS 7.0安装配置Vsftp服务器
- 杭电OJ 2602 典型01背包问题
- Unity3D开发敌人自动攻击和自动寻路
- 偶然碰到MEF