博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenGL(三)MFC中应用OpenGL的两个类
阅读量:5092 次
发布时间:2019-06-13

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

  • 网上找了下MFC中应用OpenGL的方法,总觉得不太方便,到处复制一堆堆函数,于是封装了两个类(CmyOpenGL和CRenderContex)。方便应用。
  • 以MFC Dialog为例添加步骤:
    • CXXXDlg中添加一CmyOpenGL成员(或指针,别忘记释放就行)
    • 将三个成员函数对应加入(OnCreate、OnPaint、OnSize)MFC的消息映射函数中,传递相应参数(CmyOpenGL要利用CDC*所指对象,因此CDC*要在CmyOpenGL对象之后释放!
  • 代码:
    • CmyOpenGL.h
      1 #pragma once  2 #include "RenderContex.h"  3  4 class CmyOpenGL  5 {
      6 public: 7 CmyOpenGL(void); 8 virtual ~CmyOpenGL(void); 9 public: 10 virtual void OnCreate(CDC* pDC); 11 virtual void OnPaint(void); 12 virtual void OnSize(int cx, int cy); 13 private: 14 virtual void RenderScene (void); 15 private: 16 CDC* m_pDC; 17 CRenderContex* m_pRC; 18 };
    • CmyOpenGL.cpp
      1 #include "StdAfx.h"  2 #include "myOpenGL.h"  3  4 CmyOpenGL::CmyOpenGL(void)  5 {
      6 m_pDC = NULL; 7 m_pRC = NULL; 8 } 9 10 CmyOpenGL::~CmyOpenGL(void) 11 {
      12 if (m_pRC) 13 {
      14 delete m_pRC; 15 } 16 m_pRC = NULL; 17 } 18 19 void CmyOpenGL::OnCreate(CDC* pDC) 20 {
      21 ASSERT(pDC); 22 m_pRC = new CRenderContex(pDC); 23 ASSERT(m_pRC); 24 25 m_pDC = pDC; 26 27 //Specify Black as the clear color 28 ::glClearColor(0.0f,0.0f,0.0f,0.0f); 29 } 30 31 void CmyOpenGL::OnPaint() 32 {
      33 // Clear out the color & depth buffers 34 ::glClear( GL_COLOR_BUFFER_BIT); 35 RenderScene(); 36 // Tell OpenGL to flush its pipeline 37 ::glFinish(); 38 // Now Swap the buffers 39 ::SwapBuffers( m_pDC->GetSafeHdc() ); 40 } 41 42 void CmyOpenGL::OnSize(int cx, int cy) 43 {
      44 if ( 0 >= cx || 0 >= cy ) 45 {
      46 return; 47 } 48 // select the full client area 49 ::glViewport(0, 0, cx, cy); 50 // select the projection matrix and clear it 51 ::glMatrixMode(GL_PROJECTION); 52 ::glLoadIdentity(); 53 // select the viewing volume 54 ::gluPerspective(45.0f, (GLdouble)cx/(GLdouble)cy, 1.0f, 100.0f); 55 // switch back to the modelview matrix and clear it 56 ::glMatrixMode(GL_MODELVIEW); 57 ::glLoadIdentity(); 58 } 59 60 void CmyOpenGL::RenderScene (void) 61 {
      62 glTranslatef(0.0f, 0.0f, -5.0f); 63 glRotatef(45.0,0.0,1.0,0.0); 64 glutWireCube(1.0); 65 }
    • CRenderContex.h
      1 #pragma once  2  3 class CRenderContex  4 {
      5 public: 6 CRenderContex(CDC *pDC); 7 ~CRenderContex(void); 8 private: 9 BOOL SetupPixelFormat(CDC *pDC); //Set up the Pixel Format 10 private: 11 HGLRC m_hRC; 12 };
    • CRenderContex.cpp
      1 #include "StdAfx.h"  2 #include "RenderContex.h"  3  4 CRenderContex::CRenderContex(CDC *pDC)  5 {
      6 ASSERT(pDC); 7 8 BOOL bResult = FALSE; 9 bResult = SetupPixelFormat(pDC); 10 ASSERT(bResult); 11 12 //Create Rendering Context 13 m_hRC = ::wglCreateContext (pDC->GetSafeHdc ()); 14 ASSERT(m_hRC); 15 //Make the RC Current 16 bResult = ::wglMakeCurrent (pDC->GetSafeHdc (), m_hRC); 17 ASSERT(bResult); 18 } 19 20 CRenderContex::~CRenderContex(void) 21 {
      22 BOOL bResult = FALSE; 23 //Make the RC non-current 24 bResult = ::wglMakeCurrent (0,0); 25 ASSERT(bResult); 26 27 //Delete the rendering context 28 bResult = ::wglDeleteContext (m_hRC); 29 ASSERT(bResult); 30 } 31 32 //Setup Pixel Format 33 BOOL CRenderContex::SetupPixelFormat(CDC *pDC) 34 {
      35 static PIXELFORMATDESCRIPTOR pfd = 36 {
      37 sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd 38 1, // version number 39 PFD_DRAW_TO_WINDOW | // support window 40 PFD_SUPPORT_OPENGL | // support OpenGL 41 PFD_DOUBLEBUFFER, // double buffered 42 PFD_TYPE_RGBA, // RGBA type 43 24, // 24-bit color depth 44 0, 0, 0, 0, 0, 0, // color bits ignored 45 0, // no alpha buffer 46 0, // shift bit ignored 47 0, // no accumulation buffer 48 0, 0, 0, 0, // accum bits ignored 49 16, // 16-bit z-buffer 50 0, // no stencil buffer 51 0, // no auxiliary buffer 52 PFD_MAIN_PLANE, // main layer 53 0, // reserved 54 0, 0, 0 // layer masks ignored 55 }; 56 int m_nPixelFormat = ::ChoosePixelFormat(pDC->GetSafeHdc(), &pfd); 57 if ( m_nPixelFormat == 0 ) 58 {
      59 return FALSE; 60 } 61 if ( ::SetPixelFormat(pDC->GetSafeHdc(), m_nPixelFormat, &pfd) == FALSE) 62 {
      63 return FALSE; 64 } 65 return TRUE; 66 }
  • 参考:

转载于:https://www.cnblogs.com/dahai/archive/2011/12/30/2307800.html

你可能感兴趣的文章
数据结构 : Hash Table [II]
查看>>
面向对象的小demo
查看>>
获取地址栏参数
查看>>
java之hibernate之helloworld
查看>>
微服务之初了解(一)
查看>>
Iterator invalidation(迭代器失效)
查看>>
GDOI DAY1游记
查看>>
网络流24题(更新中
查看>>
python字典
查看>>
CouchDB 1.3.0的新特性以及算法的强化
查看>>
收集WebDriver的执行命令和参数信息
查看>>
VS2010版快捷键
查看>>
如何在Windows 10中启用关闭事件跟踪程序
查看>>
SSH(Struts2+Spring+Hibernate)框架搭建流程
查看>>
数据结构与算法(三)-线性表之静态链表
查看>>
mac下的mysql报错:ERROR 1045(28000)和ERROR 2002 (HY000)的解决办法
查看>>
Hmailserver搭建邮件服务器
查看>>
django之多表查询-2
查看>>
BULK INSERT, 实战手记:让百万级数据瞬间导入SQL Server
查看>>
快速幂
查看>>