




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、光荣在于平淡,艰巨因为漫长基于mfc的opengl编程系列文章 1,基于mfc的opengl编程part 1 a primer2,基于mfc的opengl编程part 2 setting up opengl on windows3,基于mfc的opengl编程part 3 drawing simple 2d shapes4,基于mfc的opengl编程part 4 drawing simple 3d objects5,基于mfc的opengl编程part 5 transformations - rotations, translations and scaling6,基于mfc的opengl编
2、程part 6 keyboard and mouse control7,基于mfc的opengl编程part 7 animation8,基于mfc的opengl编程part 8 colors9,基于mfc的opengl编程part 9 lighting10,基于mfc的opengl编程part 10 texture mapping11,基于mfc的opengl编程part 11 blending, antialiasing and fog12,基于mfc的opengl编程part 12 creating and using display lists13,基于mfc的opengl编程part
3、13 creating 2d and 3d text 14,基于mfc的opengl编程part 14 quadrics 15,基于mfc的opengl编程part 15 selection16,基于mfc的opengl编程part 16 reflection17,基于mfc的opengl编程part 17 shadows18,基于mfc的opengl编程part 18 reading objects from the obj file format19,基于mfc的opengl编程part 19 creating a virtual reality walkthrough applicati
4、on2 0 0 (请您对文章做出评价)光荣在于平淡,艰巨因为漫长基于mfc的opengl编程part 1 a primer 3d图形学基本概念perspectiveperspective refers to the angles between the lines that lend the illusion of three dimensions. colors and shadingmoving beyond line drawing, we need to add color to create a solid object. shading refers to the way the
5、color is applied to the polygon. shading can be of two types in opengl - flat or smooth.lights and shadowsplain solid color doesnt offer enough realism. by applying lighting effects we can make objects appear as they would in reality depending on their material properties and the lighting parameters
6、. adding a shadow further increases realism.texture mappingwith texture mapping we can have wood grains, cloth textures, brick like textures etc instead of plain materials. this technique of applying an image to the surface of a polygon is called texture mapping. the image we use is called the textu
7、re and the individual elements of the texture are called texels.fogfog is an atmospheric effect that adds haziness to objects in a scene depending on how far the objects are from the viewer. blending and transparencyblending is the combination of colors of objects on the screen. this effect can be u
8、sed for a variety of purposes. by varying the amount each object is blended with the scene we can make objects look transparent.anti-aliasingaliasing is an effect that is visible on screen due to the fact that an image consists of discrete pixels. by carefully blending the lines with the background
9、color we can eliminate jagged edges and give them a smooth appearance. this blending technique is called anti-aliasing. 第一个opengl程序/simple.cpp-firstopenglprogram#include/requiredforeverywindowsprogram#include/requiredforusingtheglutlibrary/performopenglinitializationherevoidsetuprc()/setthebackgroun
10、dclearingcolortoblueglclearcolor(0.0f,0.0f,1.0f,1.0f);/设置背景色为蓝色/thedrawingcallbackfunctionvoidrenderscene()/clearthecolorbufferglclear(gl_color_buffer_bit);/flushtherenderingpipelineglflush();voidmain()/choosethedisplaymodesettingsglutinitdisplaymode(glut_single|glut_rgb);/初始化显示模式(单缓冲,rgb)/createthe
11、windowglutcreatewindow(simple);/创建窗口/settherenderscsnefunctionasthedisplaycallbackglutdisplayfunc(renderscene);/绘制回调函数,当窗口需要绘制时,glut会调用此函数/initializeopenglsetuprc();/初始化opengl/starttheglutframeworkglutmainloop();/开始消息循环光荣在于平淡,艰巨因为漫长基于mfc的opengl编程part 2 setting up opengl on windows 源代码下载:opengl_ch2.r
12、arwgl windows的 opengl扩展层 the wgl extension consists of a set of functions (wglcreatecontext, wgldeletecontext etc.) and structures (such as pixelformatdescriptor, glyphmetricsfloat) etc. thus every opengl implementation has a platform-specific portion which has to be set up and used according to the
13、 particular platform. 设备上下文the windows graphical device interface (gdi) is capable of drawing to screen, to memory, to printers or to any other device that provides a gdi interface layer and that can process gdi calls. gdi accomplishes this by a rendering handle to the currently selected device, whi
14、ch is called the device context, or dc. 绘制上下文a rendering context is the opengl equivalent of the gdi dc. all opengl calls are rendered to the device through a rc. the rendering context maintains opengl state variables such as current background color, current color etc. just as the dc maintains gdi
15、state variables such as current pen, current brush etc. 像素格式pixel formats are the translation layer between opengl calls and the rendering operation that windows performs. 举个例子,若像素格式只支持很少一部分颜色值,则opengl在用rgb值(128,120,135)绘制一个像素时,就可能使用转换后的值(128,128,128)来绘制. the pixel format selected essentially descri
16、bes such things as how colors are displayed, depth of field resolution and what additional capabilities are supported by the rendering context created. 第一个基于mfc的opengl应用程开发环境:vc6.01, 首先下载需要的glut头文件,dll和lib文件,下载链接: glutdlls37beta.zip (149 kilobytes),解压缩后把gltu.h放到vc98/include/gl下,把glut.lib和glut32.lib放
17、到vc9/lib 下,glut32.dll和glut.dll放到你创建的应用程序的运行目录下2, 创建一个mfc sdi应用程序,在项目属性中加入所需要链接的库文件1, 在stdafx.h中加入下列语句:/openglheaders#include#include#include#include2, 打开classwizard,选择ccy457openglview类,为下述消息加入消息处理函数:wm_create (for oncreate), wm_destroy (for ondestroy), wm_size (for onsize), wm_erasebackground (for o
18、nerasebkground).3,在窗口创建之前我们必须设置窗口风格包含ws_clipchildren和 ws_clipsiblings,从而避免opengl绘制到其他窗口中去。这些应该放在precreatewindow()中。boolccy457openglview:precreatewindow(createstruct&cs)/todo:modifythewindowclassorstylesherebymodifying/thecreatestructcs/anopenglwindowmustbecreatedwiththefollowingflagscs.style|=ws_cli
19、psiblings|ws_clipchildren;returncview:precreatewindow(cs);4,在ccy457openglview.h中加入如下语句:hglrcm_hrc;/renderingcontextcdc*m_pdc;/devicecontextboolinitializeopengl();/initializeopenglboolsetuppixelformat();/setupthepixelformatvoidrenderscene();/renderthescene5,在oncreate中我们将通过建立像素格式和绘制上下文来初始化opengl. 在ini
20、tializeopengl()中会创建一个设备上下文(dc),为这个dc选择一个像素格式,创建和这个dc相关的绘制上下文(rc),然后选择这个rc.这个函数会调用setuppixelformat()来建立像素格式。intccy457openglview:oncreate(lpcreatestructlpcreatestruct)if(cview:oncreate(lpcreatestruct)=-1)return-1;/initializeopenglhereinitializeopengl();return0;boolccy457openglview:initializeopengl()/g
21、etadcfortheclientaream_pdc=newcclientdc(this);/failuretogetdcif(m_pdc=null)messagebox(errorobtainingdc);returnfalse;/failuretosetthepixelformatif(!setuppixelformat()returnfalse;/createrenderingcontextm_hrc=:wglcreatecontext(m_pdc-getsafehdc();/failuretocreaterenderingcontextif(m_hrc=0)messagebox(err
22、orcreatingrc);returnfalse;/maketherccurrentif(:wglmakecurrent(m_pdc-getsafehdc(),m_hrc)=false)messagebox(errormakingrccurrent);returnfalse;/specifyblackastheclearcolor:glclearcolor(0.0f,0.0f,0.0f,0.0f);/specifythebackofthebufferascleardepth:glcleardepth(1.0f);/enabledepthtesting:glenable(gl_depth_te
23、st);returntrue;/setuppixelformat/boolccy457openglview:setuppixelformat()staticpixelformatdescriptorpfd=sizeof(pixelformatdescriptor),/sizeofthispfd1,/versionnumberpfd_draw_to_window|/supportwindowpfd_support_opengl|/supportopenglpfd_doublebuffer,/doublebufferedpfd_type_rgba,/rgbatype24,/24-bitcolord
24、epth0,0,0,0,0,0,/colorbitsignored0,/noalphabuffer0,/shiftbitignored0,/noaccumulationbuffer0,0,0,0,/accumbitsignored16,/16-bitz-buffer0,/nostencilbuffer0,/noauxiliarybufferpfd_main_plane,/mainlayer0,/reserved0,0,0/layermasksignored;intm_npixelformat=:choosepixelformat(m_pdc-getsafehdc(),&pfd);if(m_np
25、ixelformat=0)returnfalse;if(:setpixelformat(m_pdc-getsafehdc(),m_npixelformat,&pfd)=false)returnfalse;returntrue;6,在onsize()中一般用来设置视口和视锥,因为这些是和窗口大小相关的。基本操作包括设置视口,选择投影矩阵,设置模型视图矩阵。voidccy457openglview:onsize(uintntype,intcx,intcy)cview:onsize(ntype,cx,cy);gldoubleaspect_ratio;/width/heightratioif(0=cx
26、|0=cy)return;/selectthefullclientarea:glviewport(0,0,cx,cy);/computetheaspectratio/thiswillkeepalldimensionscalesequalaspect_ratio=(gldouble)cx/(gldouble)cy;/selecttheprojectionmatrixandclearit:glmatrixmode(gl_projection);:glloadidentity();/selecttheviewingvolume:gluperspective(45.0f,aspect_ratio,.0
27、1f,200.0f);/switchbacktothemodelviewmatrixandclearit:glmatrixmode(gl_modelview);:glloadidentity();7,在绘制场景时,一般包括如下步骤:1)清空缓存。2)绘制场景。3)flush掉渲染流水线。4)若设置了双缓冲,则交换前后台缓冲区。voidccy457openglview:ondraw(cdc*pdc)ccy457opengldoc*pdoc=getdocument();assert_valid(pdoc);/clearoutthecolor&depthbuffers:glclear(gl_colo
28、r_buffer_bit|gl_depth_buffer_bit);renderscene();/tellopengltoflushitspipeline:glfinish();/nowswapthebuffers:swapbuffers(m_pdc-getsafehdc();voidccy457openglview:renderscene()/第一个玩具嘛,先空着,后面慢慢填8,试试改变窗口的大小,你会看到很严重的闪烁,并且关闭程序后会报告内存泄露,因此我们这就来解决这两个问题吧。发生闪烁的原因是windows先绘制背景,然后再是opengl绘制,因为我们已经让opengl负责清空背景色,因
29、此我们不需要windows去清空背景了boolccy457openglview:onerasebkgnd(cdc*pdc)/tellwindowsnottoerasethebackgroundreturntrue;内存泄露的原因是我们在setuppixelformat()中使用了new运算符来为cclientdc对象分配内存,因此需要显示delete掉。voidccy457openglview:ondestroy()cview:ondestroy();/makethercnon-currentif(:wglmakecurrent(0,0)=false)messagebox(couldnotma
30、kercnon-current);/deletetherenderingcontextif(:wgldeletecontext(m_hrc)=false)messagebox(couldnotdeleterc);/deletethedcif(m_pdc)deletem_pdc;/setittonullm_pdc=null;光荣在于平淡,艰巨因为漫长基于mfc的opengl编程part 3 drawing simple 2d shapes 剪裁区域in opengl when you create a window to draw in we must specify the coordinat
31、e system we want to use and how to map the specified coordinates into physical screen coordinates. we would be using the 2d cartesian coordinate system with the origin 0,0 at the centre of the screen. before we can start plotting points, lines and shapes in a window we must also specify how to trans
32、late coordinate pairs into screen coordinates, by specifying the clipping area i.e the region of cartesian space that occupies the window.视口the clipping area height and width will rarely match the width and height of the window in pixels. the coordinate system must therefore be mapped from logical c
33、artesian coordinates to physical screen coordinates. this mapping is specified by a setting known as the viewport, which is the region within the windows client area that is used for drawing the clipping area.顶点和基本图元a vertex is nothing more than a coordinate in 2d or 3d space. in both 2d and 3d, whe
34、n we draw an object we compose it with several smaller shapes called primitives which as 1 or 2 dimensional entities such as points, lines, and polygons. each corner of an object composed of primitives is a vertex基本图形绘制程序1,在ccy457openglview.h中加入下列变量:boolm_bpoint;/statusofpointboolm_bline;/statusofli
35、neboolm_bpolygon;/statusofpolygonboolm_btriangle;/statusoftriangle2,并且加入四个菜单项及其对应的事件处理程序。voidccy457openglview:onshapespoint()/画点m_bpoint=true;m_bline=false;m_bpolygon=false;m_btriangle=false;invalidaterect(null,false);voidccy457openglview:onshapesline()/画线m_bpoint=false;m_bline=true;m_bpolygon=false
36、;m_btriangle=false;invalidaterect(null,false);voidccy457openglview:onshapespolygon()/画多边形m_bpoint=false;m_bline=false;m_bpolygon=true;m_btriangle=false;invalidaterect(null,false);voidccy457openglview:onshapestriangle()/画三角形m_bpoint=false;m_bline=false;m_bpolygon=false;m_btriangle=true;invalidaterect
37、(null,false);3,修改第二篇文章中的onsize()函数,因为本文中只绘制2维图形.voidccy457openglview:onsize(uintntype,intcx,intcy)cview:onsize(ntype,cx,cy);gldoubleaspect_ratio;/width/heightratioif(0=cx|0=cy)return;/selectthefullclientarea:glviewport(0,0,cx,cy);/computetheaspectratio/thiswillkeepalldimensionscalesequalaspect_ratio
38、=(gldouble)cx/(gldouble)cy;/selecttheprojectionmatrixandclearit:glmatrixmode(gl_projection);:glloadidentity();/selecttheviewingvolume/:gluperspective(45.0f,aspect_ratio,.01f,200.0f);:gluortho2d(-10.0f,10.0f,-10.0f,10.0f);/switchbacktothemodelviewmatrixandclearit:glmatrixmode(gl_modelview);:glloadide
39、ntity();4,在renderscene中加入具体的绘制代码:voidccy457openglview:renderscene()/绘制函数if(m_bpoint=true)glpointsize(3.0f);glbegin(gl_points);glvertex2f(0.0f,0.0f);glvertex2f(1.0f,0.0f);glvertex2f(0.0f,1.0f);glend();elseif(m_bline=true)glbegin(gl_lines);glvertex2f(0.0f,0.0f);glvertex2f(1.0f,0.0f);glend();elseif(m_b
40、triangle=true)glbegin(gl_triangles);glvertex2f(0.0f,0.0f);glvertex2f(2.0f,0.0f);glvertex2f(0.0f,2.0f);glend();elseif(m_bpolygon=true)glbegin(gl_polygon);glvertex2f(0.0f,0.0f);glvertex2f(3.0f,0.0f);glvertex2f(4.0f,3.0f);glvertex2f(1.5f,6.0f);glvertex2f(-1.0f,3.0f);glend();光荣在于平淡,艰巨因为漫长基于mfc的opengl编程p
41、art 4 drawing simple 3d objects 视见体 viewing volume is nothing but the region of 3d cartesian space in that will occupy the window. it is nothing but the minimum and maximum x, y and z values that are inside the window. so if a vertex is outside this range of x, y and z values then they are clipped b
42、y opengl before rendering can occur. z bufferthe new term we have to deal with in addition to width and height of an object in 3d graphics is depth. the depth of an object is its distance from the viewpoint. the viewpoint is the location from which we are looking at that point. this depth value goes
43、 into the depth or z-buffer. if we are drawing 2 objects that have some pixels that overlap, the first object will after it is rendered have its depth value in the depth buffer. when the next object is rendered, opengl will check to see whether the pixel its about to draw is in front of (with respec
44、t to the viewpoint) any pixel from the first object thats already drawn. it does this by checking the z value of the current pixel with the value that is already in the buffer. if the new pixel is closer to the viewpoint, opengl places its depth value in the depth buffer. this is how the z-buffer wo
45、rks.正交投影和透视投影one term we need to understand very well to learn 3d graphics well is projection. well, computer graphics at its simplest is all about setting a color to a pixel on screen. and a pixel on a screen can have only two dimensions. so 3d graphics is merely an illusion. the 3d coordinates tha
46、t we specify will have to be projected onto a 2d surface to create this illusion for us. and we have to specify how this projection works. by specifying a projection we specify the clipping or viewing volume. 基本3d图形绘制1,在ccy457openglview.h中加入下列变量boolm_bpoint;/statusofpointboolm_bline;/statusoflineboolm_bpolygon;/statusofpolygonboolm_btriangle;/statusoftriangle并且在构造函数中初始化cc
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 企业循环贷款合同范本
- 国际技术转让合同(范本)
- 住房公积金联名贷款合同
- 土地抵押借款合同例文
- 度重型吊车租赁合同
- 连锁加盟合伙经营合同格式
- 建筑装修工程合同协议
- 小型建筑企业贷款合同协议书
- 网络广告位出租合同协议
- 能源项目合作合同
- 2024年济南广播电视台招聘工作人员笔试真题
- 2025年临床医师定期考核必考复习题库及答案(1060题)
- 市场消防安全课件
- 名师工作室建设课件
- 2025-2025学年度人教版小学五年级美术下册教学计划
- 《电力建设工程施工安全管理导则》(NB∕T 10096-2018)
- 2024-2025学年广东省部分学校高一(上)第一次联合考试物理试卷(含答案)
- 《黄色新闻的泛滥》课件
- 2024年山东省公务员考试《行测》真题及答案解析
- 化工原理Ⅱ学习通超星期末考试答案章节答案2024年
- 2024-2025学年初中体育与健康九年级全一册人教版(2024)教学设计合集
评论
0/150
提交评论