用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() 获 ...
随机推荐
- Leetcode 242 Valid Anagram pytyhon
题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example,s ...
- Leetcode 226 Invert Binary Tree python
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
- Mongo客户端
http://www.linuxidc.com/Linux/2012-07/64233.htm http://www.oschina.net/p/rockmongo http://www.cnblog ...
- 【the service mysql57 failed the most】
异常信息: the the service mysql57 failed the most recent status change request with the messagethe servi ...
- ISSkin 使用技巧,WinXP 下的窗口阴影
原文 http://restools.hanzify.org/article.asp?id=109 是否觉得在使用 ISSkin 的时候感觉窗口太过平板,尤其对于那些窗口边缘和窗口内部颜色一致的皮肤尤 ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- 【Java之】多线程学习笔记
最近在学习thinking in java(第三版),本文是多线程这一章的学习总结. --------------------------------------------------------- ...
- Scrapy URLError
错误信息如下: 2015-12-03 16:05:08 [scrapy] INFO: Scrapy 1.0.3 started (bot: LabelCrawler) 2015-12-03 16:05 ...
- Android_Layout_xml布局
本博文为子墨原创,转载请注明出处! http://blog.csdn.net/zimo2013/article/details/11840079 1.构建xml布局文件 使用android提供的xml ...
- ORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instance
1.启动报错SQL> startupORA-32004: obsolete or deprecated parameter(s) specified for RDBMS instanceORAC ...