版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
1、計算機圖形學編程練習8:MFC+明暗處理實現(xiàn)MFC與OpenGL集成在Windows下編程,利用MFC是一個非常便捷的方法。本次練習的主要目的,是希望同學們在MFC應用程序框架下進行OpenGL編程。為此,需要對MFC生成的應用程序進行適當?shù)某跏蓟P于這方面的內(nèi)容詳見:1 Crain, Dennis. Windows NT OpenGL: Getting Started. April 1994. (MSDN Library, Technical Articles)2 Rogerson, Dale. OpenGL I: Quick Start. December 1994. (MSDN Lib
2、rary, Technical Articles)3 D. Shreiner and The Khronos OpenGL ARB Working Group. OpenGL Programming Guide: The Official Guide to Learning OpenGL, Versions 3.0 and 3.1, 7th Ed., 2009. (附錄D)從設計目標來說,OpenGL是流水線結(jié)構(gòu)(streamlined)、硬件無關(hardware-independent)、跨平臺的3D圖形編程API。但是,在實際應用時,OpenGL的具體實現(xiàn)是與操作系統(tǒng)以及圖形硬件相關的。
3、為此,操作系統(tǒng)需要提供像素格式(pixel format)與繪制上下文管理函數(shù)(rendering context managnment functions)。Windows操作系統(tǒng)提供了通用圖形設備接口(generic graphics device interface, GDI)以及設備驅(qū)動實現(xiàn)。為了使OpenGL命令得到正確的執(zhí)行,需要調(diào)用WGL函數(shù),具體的步驟如下:Step 1: 添加成員變量在CView類(利用AppWizard生成)中添加如下成員變量:/ OpenGL Windows specificationHDC m_hDC;/ Device ContextHGLRC m_hG
4、LRC;/ Rendering ContextCPalette m_cGLLP;/ Logical PaletteStep 2: 設置像素格式創(chuàng)建CView類的WM_CREATE的消息響應函數(shù),進行像素格式的設置,例如:int COpenGLRenderView:OnCreate(LPCREATESTRUCT lpCreateStruct) if (CView:OnCreate(lpCreateStruct) = -1)return -1;/ TODO: Add your specialized creation code hereint nPixelFormat;/ Pixel format
5、 indexHWND hWnd = GetSafeHwnd();/ Get the windows handlem_hDC = :GetDC(hWnd);/ Get the Device contextstatic PIXELFORMATDESCRIPTOR pfd = sizeof(PIXELFORMATDESCRIPTOR),/ Size of this structure1,/ Version of this structurePFD_DRAW_TO_WINDOW |/ Draw to Window (not to bitmap)PFD_SUPPORT_OPENGL |/ Support
6、 OpenGL calls in windowPFD_DOUBLEBUFFER,/ Double buffered modePFD_TYPE_RGBA,/ RGBA Color mode24,/ Want 24bit color 0,0,0,0,0,0,/ Not used to select mode0,0,/ Not used to select mode0,0,0,0,0,/ Not used to select mode32,/ Size of depth buffer0,/ Not used to select mode0,/ Not used to select modePFD_M
7、AIN_PLANE,/ Draw in main plane0,/ Not used to select mode0,0,0 ;/ Not used to select mode/ Choose a pixel format that best matches that described in pfdnPixelFormat = ChoosePixelFormat(m_hDC, &pfd);/ Set the pixel format for the device contextVERIFY(SetPixelFormat(m_hDC, nPixelFormat, &pfd);/ Create
8、 the rendering contextm_hGLRC = wglCreateContext(m_hDC);/ Create the palette if neededInitializePalette();/ Make the rendering context current, perform initialization, then deselect itVERIFY(wglMakeCurrent(m_hDC, m_hGLRC);GLSetupDef(m_hDC);wglMakeCurrent(NULL, NULL);return 0;上述步驟的具體含義參看參考文獻1-3.Step
9、3: 創(chuàng)建繪制上下文該步驟在Step 2中已完成,具體的就是:m_hGLRC = wglCreateContext(m_hDC);Step 4: 設置調(diào)色板創(chuàng)建CView類的一個成員函數(shù),進行調(diào)色板的設置,例如:void CTriangularPatchView:InitializePalette(void)PIXELFORMATDESCRIPTOR pfd;/ Pixel Format DescriptorLOGPALETTE *pPal;/ Pointer to memory for logical paletteint nPixelFormat;/ Pixel format indexi
10、nt nColors;/ Number of entries in paletteint i;/ Counting variableBYTE RedRange,GreenRange,BlueRange;/ Range for each color entry (7,7,and 3)/ Get the pixel format index and retrieve the pixel format descriptionnPixelFormat = GetPixelFormat(m_hDC);DescribePixelFormat(m_hDC, nPixelFormat, sizeof(PIXE
11、LFORMATDESCRIPTOR), &pfd);/ Does this pixel format require a palette? If not, do not create a/ palette and just return NULLif (!(pfd.dwFlags & PFD_NEED_PALETTE)return;/ Number of entries in palette. 8 bits yeilds 256 entriesnColors = 1 palVersion = 0 x300;/ Windows 3.0pPal-palNumEntries = nColors; /
12、 table size/ Build mask of all 1s. This creates a number represented by having/ the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and/ pfd.cBlueBits. RedRange = (1 pfd.cRedBits) -1;GreenRange = (1 pfd.cGreenBits) - 1;BlueRange = (1 pfd.cBlueBits) -1;/ Loop through all the palette ent
13、riesfor (i = 0; i palPalEntryi.peRed = (i pfd.cRedShift) & RedRange;pPal-palPalEntryi.peRed = (unsigned char)(double) pPal-palPalEntryi.peRed * 255.0 / RedRange);pPal-palPalEntryi.peGreen = (i pfd.cGreenShift) & GreenRange;pPal-palPalEntryi.peGreen = (unsigned char)(double)pPal-palPalEntryi.peGreen
14、* 255.0 / GreenRange);pPal-palPalEntryi.peBlue = (i pfd.cBlueShift) & BlueRange;pPal-palPalEntryi.peBlue = (unsigned char)(double)pPal-palPalEntryi.peBlue * 255.0 / BlueRange);pPal-palPalEntryi.peFlags = (unsigned char) NULL;/ Create the palettem_cGLLP.CreatePalette(pPal);/ Go ahead and select and r
15、ealize the palette for this device contextSelectPalette(m_hDC,(HPALETTE)m_cGLLP,FALSE);RealizePalette(m_hDC);/ Free the memory used for the logical palette structurefree(pPal);至此,已經(jīng)可以調(diào)用OpenGL函數(shù)了,一定要記住:OpenGL命令只在獲取了正確的繪制上下文后才能正確執(zhí)行,即wglMakeCurrent(m_hDC, m_hGLRC);/ issue OpenGL commands .wglMakeCurren
16、t(m_hDC, NULL);Step 5: 重載OnEraseBkgnd函數(shù)創(chuàng)建CView類的WM_ERASEBKGND的消息響應函數(shù),防止Windows進行額外的背景清除操作,例如:BOOL COpenGLRenderView:OnEraseBkgnd(CDC* pDC) / TODO: Add your message handler code here and/or call defaultreturn FALSE;Step 6: 設置OpenGL的基本參數(shù)(可選)在CView類中添加頭文件:#include #include #include 以及成員函數(shù):void GLSetupD
17、ef(void *pData);創(chuàng)建CView類的一個成員函數(shù),進行OpenGL的基本參數(shù)的設置,例如:void COpenGLRenderView:GLSetupDef(void *pData)/ set up clear colorglClearColor(1.0f, 1.0f, 1.0f, 1.0f);/ set up lightingGLfloat light_position04 = 52.0f, 16.0f, 50.0f, 0.0f;GLfloat light_position14 = 26.0f, 48.0f, 50.0f, 0.0f;GLfloat light_position
18、24 = -16.0f, 52.0f, 50.0f, 0.0f;GLfloat direction03 = -52.0f, -16.0f, -50.0f;GLfloat direction13 = -26.0f, -48.0f, -50.0f;GLfloat direction23 = 16.0f, -52.0f, -50.0f;GLfloat color04 = 1.0f, 0.0f, 0.0f, 1.0f;GLfloat color14 = 0.0f, 1.0f, 0.0f, 1.0f;GLfloat color24 = 0.0f, 0.0f, 1.0f, 1.0f;glLightfv(G
19、L_LIGHT0, GL_POSITION, light_position0);glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction0);glLightfv(GL_LIGHT0, GL_DIFFUSE, color0);glLightfv(GL_LIGHT0, GL_SPECULAR, color0);glLightfv(GL_LIGHT1, GL_POSITION, light_position1);glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, direction1);glLightfv(GL_LIGHT1, G
20、L_DIFFUSE, color1);glLightfv(GL_LIGHT1, GL_SPECULAR, color1);glLightfv(GL_LIGHT2, GL_POSITION, light_position2);glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, direction2);glLightfv(GL_LIGHT2, GL_DIFFUSE, color2);glLightfv(GL_LIGHT2, GL_SPECULAR, color2);glEnable(GL_LIGHTING);glEnable(GL_LIGHT0);glEnable(GL
21、_LIGHT1);glEnable(GL_LIGHT2);/ set up materialsGLfloat ambient4 = 0.3f, 0.3f, 0.3f, 0.5f;GLfloat material_ambient4 = 0.0f, 0.0f, 0.0f, 0.0f;GLfloat material_diffuse4 = 1.0f, 1.0f, 1.0f, 0.3f;GLfloat material_specular4 = 0.5f, 0.5f, 0.5f, 0.5f;GLfloat material_shininess = 51.2f;glLightModelfv(GL_LIGH
22、T_MODEL_AMBIENT, ambient);glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_ambient);glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material_shininess);/ enable depth testglDepthFunc(GL_LESS);glEnable(GL_DEPTH_TEST);/ enable cull face/glCullFace(GL_BACK);/glEnable(GL_CULL_FACE);/ set up point size an
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 計算機證課程設計
- 2024合同內(nèi)容變更出現(xiàn)的爭議如何處理
- 美術非遺系列課程設計
- 體檢中心個人工作計劃
- 班主任對學生多元智能的關注計劃
- 美甲圖案課程設計
- 打造和諧班級的有效策略計劃
- 生物分離工程課程設計
- 手機軟件開發(fā)協(xié)議三篇
- 昆蟲課程設計簡單
- 地貌與公路工程-山嶺地貌(工程地質(zhì)課件)
- 人事考試服務投標方案(技術方案)
- 川教版生命生態(tài)安全二年級上冊 第10課《室內(nèi)衛(wèi)生有講究》 課件
- 湖北省武漢市2022-2023學年八年級上學期語文期中試卷(含答案)
- 術中知曉預防與腦功能監(jiān)測
- 昆明地鐵5號線 施工方案
- 1.5基爾霍夫定律
- 新教科版四年級上冊科學 2-8 食物在身體里的旅行 教學課件
- 機器設備評估常用數(shù)據(jù)與參數(shù)
- 糖尿病飲食指導健康講解課件
- 肺栓塞業(yè)務學習課件
評論
0/150
提交評論