用MFC实现OpenGL编程

- #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
- #include <afxwin.h> // MFC core and standard components
- #include <afxext.h> // MFC extensions
- #include <gl/gl.h> // OpenGL32库的头文件
- #include <gl/glu.h> // GLu32库的头文件
- #include <gl/glaux.h> // GLaux库的头文件
- #ifndef _AFX_NO_AFXCMN_SUPPORT
- #include <afxcmn.h> // MFC support for Windows 95 Common Controls
- #endif // _AFX_NO_AFXCMN_SUPPORT
- //同样也可以使用下列方式连接lib
- #pragma comment( lib, "opengl32.lib") // OpenGL32连接库
- #pragma comment( lib, "glu32.lib") // GLu32连接库
- #pragma comment( lib, "glaux.lib") // GLaux连接库
- #pragma comment( lib, "glut32.lib") // GLut连接库
- BOOL COpenGlView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- cs.style |= (WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
- return CView::PreCreateWindow(cs);
- }
产生一个RC的第一步是定义窗口的像素格式。像素格式决定窗口着所显示的图形在内存中是如何表示的。由像素格式控制的参数包括:颜色深度、缓冲模式和所支持的绘画接口。在下面将有对这些参数的设置。我们先在COpenGLView的类中添加一个保护型的成员函数BOOL SetWindowPixelFormat(HDC hDC),并编辑其中的代码。
- BOOL COpenGLView::SetWindowPixelFormat(HDC hDC)
- {
- PIXELFORMATDESCRIPTOR pixelDesc;
- pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
- pixelDesc.nVersion = 1;
- pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
- PFD_DRAW_TO_BITMAP |
- PFD_SUPPORT_OPENGL |
- PFD_SUPPORT_GDI |
- 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) // Let's choose a default index.
- {
- 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;
- }
- int COpenGlView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: Add your specialized creation code here
- HWND hWnd = GetSafeHwnd();
- HDC hDC = ::GetDC(hWnd);
- if (SetWindowPixelFormat (hDC)==FALSE)
- return 0;
- if (CreateViewGLContext (hDC)==FALSE)
- return 0;
- return 0;
- }
5、代码解释
- BOOL COpenGlView::CreateViewGLContext(HDC hDC)
- {
- m_hGLContext = wglCreateContext(hDC);//用当前DC产生绘制环境(RC)
- if (m_hGLContext == NULL)
- {
- return FALSE;
- }
- if (wglMakeCurrent(hDC, m_hGLContext)==FALSE)
- {
- return FALSE;
- }
- return TRUE;
- }
- int COpenGlView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: Add your specialized creation code here
- HWND hWnd = GetSafeHwnd();
- HDC hDC = ::GetDC(hWnd);
- if (SetWindowPixelFormat (hDC)==FALSE)
- return 0;
- if (CreateViewGLContext (hDC)==FALSE)
- return 0;
- return 0;
- }
- void COpenGlView::OnDestroy()
- {
- CView::OnDestroy();
- // TODO: Add your message handler code here
- if(wglGetCurrentContext()!=NULL)
- {
- // make the rendering context not current
- wglMakeCurrent(NULL, NULL) ;
- }
- if (m_hGLContext!=NULL)
- {
- wglDeleteContext(m_hGLContext);
- m_hGLContext = NULL;
- }
- }
- COpenGlView::COpenGlView()
- {
- // TODO: add construction code here
- m_hGLContext = NULL;
- m_GLPixelIndex = 0;
- }
- void COpenGlView::OnSize(UINT nType, int cx, int cy)
- {
- CView::OnSize(nType, cx, cy);
- // TODO: Add your message handler code here
- GLsizei width, height;
- GLdouble aspect;
- width = cx;
- height = cy;
- if (cy==0)
- aspect = (GLdouble)width;
- else
- aspect = (GLdouble)width/(GLdouble)height;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0.0, 500.0*aspect, 0.0, 500.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void COpenGlView::OnDraw(CDC* /*pDC*/)
- {
- COpenGlTestMFCDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- if (!pDoc)
- return;
- // TODO: add draw code for native data here
- glLoadIdentity();
- glClear(GL_COLOR_BUFFER_BIT);
- glBegin(GL_POLYGON);
- glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
- glVertex2f(100.0f, 50.0f);
- glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
- glVertex2f(450.0f, 400.0f);
- glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
- glVertex2f(450.0f, 50.0f);
- glEnd();
- glFlush();
- }
用MFC实现OpenGL编程的更多相关文章
- [转]VS 2012环境下使用MFC进行OpenGL编程
我就不黏贴复制了,直接给出原文链接:VS 2012环境下使用MFC进行OpenGL编程 其它好文链接: 1.OpenGL系列教程之十二:OpenGL Windows图形界面应用程序
- Win32 OpenGL 编程( 1 ) Win32 下的 OpenGL 编程必须步骤
http://blog.csdn.net/vagrxie/article/details/4602961 Win32 OpenGL 编程( 1 ) Win32 下的 OpenGL 编程必须步骤 wri ...
- OpenGL编程指南(第七版)
OpenGL编程指南(第七版) 转自:http://blog.csdn.net/w540982016044/article/details/21287645 在接触OpenGL中,配置显得相当麻烦,特 ...
- 编译opengl编程指南第八版示例代码通过
最近在编译opengl编程指南第八版的示例代码,如下 #include <iostream> #include "vgl.h" #include "LoadS ...
- MFC下OpenGL入门(可以用)
MFC下OpenGL入门 源文件 1, 建一工程文件,我这里命名为first,现在first工程里面我们没有添加任何东西,所有的东西都是MFC自动帮我们创建的. 2, 添加链接库.这一步很关键.打开菜 ...
- 在 Mac OS X Yosemite 10.10.5 上配置 OpenGL 编程环境
这个教程主要参考了youtube上的视频 Getting Started in OpenGL with GLFW/GLEW in Xcode 6 ,这个视频有点问题,不能照搬.本人通过自己摸(瞎)索( ...
- MFC下DLL编程(图解)
MFC下DLL编程(图解) DLL(Dynamic Link Library,动态链接库)是微软公司为Windows和OS/2操作系统设计一种供应用程序在运行时调用的共享函数库.DLL是应用程序的一种 ...
- 基于MFC的socket编程
网络编程 1.windows 套接字编程(开放的网络编程接口)添加头文件#include<windows.h> 2.套接字及其分类 socket分为两种:(1)数据报socket:无连接套 ...
- MFC控件编程进度条编写
MFC控件编程进度条编写 一丶进度条编程需要用到的方法 进度条MFC已经帮我们封装好类了. 叫做 CProgressCtrl 进度条编程也很简单. 封装的方法也就那个那几个. GetPos() 获 ...
随机推荐
- ruby和Python简单对比
前不久学了ruby,发现ruby和Python非常像,于是自个测试对比了下,测完了才知道网上有现成的……下面是测试结果 序列(包括列表和元组等)有分片的特点:可能会比较方便的提取其中特定元素,暂时 ...
- Linux下Nginx+PHP 简单安装配置
测试环境 Linux 2.6.18nginx-1.0.4 http://www.nginx.org/php-5.3.6 http://www.php.net/ 一,安装Nginxwget http:/ ...
- Easyui tabs学习
前端时间花了一些时间学习easy ui,这个东西非常好用,界面也很美观,你都不需要在界面上花太多的工夫,例子程序也比较完善,基本上看下例子就能很好的使用easyui了,很方便. 特地分享一些使用时候遇 ...
- 弹出框、遮罩层demo
仿alert.confirm的弹出框. 弹出后,用遮罩层将背景虚化. 代码如下: <html> <meta http-equiv="Content-Type" c ...
- Ruby小例子
1.ruby定义函数与执行函数案例 def fact(n) ) end end print fact() 结果: 24 2.一个小例子 words = [)] print "guess?\n ...
- js LocalStorage
此对象主要有两个方法:保存数据:localStorage.setItem(Key, value);读取数据:localStorage.getItem(Key);Key:表示你要存入的键名称,此名称可以 ...
- java 科学计数法表示转换
BigDecimal strScien = new BigDecimal("9.67953970412123E-05"); System.out.println(strScien. ...
- python Debug 单步调试
一直犯愁的是python的调试,曾经写c都是编译完了用gdb直接调试了,轻松愉快.如今遇到这么一个解释型的程序.不知道怎么办了.用log吧,有时就是一个小程序,不想写这么多代码.打屏吧.有时屏幕翻得快 ...
- 依赖注入及AOP简述(四)——“好莱坞原则”和依赖注入框架简介 .
3.2. “好莱坞原则” 看了前面关于依赖注入概念的描述,我们来提炼出依赖注入的核心思想.如果说传统的组件间耦合方式,例如new.工厂模式等,是一种由开发者主动去构建依赖对象的话,那么依赖注入模 ...
- 实用的JavaScript技巧、窍门和最佳实践
JavaScript是世界上第一的编程语言,它是Web的语言,是移动混合应用(mobile hybrid apps)的语言(比如 PhoneGap或者 Appcelerator),是服务器端的语言(比 ...