计算机图形学编程练习8mfc-明暗处理实现

上传人:F****n 文档编号:100169983 上传时间:2019-09-22 格式:DOC 页数:8 大小:474KB
返回 下载 相关 举报
计算机图形学编程练习8mfc-明暗处理实现_第1页
第1页 / 共8页
计算机图形学编程练习8mfc-明暗处理实现_第2页
第2页 / 共8页
计算机图形学编程练习8mfc-明暗处理实现_第3页
第3页 / 共8页
计算机图形学编程练习8mfc-明暗处理实现_第4页
第4页 / 共8页
计算机图形学编程练习8mfc-明暗处理实现_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《计算机图形学编程练习8mfc-明暗处理实现》由会员分享,可在线阅读,更多相关《计算机图形学编程练习8mfc-明暗处理实现(8页珍藏版)》请在金锄头文库上搜索。

1、计算机图形学编程练习8:MFC+明暗处理实现MFC与OpenGL集成在Windows下编程,利用MFC是一个非常便捷的方法。本次练习的主要目的,是希望同学们在MFC应用程序框架下进行OpenGL编程。为此,需要对MFC生成的应用程序进行适当的初始化,关于这方面的内容详见: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是流水线结构(streamlined)、硬件无关(hardware-independent)、跨平台的3D图形编程API。但是,在实际应用时,OpenGL的具体实现是与操作系统以及图形硬件相关的。

3、为此,操作系统需要提供像素格式(pixel format)与绘制上下文管理函数(rendering context managnment functions)。Windows操作系统提供了通用图形设备接口(generic graphics device interface, GDI)以及设备驱动实现。为了使OpenGL命令得到正确的执行,需要调用WGL函数,具体的步骤如下:Step 1: 添加成员变量在CView类(利用AppWizard生成)中添加如下成员变量:/ OpenGL Windows specificationHDC m_hDC;/ Device ContextHGLRC m_hG

4、LRC;/ Rendering ContextCPalette m_cGLLP;/ Logical PaletteStep 2: 设置像素格式创建CView类的WM_CREATE的消息响应函数,进行像素格式的设置,例如: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: 创建绘制上下文该步骤在Step 2中已完成,具体的就是:m_hGLRC = wglCreateContext(m_hDC);Step 4: 设置调色板创建CView类的一个成员函数,进行调色板的设置,例如: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 = 0x300;/ 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 entr

13、iesfor (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 * 255.0 / GreenRange);pPal-palPalEntryi.peBlue = (i pfd.cBlueShift) & BlueRange;pPal-palPalEntryi.peBlue = (unsigned char)(double)pPal-palPalEntryi.peBlue * 255

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号