用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() 获 ...
随机推荐
- Python爬虫实战(4):豆瓣小组话题数据采集—动态网页
1, 引言 注释:上一篇<Python爬虫实战(3):安居客房产经纪人信息采集>,访问的网页是静态网页,有朋友模仿那个实战来采集动态加载豆瓣小组的网页,结果不成功.本篇是针对动态网页的数据 ...
- Net Core WebApi单元测试
单元测试 本篇将结合这个系列的例子的基础上演示在Asp.Net Core里如何使用XUnit结合Moq进行单元测试,同时对整个项目进行集成测试. 第一部分.XUnit 修改 Project.json ...
- python bool值要注意的一些地方
1.像(),[],{}这三个是可以通过bool(()),bool([]),bool({})转化为bool值的:且它们转化后的结果为False.但是这三个值它本身并不等于False.切记不可以与Fals ...
- 厂商自定义USB设备固件程序及特性
通过前面的学习,大家应该对USB固件程序结构有了比较深的认识,现在再来详细说说固件里决定设备识别成厂商自定义USB设备的地方有哪些,或者说厂商自定义USB设备的固件特性有哪些. 之前不止一次说过学习U ...
- Chapter 1. Hello, Perl/Tk
Chapter 1. Hello, Perl/Tk 内容: Perl/Tk Concepts Some Perl/Tk History Getting Started with Perl/Tk Hel ...
- Python中的图形库
Python中的图形库 根据Python 2.x的官网文档的解释: Graphical User Interfaces with Tk 和 Other Graphical User Interface ...
- 杭电oj 2719
Tips:本程序没有什么难度,只要按照逻辑进行替换即可,需要注意的是,由于输入串中含有空格符号,所以不能使用scanf("%s",ch);来读取一串,可以使用gets()函数读取一 ...
- Java List 汉字进行排序
Comparator<Person> cmp = new Comparator<Person>() { public int compare(Person o1, Person ...
- OC基础9:预处理程序
"OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1. 关于#define语句: (1). ...
- windows7下virtualBox配置识别usb
在windows7下安装virtualBox后.在虚拟机里面是不能识别手机的,此时我们须要做一些配置. 一. virtualBox菜单: 管理–全局设定–扩展–加入包(右側,virtualBox ex ...