• mfc 下的OpenGL


    建立一个SDI 的MFC工程,然后按freeglut 在mfc 下的编译_leon_zeng0的博客-CSDN博客​​​​​​

    一文设置好include lib 路径 

    在view 中建立这2个函数:

    1. // Standard OpenGL Init Stuff
    2. BOOL CmfcOpenglDemoView::SetupPixelFormat()
    3. {
    4. static PIXELFORMATDESCRIPTOR pfd =
    5. {
    6. sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
    7. 1, // version number
    8. PFD_DRAW_TO_WINDOW | // support window
    9. PFD_SUPPORT_OPENGL | // support OpenGL
    10. PFD_DOUBLEBUFFER, // double buffered
    11. PFD_TYPE_RGBA, // RGBA type
    12. 24, // 24-bit color depth
    13. 0, 0, 0, 0, 0, 0, // color bits ignored
    14. 0, // no alpha buffer
    15. 0, // shift bit ignored
    16. 0, // no accumulation buffer
    17. 0, 0, 0, 0, // accumulation bits ignored
    18. 16, // 16-bit z-buffer
    19. 0, // no stencil buffer
    20. 0, // no auxiliary buffer
    21. PFD_MAIN_PLANE, // main layer
    22. 0, // reserved
    23. 0, 0, 0 // layer masks ignored
    24. };
    25. int m_nPixelFormat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);
    26. if (m_nPixelFormat == 0)
    27. return FALSE;
    28. return ::SetPixelFormat(m_pDC->GetSafeHdc(), m_nPixelFormat, &pfd);
    29. }
    30. BOOL CmfcOpenglDemoView::InitOpenGL()
    31. {
    32. TRACE(L"InitOpenGL()\n");
    33. //Get a DC for the Client Area
    34. m_pDC = new CClientDC(this);
    35. //Failure to Get DC
    36. if (m_pDC == NULL)
    37. return FALSE;
    38. if (!SetupPixelFormat())
    39. return FALSE;
    40. //Create Rendering Context
    41. m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
    42. //Failure to Create Rendering Context
    43. if (m_hRC == 0)
    44. return FALSE;
    45. //Make the RC Current
    46. if (::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC) == FALSE)
    47. return FALSE;
    48. // Usual OpenGL stuff
    49. glClearColor(0.0f, 0.0f, 0.3f, 0.0f);
    50. glClearDepth(1.0f);
    51. glEnable(GL_DEPTH_TEST);
    52. //glDisable( GL_DEPTH_TEST );
    53. //glEnable( GL_TEXTURE_2D );
    54. glEnable(GL_BLEND);
    55. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    56. // glDepthMask(GL_FALSE);
    57. glLineWidth(1.0f);
    58. glPointSize(1.0f);
    59. // for test
    60. m_RenderScene = render;
    61. return TRUE;
    62. }
    63. void CmfcOpenglDemoView::RenderScene()
    64. {
    65. if (m_RenderScene != NULL)
    66. m_RenderScene((CmfcOpenglDemoDoc*)(GetDocument()));
    67. else
    68. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    69. }

    利用class wizard 建立几个message:

    OnCreate(LPCREATESTRUCT lpCreateStruct)

    OnDestroy()

    OnSize(UINT nType, int cx, int cy)

    OnEraseBkgnd(CDC* pDC)

    1. int CmfcOpenglDemoView::OnCreate(LPCREATESTRUCT lpCreateStruct)
    2. {
    3. if (CView::OnCreate(lpCreateStruct) == -1)
    4. return -1;
    5. // TODO: Add your specialized creation code here
    6. if (!InitOpenGL())
    7. {
    8. MessageBox(L"Error setting up OpenGL!", L"Init Error!",
    9. MB_OK | MB_ICONERROR);
    10. return -1;
    11. }
    12. COLORREF color = ::GetSysColor(COLOR_3DFACE);
    13. glClearColor(0.0, 0, 0.25, 1.0);
    14. glPolygonMode(GL_FRONT, GL_FILL);
    15. glPolygonMode(GL_BACK, GL_FILL);
    16. glEnable(GL_DEPTH_TEST);
    17. // Antialiasing
    18. glEnable(GL_LINE_SMOOTH);
    19. glEnable(GL_BLEND);
    20. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    21. glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    22. glLineWidth(1.0);
    23. return 0;
    24. }
    25. void CmfcOpenglDemoView::OnDestroy()
    26. {
    27. CView::OnDestroy();
    28. // TODO: Add your message handler code here
    29. wglMakeCurrent(0, 0);
    30. wglDeleteContext(m_hRC);
    31. if (m_pDC)
    32. {
    33. delete m_pDC;
    34. }
    35. m_pDC = NULL;
    36. }
    37. void CmfcOpenglDemoView::OnSize(UINT nType, int cx, int cy)
    38. {
    39. CView::OnSize(nType, cx, cy);
    40. // TODO: Add your message handler code here
    41. Resize(cx,cy);
    42. RenderScene();// OnPaint();
    43. }
    44. BOOL CmfcOpenglDemoView::OnEraseBkgnd(CDC* pDC)
    45. {
    46. // TODO: Add your message handler code here and/or call default
    47. TRACE("COpem:OnEraseBkgnd--\n");
    48. return CView::OnEraseBkgnd(pDC);
    49. }
    50. void CmfcOpenglDemoView::Resize(int cx,int cy)
    51. {
    52. // TODO: Add your implementation code here.
    53. GLsizei width, height;
    54. GLdouble aspect;
    55. width = cx;
    56. height = cy;
    57. if (cy == 0)
    58. aspect = (GLdouble)width;
    59. else
    60. aspect = (GLdouble)width / (GLdouble)height;
    61. glViewport(0, 0, width, height);
    62. glMatrixMode(GL_PROJECTION);
    63. glLoadIdentity();
    64. gluOrtho2D(-1.0f,1.0,-1.0f,1.0f);
    65. //glOrtho(-4.0 * aspect - pDoc->m_transX, 4.0 * aspect - pDoc->m_transX, -4 - pDoc->m_transY, 4 - pDoc->m_transY, -10, 10);//aspect=cx/cy
    66. glMatrixMode(GL_MODELVIEW);
    67. //glMatrixMode( GL_PROJECTION );
    68. }

    Resize 可以参考下面代码,设置立体图,上面代码只是正面的平面图。

    1. width = m_sizeCx;
    2. height = m_sizeCy;
    3. if(m_sizeCy==0)
    4. aspect = (GLdouble)width;
    5. else
    6. aspect = (GLdouble)width/(GLdouble)height;
    7. glViewport(0,0,width,height);
    8. glMatrixMode(GL_PROJECTION);
    9. glLoadIdentity();
    10. if(pDoc->m_ViewSelect==0)
    11. gluPerspective(45,aspect,1,100.0);
    12. else
    13. //gluOrtho2D(0.0f,aspect,0.0f,1.0f);
    14. glOrtho(-4.0*aspect-pDoc->m_transX, 4.0*aspect-pDoc->m_transX, -4-pDoc->m_transY,4-pDoc->m_transY, -10, 10);//aspect=cx/cy
    15. glMatrixMode(GL_MODELVIEW);

    glOtho2D 参看

    https://registry.khronos.org/OpenGL-Refpages/gl2.1/xhtml/glOrtho.xml

    C Specification

    void glOrtho(GLdouble left,
    GLdouble right,
    GLdouble bottom,
    GLdouble top,
    GLdouble nearVal,
    GLdouble farVal);

    Parameters

    leftright

    Specify the coordinates for the left and right vertical clipping planes.

    bottomtop

    Specify the coordinates for the bottom and top horizontal clipping planes.

    nearValfarVal

    Specify the distances to the nearer and farther depth clipping planes. These values are negative if the plane is to be behind the viewer.

    Description

    glOrtho describes a transformation that produces a parallel projection. The current matrix (see glMatrixMode) is multiplied by this matrix and the result replaces the current matrix, as if glMultMatrix were called with the following matrix as its argument:

    2right-left00��02top-bottom0��00-2farVal-nearVal��0001

    where

    ��=-right+leftright-left

    ��=-top+bottomtop-bottom

    ��=-farVal+nearValfarVal-nearVal

    Typically, the matrix mode is GL_PROJECTION, and leftbottom-nearVal and righttop-nearVal specify the points on the near clipping plane that are mapped to the lower left and upper right corners of the window, respectively, assuming that the eye is located at (0, 0, 0). -farVal specifies the location of the far clipping plane. Both nearVal and farVal can be either positive or negative.

    Use glPushMatrix and glPopMatrix to save and restore the current matrix stack.

    Errors

    GL_INVALID_VALUE is generated if left = right, or bottom = top, or near = far.

    GL_INVALID_OPERATION is generated if glOrtho is executed between the execution of glBegin and the corresponding execution of glEnd.

     

    在.h 文件中加这几个函数 

    1. void SetRenderFunc(void (*func) (CmfcOpenglDemoDoc*)) { m_RenderScene = func; }
    2. void RenderScene();
    3. // Each viewport uses its own context
    4. // so we need to make sure the correct
    5. // context is set whenever we make an
    6. // OpenGL command.
    7. void SetContext() { wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC); }
    8. void SwapGLBuffers() { SwapBuffers(m_pDC->GetSafeHdc()); }

    添加一个画图函数,不是类里的,独立的

    1. void render(CmfcOpenglDemoDoc* pdoc)
    2. {
    3. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    4. glBegin(GL_TRIANGLE_STRIP);
    5. glColor3f(0.0f, 0.5f, 0.0f);
    6. glVertex2f(0.0f, -0.0f);
    7. glVertex2f(0.0f, 0.3f);
    8. glVertex2f(0.4f, 0.0f);
    9. glEnd();
    10. }

     在init里有这一句:

    m_RenderScene = render;

    在.h 文件中有:

    1. // Attributes
    2. public:
    3. CmfcOpenglDemoDoc* GetDocument() const;
    4. //------------------
    5. void (*m_RenderScene) (CmfcOpenglDemoDoc* doc); // void function pointer to the rendering
    6. // function. Used to change to easily
    7. // change what a viewport displays.
    8. protected:
    9. HGLRC m_hRC; //Rendering Context
    10. CDC* m_pDC; //Device Context
    11. // Operations
    12. public:
    13. // OpenGL init stuff
    14. BOOL SetupPixelFormat();
    15. BOOL InitOpenGL();

     这样就构造了一个基本的mfc 下的opengl 工程

  • 相关阅读:
    设计模式-02-工厂模式
    java基于springboot+Vue社区居民医疗健康网站
    22/6/26
    Opencv——can‘t open/read file: check file path/integrity的解决办法
    ARM开发流程LDS相关解惑
    C Primer Plus(6) 中文版 第7章 C控制语句:分支和跳转 7.5 条件运算符 ?:
    【历史上的今天】8 月 15 日:苹果推出初代 iMac;谷歌收购摩托罗拉移动;Fuchsia 首次发布
    Spring Boot集成Swagger接口分类与各元素排序问题
    数据分析:数据分析篇
    C语言——结构体详解
  • 原文地址:https://blog.csdn.net/leon_zeng0/article/details/134215436