日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

《MFC dialog中加入OpenGL窗体》

發(fā)布時(shí)間:2024/4/13 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《MFC dialog中加入OpenGL窗体》 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

《MFC dialog中加入OpenGL窗體》

? ? 最近學(xué)習(xí)了如何在MFC對(duì)話框程序中加入OpenGL窗體的方法,在這里將自己的實(shí)現(xiàn)過程歸納一下。

?

步驟零: 加入PictureControl控件

? ? 新建MFC對(duì)話框程序,刪除對(duì)話框上的按鈕控件的Label控件,然后向窗體添加PictureControl控件,作為繪制的窗體。

? ? 將該控件的ID設(shè)置為:IDC_RENDER

?

步驟一: 加入OpenGL的lib文件和頭文件

? ? 在項(xiàng)目上單擊右鍵,添加OpenGL的lib文件,freeglut_static.lib和gltools.lib,如下。

?

? ? 然后在stdafx.h中包含相關(guān)的頭文件如下:

步驟二: 設(shè)置對(duì)話框的頭文件***Dlg.h

? ? 在對(duì)話框頭文件中聲明相關(guān)的變量:

1 HDC hrenderDC; //設(shè)備上下文 2 HGLRC hrenderRC; //渲染上下文 3 float m_yRotate; //轉(zhuǎn)速 4 int PixelFormat; //像素格式

? ? 在對(duì)話框頭文件中聲明相關(guān)方法:

1 BOOL SetWindowPixelFormat(HDC hDC); //設(shè)定像素格式 2 BOOL CreateViewGLContext(HDC hDC); //view GL Context 3 void RenderScene(); //繪制場景

? ??加入消息映射函數(shù):

?1 afx_msg void OnTimer(UINT nIDEvent);?

? ? 具體的對(duì)話框頭文件如下:

1 // OpenGLTest1Dlg.h : 頭文件 2 // 3 4 #pragma once 5 6 7 // COpenGLTest1Dlg 對(duì)話框 8 class COpenGLTest1Dlg : public CDialogEx 9 { 10 // 構(gòu)造 11 public: 12 COpenGLTest1Dlg(CWnd* pParent = NULL); // 標(biāo)準(zhǔn)構(gòu)造函數(shù) 13 14 BOOL SetWindowPixelFormat(HDC hDC); //設(shè)定像素格式 15 BOOL CreateViewGLContext(HDC hDC); //view GL Context 16 void RenderScene(); //繪制場景 17 18 HDC hrenderDC; //設(shè)備上下文 19 HGLRC hrenderRC; //渲染上下文 20 float m_yRotate; //轉(zhuǎn)速 21 int PixelFormat; //像素格式 22 23 // 對(duì)話框數(shù)據(jù) 24 enum { IDD = IDD_OPENGLTEST1_DIALOG }; 25 26 protected: 27 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 28 29 30 // 實(shí)現(xiàn) 31 protected: 32 HICON m_hIcon; 33 34 35 // 生成的消息映射函數(shù) 36 virtual BOOL OnInitDialog(); 37 afx_msg void OnSysCommand(UINT nID, LPARAM lParam); 38 afx_msg void OnPaint(); 39 afx_msg HCURSOR OnQueryDragIcon(); 40 afx_msg void OnTimer(UINT nIDEvent); 41 DECLARE_MESSAGE_MAP() 42 };

步驟三: 設(shè)置對(duì)話框的源文件***Dlg.cpp

? ? a. 開啟定時(shí)器消息循環(huán)

? ? 在消息循環(huán)的代碼塊中加入ON_WM_TIMER()消息循環(huán):

1 BEGIN_MESSAGE_MAP(COpenGLTest1Dlg, CDialogEx) 2 ON_WM_SYSCOMMAND() 3 ON_WM_PAINT() 4 ON_WM_QUERYDRAGICON() 5 ON_WM_TIMER() 6 END_MESSAGE_MAP()

? ? 這里的OnTimer函數(shù)用于相應(yīng)SetTimer消息。當(dāng)SetTimer設(shè)置的時(shí)間到了,就會(huì)自動(dòng)調(diào)用OnTimer()函數(shù)。

? ? 寫OnTimer函數(shù)的函數(shù)體,如下所示:

1 void COpenGLTest1Dlg::OnTimer(UINT nIDEvent) //實(shí)時(shí)繪制場景 2 { 3 // TODO: Add your message handler code here and/or call default 4 RenderScene(); 5 m_yRotate +=3; 6 CDialog::OnTimer(nIDEvent); 7 }

?

?? ??b. 寫函數(shù)SetWindowPixelFormat,用于生成像素格式

? ??函數(shù)體如下所示:

?

1 BOOL COpenGLTest1Dlg::SetWindowPixelFormat(HDC hDC) 2 { 3 PIXELFORMATDESCRIPTOR pixelDesc; 4 5 pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR); 6 pixelDesc.nVersion = 1; 7 8 pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | 9 PFD_SUPPORT_OPENGL | 10 PFD_DOUBLEBUFFER | 11 PFD_TYPE_RGBA; 12 13 pixelDesc.iPixelType = PFD_TYPE_RGBA; 14 pixelDesc.cColorBits = 32; 15 pixelDesc.cRedBits = 0; 16 pixelDesc.cRedShift = 0; 17 pixelDesc.cGreenBits = 0; 18 pixelDesc.cGreenShift = 0; 19 pixelDesc.cBlueBits = 0; 20 pixelDesc.cBlueShift = 0; 21 pixelDesc.cAlphaBits = 0; 22 pixelDesc.cAlphaShift = 0; 23 pixelDesc.cAccumBits = 0; 24 pixelDesc.cAccumRedBits = 0; 25 pixelDesc.cAccumGreenBits = 0; 26 pixelDesc.cAccumBlueBits = 0; 27 pixelDesc.cAccumAlphaBits = 0; 28 pixelDesc.cDepthBits = 0; 29 pixelDesc.cStencilBits = 1; 30 pixelDesc.cAuxBuffers = 0; 31 pixelDesc.iLayerType = PFD_MAIN_PLANE; 32 pixelDesc.bReserved = 0; 33 pixelDesc.dwLayerMask = 0; 34 pixelDesc.dwVisibleMask = 0; 35 pixelDesc.dwDamageMask = 0; 36 37 PixelFormat = ChoosePixelFormat(hDC,&pixelDesc); 38 if(PixelFormat==0) // Choose default 39 { 40 PixelFormat = 1; 41 if(DescribePixelFormat(hDC,PixelFormat, 42 sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0) 43 { 44 return FALSE; 45 } 46 } 47 48 if(SetPixelFormat(hDC,PixelFormat,&pixelDesc)==FALSE) 49 50 { 51 return FALSE; 52 } 53 54 return TRUE; 55 }

?

? ? ?c. 寫函數(shù)CreateViewGLContext,用于生成渲染上下文

? ? 具體函數(shù)體如下:

1 BOOL COpenGLTest1Dlg::CreateViewGLContext(HDC hDC) 2 { 3 hrenderRC = wglCreateContext(hDC); 4 5 if(hrenderRC==NULL) 6 return FALSE; 7 8 if(wglMakeCurrent(hDC,hrenderRC)==FALSE) 9 return FALSE; 10 11 12 13 return TRUE; 14 }

? ??d. 寫函數(shù)RenderScene,用于繪制場景

? ? 具體函數(shù)體如下:

1 void COpenGLTest1Dlg::RenderScene() 2 { 3 4 5 / 6 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 7 8 9 glLoadIdentity(); 10 glTranslatef(0.0f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0 11 glRotated(m_yRotate, 0.0, 1.0, 0.0); 12 glBegin(GL_TRIANGLES); // Drawing Using Triangles 13 14 glVertex3f( 0.0f, 1.0f, 0.0f); // Top 15 glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left 16 glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right 17 glEnd(); // Finished Drawing The Triangle 18 SwapBuffers(hrenderDC); 19 }

? ??e. 在對(duì)話框初始化程序OnInitDialog中添加初始化代碼

? ? 具體代碼如下:

1 ///OPENGL INIT/ 2 CWnd *wnd=GetDlgItem(IDC_RENDER); 3 hrenderDC=::GetDC(wnd->m_hWnd); 4 if(SetWindowPixelFormat(hrenderDC)==FALSE) 5 return 0; 6 7 if(CreateViewGLContext(hrenderDC)==FALSE) 8 return 0; 9 10 glPolygonMode(GL_FRONT,GL_FILL); 11 glPolygonMode(GL_BACK,GL_FILL); 12 /// 13 glEnable(GL_TEXTURE_2D); 14 glShadeModel(GL_SMOOTH); 15 glViewport(0,0,259,231); 16 glMatrixMode(GL_PROJECTION); 17 glLoadIdentity(); 18 gluPerspective(45,1,0.1,100.0); 19 glMatrixMode(GL_MODELVIEW); 20 glLoadIdentity(); 21 glShadeModel(GL_SMOOTH); // Enable Smooth Shading 22 glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background 23 glClearDepth(1.0f); // Depth Buffer Setup 24 glEnable(GL_DEPTH_TEST); // Enables Depth Testing 25 glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do 26 / 27 glEnableClientState(GL_VERTEX_ARRAY); 28 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 29 30 SetTimer(1,10,0); 31 32 ////

? ??f. 整個(gè).cpp源代碼

1 // OpenGLTest1Dlg.cpp : 實(shí)現(xiàn)文件 2 // 3 4 #include "stdafx.h" 5 #include "OpenGLTest1.h" 6 #include "OpenGLTest1Dlg.h" 7 #include "afxdialogex.h" 8 9 10 #ifdef _DEBUG 11 #define new DEBUG_NEW 12 #endif 13 14 15 // 用于應(yīng)用程序“關(guān)于”菜單項(xiàng)的 CAboutDlg 對(duì)話框 16 17 class CAboutDlg : public CDialogEx 18 { 19 public: 20 CAboutDlg(); 21 22 // 對(duì)話框數(shù)據(jù) 23 enum { IDD = IDD_ABOUTBOX }; 24 25 protected: 26 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 27 28 // 實(shí)現(xiàn) 29 protected: 30 DECLARE_MESSAGE_MAP() 31 }; 32 33 CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD) 34 { 35 } 36 37 void CAboutDlg::DoDataExchange(CDataExchange* pDX) 38 { 39 CDialogEx::DoDataExchange(pDX); 40 } 41 42 BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx) 43 END_MESSAGE_MAP() 44 45 46 // COpenGLTest1Dlg 對(duì)話框 47 48 49 50 51 COpenGLTest1Dlg::COpenGLTest1Dlg(CWnd* pParent /*=NULL*/) 52 : CDialogEx(COpenGLTest1Dlg::IDD, pParent) 53 { 54 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 55 } 56 57 void COpenGLTest1Dlg::DoDataExchange(CDataExchange* pDX) 58 { 59 CDialogEx::DoDataExchange(pDX); 60 } 61 62 BEGIN_MESSAGE_MAP(COpenGLTest1Dlg, CDialogEx) 63 ON_WM_SYSCOMMAND() 64 ON_WM_PAINT() 65 ON_WM_QUERYDRAGICON() 66 ON_WM_TIMER() 67 END_MESSAGE_MAP() 68 69 70 // COpenGLTest1Dlg 消息處理程序 71 72 BOOL COpenGLTest1Dlg::OnInitDialog() 73 { 74 CDialogEx::OnInitDialog(); 75 76 // 將“關(guān)于...”菜單項(xiàng)添加到系統(tǒng)菜單中。 77 78 // IDM_ABOUTBOX 必須在系統(tǒng)命令范圍內(nèi)。 79 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); 80 ASSERT(IDM_ABOUTBOX < 0xF000); 81 82 CMenu* pSysMenu = GetSystemMenu(FALSE); 83 if (pSysMenu != NULL) 84 { 85 BOOL bNameValid; 86 CString strAboutMenu; 87 bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX); 88 ASSERT(bNameValid); 89 if (!strAboutMenu.IsEmpty()) 90 { 91 pSysMenu->AppendMenu(MF_SEPARATOR); 92 pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); 93 } 94 } 95 96 // 設(shè)置此對(duì)話框的圖標(biāo)。當(dāng)應(yīng)用程序主窗口不是對(duì)話框時(shí),框架將自動(dòng) 97 // 執(zhí)行此操作 98 SetIcon(m_hIcon, TRUE); // 設(shè)置大圖標(biāo) 99 SetIcon(m_hIcon, FALSE); // 設(shè)置小圖標(biāo) 100 101 // TODO: 在此添加額外的初始化代碼 102 ///OPENGL INIT/ 103 CWnd *wnd=GetDlgItem(IDC_RENDER); 104 hrenderDC=::GetDC(wnd->m_hWnd); 105 if(SetWindowPixelFormat(hrenderDC)==FALSE) 106 return 0; 107 108 if(CreateViewGLContext(hrenderDC)==FALSE) 109 return 0; 110 111 glPolygonMode(GL_FRONT,GL_FILL); 112 glPolygonMode(GL_BACK,GL_FILL); 113 /// 114 glEnable(GL_TEXTURE_2D); 115 glShadeModel(GL_SMOOTH); 116 glViewport(0,0,259,231); 117 glMatrixMode(GL_PROJECTION); 118 glLoadIdentity(); 119 gluPerspective(45,1,0.1,100.0); 120 glMatrixMode(GL_MODELVIEW); 121 glLoadIdentity(); 122 glShadeModel(GL_SMOOTH); // Enable Smooth Shading 123 glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background 124 glClearDepth(1.0f); // Depth Buffer Setup 125 glEnable(GL_DEPTH_TEST); // Enables Depth Testing 126 glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do 127 / 128 glEnableClientState(GL_VERTEX_ARRAY); 129 glEnableClientState(GL_TEXTURE_COORD_ARRAY); 130 131 SetTimer(1,10,0); 132 133 //// 134 return TRUE; // 除非將焦點(diǎn)設(shè)置到控件,否則返回 TRUE 135 } 136 137 void COpenGLTest1Dlg::OnSysCommand(UINT nID, LPARAM lParam) 138 { 139 if ((nID & 0xFFF0) == IDM_ABOUTBOX) 140 { 141 CAboutDlg dlgAbout; 142 dlgAbout.DoModal(); 143 } 144 else 145 { 146 CDialogEx::OnSysCommand(nID, lParam); 147 } 148 } 149 150 // 如果向?qū)υ捒蛱砑幼钚』粹o,則需要下面的代碼 151 // 來繪制該圖標(biāo)。對(duì)于使用文檔/視圖模型的 MFC 應(yīng)用程序, 152 // 這將由框架自動(dòng)完成。 153 154 void COpenGLTest1Dlg::OnPaint() 155 { 156 if (IsIconic()) 157 { 158 CPaintDC dc(this); // 用于繪制的設(shè)備上下文 159 160 SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0); 161 162 // 使圖標(biāo)在工作區(qū)矩形中居中 163 int cxIcon = GetSystemMetrics(SM_CXICON); 164 int cyIcon = GetSystemMetrics(SM_CYICON); 165 CRect rect; 166 GetClientRect(&rect); 167 int x = (rect.Width() - cxIcon + 1) / 2; 168 int y = (rect.Height() - cyIcon + 1) / 2; 169 170 // 繪制圖標(biāo) 171 dc.DrawIcon(x, y, m_hIcon); 172 } 173 else 174 { 175 CDialogEx::OnPaint(); 176 } 177 } 178 179 //當(dāng)用戶拖動(dòng)最小化窗口時(shí)系統(tǒng)調(diào)用此函數(shù)取得光標(biāo) 180 //顯示。 181 HCURSOR COpenGLTest1Dlg::OnQueryDragIcon() 182 { 183 return static_cast<HCURSOR>(m_hIcon); 184 } 185 186 BOOL COpenGLTest1Dlg::SetWindowPixelFormat(HDC hDC) 187 { 188 PIXELFORMATDESCRIPTOR pixelDesc; 189 190 pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR); 191 pixelDesc.nVersion = 1; 192 193 pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW | 194 PFD_SUPPORT_OPENGL | 195 PFD_DOUBLEBUFFER | 196 PFD_TYPE_RGBA; 197 198 pixelDesc.iPixelType = PFD_TYPE_RGBA; 199 pixelDesc.cColorBits = 32; 200 pixelDesc.cRedBits = 0; 201 pixelDesc.cRedShift = 0; 202 pixelDesc.cGreenBits = 0; 203 pixelDesc.cGreenShift = 0; 204 pixelDesc.cBlueBits = 0; 205 pixelDesc.cBlueShift = 0; 206 pixelDesc.cAlphaBits = 0; 207 pixelDesc.cAlphaShift = 0; 208 pixelDesc.cAccumBits = 0; 209 pixelDesc.cAccumRedBits = 0; 210 pixelDesc.cAccumGreenBits = 0; 211 pixelDesc.cAccumBlueBits = 0; 212 pixelDesc.cAccumAlphaBits = 0; 213 pixelDesc.cDepthBits = 0; 214 pixelDesc.cStencilBits = 1; 215 pixelDesc.cAuxBuffers = 0; 216 pixelDesc.iLayerType = PFD_MAIN_PLANE; 217 pixelDesc.bReserved = 0; 218 pixelDesc.dwLayerMask = 0; 219 pixelDesc.dwVisibleMask = 0; 220 pixelDesc.dwDamageMask = 0; 221 222 PixelFormat = ChoosePixelFormat(hDC,&pixelDesc); 223 if(PixelFormat==0) // Choose default 224 { 225 PixelFormat = 1; 226 if(DescribePixelFormat(hDC,PixelFormat, 227 sizeof(PIXELFORMATDESCRIPTOR),&pixelDesc)==0) 228 { 229 return FALSE; 230 } 231 } 232 233 if(SetPixelFormat(hDC,PixelFormat,&pixelDesc)==FALSE) 234 235 { 236 return FALSE; 237 } 238 239 return TRUE; 240 } 241 242 243 BOOL COpenGLTest1Dlg::CreateViewGLContext(HDC hDC) 244 { 245 hrenderRC = wglCreateContext(hDC); 246 247 if(hrenderRC==NULL) 248 return FALSE; 249 250 if(wglMakeCurrent(hDC,hrenderRC)==FALSE) 251 return FALSE; 252 253 254 255 return TRUE; 256 } 257 258 void COpenGLTest1Dlg::RenderScene() 259 { 260 261 262 / 263 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 264 265 266 glLoadIdentity(); 267 glTranslatef(0.0f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0 268 glRotated(m_yRotate, 0.0, 1.0, 0.0); 269 glBegin(GL_TRIANGLES); // Drawing Using Triangles 270 271 glVertex3f( 0.0f, 1.0f, 0.0f); // Top 272 glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left 273 glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right 274 glEnd(); // Finished Drawing The Triangle 275 SwapBuffers(hrenderDC); 276 } 277 278 void COpenGLTest1Dlg::OnTimer(UINT nIDEvent) //實(shí)時(shí)繪制場景 279 { 280 // TODO: Add your message handler code here and/or call default 281 RenderScene(); 282 m_yRotate +=3; 283 CDialog::OnTimer(nIDEvent); 284 }

步驟四: 運(yùn)行調(diào)試

? ? 運(yùn)行結(jié)果如下所示:

?

轉(zhuǎn)載于:https://www.cnblogs.com/wiener-zyj/p/4159310.html

總結(jié)

以上是生活随笔為你收集整理的《MFC dialog中加入OpenGL窗体》的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。