2022å¹´ã«ãããã¨ã§ã¯ãªãã¨ã¯æãã¾ãããä»äºã®åå¼·ãã¦ãMFCã使ã£ãã°ã©ãã£ã«ã«ãªã¢ããªã±ã¼ã·ã§ã³ãä½ããããªãã¾ãã. ããã§OpenGLã使ç¨ããMFCã¢ããªã±ã¼ã·ã§ã³ãä½ãã¾ã.
ãªããMFCã¯æ¸ç±çãä¸å¤ã§ããè¦å½ãããªãã»ã©éå»ã®ãã®ã§ãããç¹å¥ãªäºæ ããªãéã使ç¨ããçç±ã¯ãªãã§ãï¼éã«è¨ãã°ãä»åã¯ä»äºã§ä½¿ãããã ã¨ããçç±ããMFCãå©ç¨ãã¦ãã¾ãï¼.
ãã®è¨äºã¯ãã®1ã¨ãã¦ãç°å¢æ§ç¯ããç©å½¢ã表示ããã¨ããã¾ã§ãã«ãã¼ãã¦ãã¾ã.
2022/8/16 ã©ã¤ãã©ãªã«é¢ããè¨è¿°ãä¸é¨ä¿®æ£
2022/10/6 å 容ãä¿®æ£
ç°å¢
é ç® | ãã¼ã¸ã§ã³ |
---|---|
OS | Windows11 |
Visual Studio | 2017 Community Edition (ãã¼ã«ã»ããv141) |
Windows SDK | 10.0.17763.0 |
Glew_static | 2.1.0.3 |
glfw | 3.38 |
ããã¸ã§ã¯ãã®ä½æ
ã¾ãã¯æ°ããMFCããã¸ã§ã¯ããä½æãã¾ã.ãã¡ã¤ã«->æ°è¦ä½æ->ããã¸ã§ã¯ãããVisual C++->MFC/ATL->MFCã¢ããªãé¸æãã好ããªããã¸ã§ã¯ãåãä½æãã¾ã. ä»åã¯OpenGLã¨ããååã«ãã¾ãã. MFCã¢ããªä½æç¨ã¦ã£ã¶ã¼ãã表示ãããã®ã§ã次ã®ç»åã®ããã«ããã¸ã§ã¯ããä½æãã¾ã.
ä¸è¨ã®ç»åã®ããã«ã¦ã£ã¶ã¼ããé²ãããå®äºãæ¼ãã¦ããã¸ã§ã¯ãã®ä½æã¯å®äºã§ã.ä½æãããã°ããã®ããã¸ã§ã¯ãããã«ãããã¨ã次ã®ãããªãã¬ã¼ã ã表示ããã¾ã.
ã¨ããããã³ã³ããã¼ã«ã«OpenGLã§æç»
ãã¦ãããããå®éã«MFCã®ã³ã³ããã¼ã«ã«OpenGLãç¨ãã¦æç»ãã¦ããã¾ã. 次ã®è¨äºãåèã«ããã¦ããã ãã¾ãã.
ãã£ããã¨ããæµãã¯æ¬¡ã«ãªãã¾ã
- ãã¯ãã£ã¼ã³ã³ããã¼ã«ããã¤ã¢ãã°ã«è¿½å ãã
- CStaticã¯ã©ã¹ãç¶æ¿ããCGLScreenã¯ã©ã¹ãä½ã
- ãã¤ã¢ãã°ã®ã½ã¼ã¹ã«CGLScreenåã®DDXå¤æ°ã追å
- CGLScreenã®ããã¤ã¹ã³ã³ããã¹ããåå¾ããOpenGLã§ç©å½¢ãæç»ãã
ãã¯ãã£ã¼ã³ã³ããã¼ã«ã®è¿½å
ãªã½ã¼ã¹ãã¥ã¼ ->OpenGL->OpenGL.rc ->Dialog->IDD_OPENGL_DIALOGãé¸æãããªã½ã¼ã¹ã¨ãã£ã¿ãéãã¾ã. ãªã½ã¼ã¹ã¨ãã£ã¿ã表示ä¸ã«ãã¼ã«ããã¯ã¹ãã¯ãªãã¯ããã¨ãæ§ã ãªã³ã³ããã¼ã«ã表示ããã¦ãã¾ã. æ¤ç´¢çªã§Picture Controlã¨æ¤ç´¢ããPicture Controlããã©ãã°&ãããããã¦ãã¤ã¢ãã°ã«è¿½å ãã¾ããã. éªéãªã®ã§ãä¸å¿ã«ããã©ãã«ã¯åé¤ãã¦ããã¾ããã.
Picture Control追å ç´å¾ã¯ãã³ã³ããã¼ã«ã®IDãID_STATICã«ãªã£ã¦ãããã¨æãã¾ã. ä»åã¯IDC_GLã¨ããååã«å¤æ´ãã¾ã.
CGLScreenã¯ã©ã¹ã®å®ç¾©
次ã«Picture Controlç¨ã®ç¬èªã¯ã©ã¹ãå®è£ ãããããã½ãªã¥ã¼ã·ã§ã³ã¨ã¯ã¹ããã¼ã©ã¼->å³ã¯ãªãã¯->追å ->ã¯ã©ã¹ ããã¯ã©ã¹ã®ã½ã¼ã¹ãã¡ã¤ã«ã¨ãããã¼ãã¡ã¤ã«ãåæã«çæãã¾ã.
CGLScreen.h
çæãããCGLScreen.hã以ä¸ã®ããã«ãã¾ã.
// CGLScreen.h #pragma once #include <afxwin.h> class CGLScreen : public CStatic { protected: HGLRC m_hRC; CDC* m_pDC; BOOL SetupPixelFormat(); BOOL InitGLContext(); public: virtual void PreSubclassWindow(); DECLARE_MESSAGE_MAP() virtual afx_msg void OnPaint(); virtual afx_msg void OnDestroy(); };
CGLScreen.cpp
çæãããCGLScreen.cppã«ä¸ã®ãããªã³ã¼ãã追è¨ãã¾ã.
// CGLScreen.cpp #include "CGLScreen.h" #include <GL/GL.h> #include <GL/GLU.h> BOOL CGLScreen::SetupPixelFormat() { PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 }; int pixelformat; if (0 == (pixelformat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd))) { return FALSE; } if (FALSE == ::SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd)) { return FALSE; } return TRUE; } BOOL CGLScreen::InitGLContext() { m_pDC = new CClientDC(this); if (NULL == m_pDC) { return FALSE; } if (!SetupPixelFormat()) return FALSE; if (0 == (m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc()))) { return FALSE; } if (FALSE == ::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC)) { return FALSE; } return TRUE; } void CGLScreen::PreSubclassWindow() { LONG style = GetWindowLong(this->m_hWnd, GWL_STYLE); style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN; SetWindowLong(this->m_hWnd, GWL_STYLE, style); InitGLContext(); CStatic::PreSubclassWindow(); } BEGIN_MESSAGE_MAP(CGLScreen, CStatic) ON_WM_PAINT() ON_WM_DESTROY() END_MESSAGE_MAP() void CGLScreen::OnPaint(){ CPaintDC dc(this); // device context for painting ::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //ã¨ãããããåè§å½¢ãæç»ãã¦ã¿ãã ::glPushMatrix(); ::glColor3f(1.0f, 1.0f, 0.0f); glColor3d(1.0, 0.0, 0.0); glBegin(GL_POLYGON); glVertex2d(-0.9, -0.9); glVertex2d(0.9, -0.9); glVertex2d(0.9, 0.9); glVertex2d(-0.9, 0.9); glEnd(); ::glPopMatrix(); ::glFinish(); if (FALSE == ::SwapBuffers(m_pDC->GetSafeHdc())) {} } void CGLScreen::OnDestroy() { CStatic::OnDestroy(); // TODO: ããã«ã¡ãã»ã¼ã¸ ãã³ãã© ã³ã¼ãã追å ãã¾ãã if (FALSE == ::wglMakeCurrent(NULL, NULL)) { // å¿ è¦ã«å¿ãã¦ã¨ã©ã¼ãã³ããªã³ã° } if (FALSE == ::wglDeleteContext(m_hRC)) { // å¿ è¦ã«å¿ãã¦ã¨ã©ã¼ãã³ããªã³ã° } if (m_pDC) delete m_pDC; }
ããã§OnPaint()ã¨OnDestroy()ã®è§£èª¬ãã°. MFCã¢ããªã±ã¼ã·ã§ã³ã¯ã¡ãã»ã¼ã¸ã«ããã¤ãã³ãé§åãåºæ¬ã«åä½ãã¾ã. ã¤ã¾ããã¤ãã³ããã³ãã©ãæ¸ã足ãã¦ã¢ããªã±ã¼ã·ã§ã³ãä½ãã®ã§ããããã®éã«ã©ã®ã¡ãã»ã¼ã¸ãæå¹ã«ãããã¨ãããã¨ãæ示ããªããã°ãªãã¾ãã. ã¡ãã»ã¼ã¸ãæå¹ã«ããå ´åãBEGIN_MESSAGE_MAP()~END_MESSAGE_MAP()ã§å²ã¾ããé¨åã«æå¹ã«ãããã¡ãã»ã¼ã¸ã¨ãã®ã¡ãã»ã¼ã¸ã«å¯¾ãããã³ãã©ãç»é²ãã¾ã. JavaScriptã§ããã¨ããã®addEventListenerã¿ãããªãã®ã§ãã. ä»åã®å ´åã¯WM_PAINTã¨WM_DESTROYã¨ããã¡ãã»ã¼ã¸ã«å¯¾ããã¤ãã³ããã³ãã©ãç»é²ãã¦ãã¦ããããã®ã¡ãã»ã¼ã¸ãåä¿¡ããã¨ãã«ãã³ãã©ãèªåçã«å®è¡ãããããã«ãã¦ãã¾ã.
PreSubclassWindow()ã¯èªåçã«å¼ã³åºãããã¨ããç¹ã§ã¯ã¤ãã³ããã³ãã©ã«ä¼¼ã¦ãã¾ããããã¬ã¼ã ã¯ã¼ã¯ã«ãã£ã¦å¼ã³åºãããé¢æ°ã§ã. ãªã®ã§ã対å¿ããã¡ãã»ã¼ã¸ãªã©ã¯åå¨ãããã¡ãã»ã¼ã¸ãããã«ç»é²ããå¿ è¦ã¯ããã¾ãã.
DDXå¤æ°ã®ãã¤ã¢ãã°ã¸ã®è¿½å
Picture Controlã¨å ã»ã©å®ç¾©ããã¯ã©ã¹ãç´ã¥ãããããDDXå¤æ°ã追å ãã¾ã. ããæ£ç¢ºã«è¨ãã°ãIDC_GLã¨ãã¤ã³ããããCGLScreenåã®å¤æ°m_GLScreenããOpenGLDlgã¯ã©ã¹ã«è¿½å ãã¾ã.
Picture Control ã追å ããã¨ãã®ããã«ãªã½ã¼ã¹ã¨ãã£ã¿ãéããDDXå¤æ°ã追å ãããã³ã³ããã¼ã«ãå³ã¯ãªãã¯->å¤æ°ã®è¿½å ããã¦ã£ã¶ã¼ããéãã¾ã.
ã³ã³ããã¼ã«å¤æ°ã®è¿½å ã¦ã£ã¶ã¼ãã®ã³ã³ããã¼ã«ã¿ãã次ã®ããã«ãã¾ã. ãªãããã®ä»ã¿ãã¯ããã©ã«ãã®ã¾ã¾ã§å¤§ä¸å¤«ã§ã.
ã¦ã£ã¶ã¼ããå®äºããå¾ã«OpenGLDlg.hãè¦ãã¨ãCGLScreenåã®å¤æ°m_GLScreenã追å ããã¦ãããã¨ãåããã¾ã. ãã ããCGLScreen.hãã¤ã³ã¯ã«ã¼ããã¦ããªãã®ã§ã赤ãç ´ç·ã表示ããã¦ãã¾ã.
ãªã®ã§ãOpenGLDlg.hå ã§CGLScreen.hãã¤ã³ã¯ã«ã¼ããã¾ããã.
OpenGLDlg.h
// OpenGLDlg.h // m_GLScreenã追å ããã #pragma once #include "CGLScreen.h" class COpenGLDlg : public CDialogEx { public: COpenGLDlg(CWnd* pParent = nullptr); // ãã¤ã¢ãã° ãã¼ã¿ #ifdef AFX_DESIGN_TIME enum { IDD = IDD_OPENGL_DIALOG }; #endif protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV ãµãã¼ã // å®è£ protected: HICON m_hIcon; // çæããããã¡ãã»ã¼ã¸å²ãå½ã¦é¢æ° virtual BOOL OnInitDialog(); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); DECLARE_MESSAGE_MAP() public: CGLScreen m_GLScreen; };
以ä¸ã§ãã¡ã¤ã«ã®ç·¨éã¯å®äºã§ã.
ãããã«ã
以ä¸ã®ããã«ç¡äºMFCã®ã³ã³ããã¼ã«ã«OpenGLã使ã£ã¦æç»ãããã¨ãåºæ¥ã¾ãã.
ã¾ã¨ã
Visual Studio 2017ã§ãMFCã¨OpenGLãé£æºãã¦ä½¿ç¨ãããã¨ãåºæ¥ã¾ãã. VC++ã§ã¤ãã³ãé§åã¢ããªã±ã¼ã·ã§ã³ãä½ãã®ã¯æµæãããã¾ãããããã£ã¦ã¿ãã¨ã»ãã®è¨èªã¨ããã¾ã§å¤ãããªãã¦é©ãã¾ãã.
次åã¯é ç¹ãããã¡ãªãã¸ã§ã¯ãã¨é ç¹é åãªãã¸ã§ã¯ãã使ã£ãç©å½¢ã®æç»ããã¦ããã¾ã.