基于MFC的OpenGL編程_第1頁(yè)
基于MFC的OpenGL編程_第2頁(yè)
基于MFC的OpenGL編程_第3頁(yè)
基于MFC的OpenGL編程_第4頁(yè)
基于MFC的OpenGL編程_第5頁(yè)
已閱讀5頁(yè),還剩98頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、光榮在于平淡,艱巨因?yàn)槁L(zhǎng)基于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 (請(qǐng)您對(duì)文章做出評(píng)價(jià))光榮在于平淡,艱巨因?yàn)槁L(zhǎng)基于mfc的opengl編程part 1 a primer 3d圖形學(xué)基本概念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. 第一個(gè)opengl程序/simple.cpp-firstopenglprogram#include/requiredforeverywindowsprogram#include/requiredforusingtheglutlibrary/performopenglinitializationherevoidsetuprc()/setthebackgroun

10、dclearingcolortoblueglclearcolor(0.0f,0.0f,1.0f,1.0f);/設(shè)置背景色為藍(lán)色/thedrawingcallbackfunctionvoidrenderscene()/clearthecolorbufferglclear(gl_color_buffer_bit);/flushtherenderingpipelineglflush();voidmain()/choosethedisplaymodesettingsglutinitdisplaymode(glut_single|glut_rgb);/初始化顯示模式(單緩沖,rgb)/createthe

11、windowglutcreatewindow(simple);/創(chuàng)建窗口/settherenderscsnefunctionasthedisplaycallbackglutdisplayfunc(renderscene);/繪制回調(diào)函數(shù),當(dāng)窗口需要繪制時(shí),glut會(huì)調(diào)用此函數(shù)/initializeopenglsetuprc();/初始化opengl/starttheglutframeworkglutmainloop();/開始消息循環(huán)光榮在于平淡,艱巨因?yàn)槁L(zhǎng)基于mfc的opengl編程part 2 setting up opengl on windows 源代碼下載:opengl_ch2.r

12、arwgl windows的 opengl擴(kuò)展層 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. 設(shè)備上下文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. 舉個(gè)例子,若像素格式只支持很少一部分顏色值,則opengl在用rgb值(128,120,135)繪制一個(gè)像素時(shí),就可能使用轉(zhuǎn)換后的值(128,128,128)來(lái)繪制. 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. 第一個(gè)基于mfc的opengl應(yīng)用程開發(fā)環(huán)境: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放到你創(chuàng)建的應(yīng)用程序的運(yùn)行目錄下2, 創(chuàng)建一個(gè)mfc sdi應(yīng)用程序,在項(xiàng)目屬性中加入所需要鏈接的庫(kù)文件1, 在stdafx.h中加入下列語(yǔ)句:/openglheaders#include#include#include#include2, 打開classwizard,選擇ccy457openglview類,為下述消息加入消息處理函數(shù):wm_create (for oncreate), wm_destroy (for ondestroy), wm_size (for onsize), wm_erasebackground (for o

18、nerasebkground).3,在窗口創(chuàng)建之前我們必須設(shè)置窗口風(fēng)格包含ws_clipchildren和 ws_clipsiblings,從而避免opengl繪制到其他窗口中去。這些應(yīng)該放在precreatewindow()中。boolccy457openglview:precreatewindow(createstruct&cs)/todo:modifythewindowclassorstylesherebymodifying/thecreatestructcs/anopenglwindowmustbecreatedwiththefollowingflagscs.style|=ws_cli

19、psiblings|ws_clipchildren;returncview:precreatewindow(cs);4,在ccy457openglview.h中加入如下語(yǔ)句:hglrcm_hrc;/renderingcontextcdc*m_pdc;/devicecontextboolinitializeopengl();/initializeopenglboolsetuppixelformat();/setupthepixelformatvoidrenderscene();/renderthescene5,在oncreate中我們將通過(guò)建立像素格式和繪制上下文來(lái)初始化opengl. 在ini

20、tializeopengl()中會(huì)創(chuàng)建一個(gè)設(shè)備上下文(dc),為這個(gè)dc選擇一個(gè)像素格式,創(chuàng)建和這個(gè)dc相關(guān)的繪制上下文(rc),然后選擇這個(gè)rc.這個(gè)函數(shù)會(huì)調(diào)用setuppixelformat()來(lái)建立像素格式。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()中一般用來(lái)設(shè)置視口和視錐,因?yàn)檫@些是和窗口大小相關(guān)的?;静僮靼ㄔO(shè)置視口,選擇投影矩陣,設(shè)置模型視圖矩陣。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,在繪制場(chǎng)景時(shí),一般包括如下步驟:1)清空緩存。2)繪制場(chǎng)景。3)flush掉渲染流水線。4)若設(shè)置了雙緩沖,則交換前后臺(tái)緩沖區(qū)。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()/第一個(gè)玩具嘛,先空著,后面慢慢填8,試試改變窗口的大小,你會(huì)看到很嚴(yán)重的閃爍,并且關(guān)閉程序后會(huì)報(bào)告內(nèi)存泄露,因此我們這就來(lái)解決這兩個(gè)問(wèn)題吧。發(fā)生閃爍的原因是windows先繪制背景,然后再是opengl繪制,因?yàn)槲覀円呀?jīng)讓opengl負(fù)責(zé)清空背景色,因

29、此我們不需要windows去清空背景了boolccy457openglview:onerasebkgnd(cdc*pdc)/tellwindowsnottoerasethebackgroundreturntrue;內(nèi)存泄露的原因是我們?cè)趕etuppixelformat()中使用了new運(yùn)算符來(lái)為cclientdc對(duì)象分配內(nèi)存,因此需要顯示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;光榮在于平淡,艱巨因?yàn)槁L(zhǎng)基于mfc的opengl編程part 3 drawing simple 2d shapes 剪裁區(qū)域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.頂點(diǎn)和基本圖元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,并且加入四個(gè)菜單項(xiàng)及其對(duì)應(yīng)的事件處理程序。voidccy457openglview:onshapespoint()/畫點(diǎn)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()函數(shù),因?yàn)楸疚闹兄焕L制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()/繪制函數(shù)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();光榮在于平淡,艱巨因?yàn)槁L(zhǎng)基于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并且在構(gòu)造函數(shù)中初始化cc

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論