VB6.0使用DIRECTX8 3d編程資料[特選借鑒]_第1頁
VB6.0使用DIRECTX8 3d編程資料[特選借鑒]_第2頁
VB6.0使用DIRECTX8 3d編程資料[特選借鑒]_第3頁
VB6.0使用DIRECTX8 3d編程資料[特選借鑒]_第4頁
已閱讀5頁,還剩28頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、你是否曾驚嘆VB的簡(jiǎn)單易用與開發(fā)的速度之快?你是否對(duì)編寫二維的游戲早已感到厭倦和無聊?你是否想有朝一日自己也能寫出魔獸、CS、起義、古墓麗影之類的3D游戲?你是否想過利用D3D甚至可以把你去過的旅游景點(diǎn)或你的家這些場(chǎng)景在電腦中構(gòu)造出來,然后加入自己的創(chuàng)意、故事和情節(jié)搞點(diǎn)什么東西?你是否想過利用D3D可以構(gòu)造你的島嶼、你的天堂,把你在現(xiàn)實(shí)世界中無法實(shí)現(xiàn)的夢(mèng)想在虛擬世界中實(shí)現(xiàn),或者預(yù)先把你的夢(mèng)想可視化以便更快速地實(shí)現(xiàn)它?那么還猶豫什么?!讓我們開始一趟激動(dòng)人心的D3D學(xué)習(xí)之旅吧! 首先選擇一下各個(gè)工具的版本。VB我選擇VB6,一是它速度比較快,二是我對(duì)它比較熟悉。然后由于VB6只含有DX7和DX8

2、的函數(shù)庫,因此對(duì)于DX版本的選擇嘛,我就選用它所支持的最高版本DX8啦。常用的3D建模軟件有3ds max和Maya,我個(gè)人覺得Maya的性能較好,而Maya導(dǎo)出X文件的插件能用于D3D中的最高版本是Maya 6(Maya 7雖然導(dǎo)出的X文件能在mesh viewer里查看,但是導(dǎo)入D3D后不正常),因此我選用Maya 6. 學(xué)習(xí)D3D,在軟件方面需要一些類的知識(shí),在數(shù)學(xué)方面需要一些向量代數(shù)、空間解析幾何、矩陣的知識(shí);對(duì)缺乏這些知識(shí)的初學(xué)者來說學(xué)習(xí)起來可能比較困難,如果實(shí)在看不懂,建議還是自己先補(bǔ)一下基礎(chǔ)知識(shí)吧。不過請(qǐng)千萬別灰心,想一想,能夠隨自己的意愿來設(shè)計(jì)3D游戲是多么激動(dòng)人心的事情??!請(qǐng)

3、用這個(gè)目標(biāo)來激勵(lì)自己吧! 下面讓我們通過一個(gè)最簡(jiǎn)單的D3D的程序來開始講解一些基本的DX和D3D概念:請(qǐng)大家首先動(dòng)手完成以下步驟:1. 新建一個(gè)標(biāo)準(zhǔn)的exe工程;2. 在窗體上放一個(gè)Timer控件,設(shè)置Enabled=False,Interval=200;3. 選擇菜單“工程(Project) - 引用(Reference)”,然后找到“DirectX 8 for Visual Basic Type Library”打勾,確定;4. 在窗體內(nèi)輸入如下代碼: 1. Option Explicit2.3. Private MyDirectX8 As New DirectX84. Private M

4、yDirect3D8 As Direct3D85. Private MyDirect3DDevice8 As Direct3DDevice86.7. Private Sub Form_Load()8. Dim DispMode As D3DDISPLAYMODE9. Dim d3dpp As D3DPRESENT_PARAMETERS10.11. Set MyDirect3D8 = MyDirectX8.Direct3DCreate12. MyDirect3D8.GetAdapterDisplayMode D3DADAPTER_DEFAULT, DispMode13.14. With d3dp

5、p15. .Windowed = 116. .SwapEffect = D3DSWAPEFFECT_COPY_VSYNC17. .BackBufferFormat = DispMode.Format18. End With19.20. Set MyDirect3DDevice8 = MyDirect3D8.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Me.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dpp)21. Timer1.Enabled = True22. End Sub23.24. Pr

6、ivate Sub Timer1_Timer()25. Const WORLD_COLOR As Long = &HFF00&26. 27. With MyDirect3DDevice828. .Clear 0, ByVal 0, D3DCLEAR_TARGET, WORLD_COLOR, 1#, 029. .BeginScene30. .EndScene31. .Present ByVal 0, ByVal 0, 0, ByVal 032. End With33. End Sub運(yùn)行。如果看見一個(gè)綠色的窗體的話,那么恭喜你,你已經(jīng)編寫了一個(gè)真正的D3D程序! 這個(gè)程序很簡(jiǎn)單,但它的確是一個(gè)完

7、整的D3D程序。 下面來分析程序: 先看模塊的聲明部分,聲明了三個(gè)模塊級(jí)變量,為了在以后的程序中能夠一眼看出變量類型,我的命名采用“My+變量類型”的原則。第一個(gè)變量在聲明類型的同時(shí)還創(chuàng)建了一個(gè)DirectX8對(duì)象(由New關(guān)鍵字可見),而后兩個(gè)變量?jī)H僅是聲明類型而已,并沒有新的對(duì)象被創(chuàng)建。到了Form_Load事件里的這一句:Set MyDirect3D8 = MyDirectX8.Direct3DCreate才創(chuàng)建了一個(gè)Direct3D8對(duì)象并讓MyDirect3D8指向它,到這一句:Set MyDirect3DDevice8 = MyDirect3D8.CreateDevice(D3DA

8、DAPTER_DEFAULT, D3DDEVTYPE_HAL, Me.hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dpp)才創(chuàng)建了一個(gè)Direct3DDevice8對(duì)象并讓 MyDirect3DDevice8 指向它。 讓我們來再看一下這三句來理順這三個(gè)對(duì)象之間的關(guān)系:Private MyDirectX8 As New DirectX8 Set MyDirect3D8 = MyDirectX8.Direct3DCreate Set MyDirect3DDevice8 = MyDirect3D8.CreateDevice(.)首先創(chuàng)建MyDirect

9、X8對(duì)象,然后由它來創(chuàng)建 MyDirect3D8 對(duì)象,最后由 MyDirect3D8 來創(chuàng)建 MyDirect3DDevice8 對(duì)象,簡(jiǎn)寫為:DirectX8 - Direct3D - Direct3DDevice顯然從左往右是整體和部分之間的關(guān)系(例如 Direct3D 是 DirectX8 的一個(gè)組件等等),這樣夠清楚了吧?為了更深刻地理解VB里的對(duì)象、對(duì)象指針、Set、New這些概念(語句),我們來看看VC+里是怎么寫的: 1. LPDIRECT3D8 MyDirect3D8= NULL; 2. LPDIRECT3DDEVICE8 MyDirect3DDevice8 = NULL;

10、3.4. MyDirect3D8 = Direct3DCreate8( D3D_SDK_VERSION );5. MyDirect3D8-CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &MyDirect3DDevice8)其中變量類型前綴LP是 Long Pointer(32位指針)的意思,LPDIRECT3D8 MyDirect3D8 等價(jià)于 DIRECT3D8* MyDirect3D8,MyDirect3D8是指向 DIRECT3D8

11、對(duì)象的指針類型。首先定義了兩個(gè)指針 MyDirect3D8、MyDirect3DDevice8 分別指向 DIRECT3D8 和 DIRECT3DDEVICE8 對(duì)象,然后用函數(shù) Direct3DCreate8 創(chuàng)建 DIRECT3D8 對(duì)象(當(dāng)然該函數(shù)返回的是指向該對(duì)象的指針),賦值給MyDirect3D8,然后用 MyDirect3D8 指向的 Direct3D8 對(duì)象的 CreateDevice 方法創(chuàng)建 Direct3DDevice8 對(duì)象,并將其地址以傳址方式傳給 MyDirect3DDevice8。與VB程序最大的不同是沒有DirectX8對(duì)象,不像VB那樣用 MyDirectX8

12、.Direct3DCreate 來創(chuàng)建Direct3D8對(duì)象,而直接使用庫函數(shù)Direct3DCreate8來創(chuàng)建(而且多了個(gè)參數(shù)D3D_SDK_VERSION)。第二個(gè)不同是VB里 MyDirect3DDevice8 的賦值是通過函數(shù)的返回值的方式,而VC里則是通過參數(shù)傳址的方式賦值。 把對(duì)象的創(chuàng)建和它們之間的關(guān)系弄清楚后,讓我們來逐個(gè)看看各個(gè)對(duì)象吧。DirectX8:DirectX8 類是使用微軟 DX 技術(shù)的起點(diǎn),編寫任何DX程序都由它開始,在程序中,我們用它的 Direct3DCreate 方法來創(chuàng)建一個(gè) Direct3D 對(duì)象,其實(shí)它還有DirectInputCreate、Direc

13、tMusicComposerCreate、DirectPlayClientCreate、DirectSoundCreate、DirectXFileCreate等方法來創(chuàng)建其他的DX對(duì)象。Direct3D8:這個(gè)類用來創(chuàng)建 Direct3D 對(duì)象和設(shè)置環(huán)境,程序中用了它的CreateDevice方法來創(chuàng)建設(shè)備,用了它的 GetAdapterDisplayMode 方法來獲取顯卡的顯示模式。Direct3DDevice8:這是D3D中最重要的類了,鼠標(biāo)、紋理、圖元、材質(zhì)、燈光、緩沖區(qū)、陰影、渲染場(chǎng)景都由它來完成,前面的兩個(gè)類只是初始化時(shí)用到一下而已,后面的程序中全部都是 Direct3DDevic

14、e8 的方法,例如:SetCursorPosition、ShowCursor、CreateIndexBuffer、CreateTexture、CreateVertexBuffer、SetRenderState、SetTransform、SetIndices、GetLight、SetMaterial、Present、Reset、DrawPrimitive、BeginScene、EndScene、Clear 從名字就可大致看出它們的功能。下面讓我們來看一下Form_Load事件里的兩個(gè)變量類型:D3DDISPLAYMODE 和 D3DPRESENT_PARAMETERS,它們都是結(jié)構(gòu)體類型。顧名思

15、義,D3DDISPLAYMODE 是用來描述顯示模式的:Type D3DDISPLAYMODE Format As CONST_D3DFORMAT Width As Long Height As Long RefreshRate As LongEnd Type后面三個(gè)參數(shù)很好理解,和Windows的顯示屬性對(duì)話框里的是一致的,可是第一個(gè)參數(shù)是什么呢?關(guān)鍵在于 CONST_D3DFORMAT 是什么。CONST_D3DFORMAT 是這樣的枚舉類型:Enum CONST_D3DFORMAT D3DFMT_UNKNOWN = 0 D3DFMT_R8G8B8 =20 (&H14) D3DFMT_A8

16、R8G8B8 =21 (&H15) D3DFMT_X8R8G8B8 =22 (&H16) D3DFMT_R5G6B5 =23 (&H17) End Enum為簡(jiǎn)明起見,以上只列出了幾種常見的取值,理解概念即可。D3DFMT_UNKNOWN:顧名思義,未知格式。D3DFMT_R8G8B8:24位RGB 格式,R、G、B分別占8位。D3DFMT_A8R8G8B8:32位ARGB格式,A表示alpha.D3DFMT_X8R8G8B8:32位RGB格式,X8是保留的8位,未來可以根據(jù)情況靈活地分配給R、G、B。D3DFMT_R5G6B5:16位RGB格式。5+6+5=16.好,這樣結(jié)構(gòu)體 D3DDIS

17、PLAYMODE 我們就理解了,里面存放的是屏幕的寬、高、刷新率和顏色空間。下來我們?cè)賮砜唇Y(jié)構(gòu)體D3DPRESENT_PARAMETERS。這里面包含了一個(gè)重要的單詞(概念):Present。它既不是“禮物”,也不是“現(xiàn)在的”,也不是“介紹,引見”,而是“呈現(xiàn),展現(xiàn)”的意思。我們?cè)谟?jì)算機(jī)構(gòu)建了3D場(chǎng)景后,最終要在顯示器上顯示出來,這個(gè)3D變2D,矢量變點(diǎn)陣的過程就叫做:Present,展現(xiàn)。好了,下面看看它的定義:Type D3DPRESENT_PARAMETERS AutoDepthStencilFormat As CONST_D3DFORMAT BackBufferCount As Lon

18、g BackBufferFormat As CONST_D3DFORMAT (*) BackBufferHeight As Long BackBufferWidth As Long EnableAutoDepthStencil As Long Flags As Long FullScreen_PresentationInterval As Long FullScreen_RefreshRateInHz As Long hDeviceWindow As Long MultiSampleType As CONST_D3DMULTISAMPLE_TYPE SwapEffect As CONST_D3

19、DSWAPEFFECT (*) Windowed As Long (*)End Type有四個(gè)成員變量均含前綴 BackBuffer,意思是后臺(tái)緩存或屏幕緩存,是為了解決繪圖時(shí)的閃爍而引進(jìn)的,先在緩存區(qū)繪好,再一次復(fù)制到顯存來顯示。為了更大限度地緩解“卡”的現(xiàn)象,還可設(shè)置多個(gè)緩存,組成“緩存鏈表”,緩存數(shù)由BackBufferCount指定。上面三個(gè)打了星號(hào)的成員變量 BackBufferFormat、SwapEffect 和 Windowed 是本程序中用到的:BackBufferFormat(1/3) 是 CONST_D3DFORMAT 類型的,存放顏色空間信息,上面已經(jīng)說過了,BackB

20、uffer 剛剛才解釋過,把它們組合在一起自然就是后天緩沖區(qū)的顏色空間格式咯。SwapEffect(2/3) 有如下四種取值:Enum CONST_D3DSWAPEFFECT D3DSWAPEFFECT_DISCARD = 1 D3DSWAPEFFECT_FLIP = 2 D3DSWAPEFFECT_COPY = 3 D3DSWAPEFFECT_COPY_VSYNC = 4End Enum其含義我還沒弄懂,先照抄 SDK 的原文:D3DSWAPEFFECT_DISCARD When a swap chain is created with a swap effect of D3DSWAPEFF

21、ECT_FLIP, D3DSWAPEFFECT_COPY or D3DSWAPEFFECT_COPY_VSYNC, the runtime will guarantee that a Direct3DDevice8.Present operation will not affect the content of any of the back buffers. Unfortunately, meeting this guarantee can involve substantial video memory or processing overheads, especially when im

22、plementing flip semantics for a windowed swap chain or copy semantics for a full-screen swap chain. An application may use the D3DSWAPEFFECT_DISCARD swap effect to avoid these overheads and to enable the display driver to select the most efficient presentation technique for the swap chain. This is a

23、lso the only swap effect that may be used when specifying a value other than D3DMULTISAMPLE_NONE for the MultiSampleType member of D3DPRESENT_PARAMETERS. As with a swap chain that uses D3DSWAPEFFECT_FLIP, a swap chain that uses D3DSWAPEFFECT_DISCARD might include more than one back buffer, any of wh

24、ich may be accessed using Direct3DDevice8.GetBackBuffer or Direct3DSwapChain8.GetBackBuffer. The swap chain is best envisaged as a queue in which 0 always indexes the back buffer that will be displayed by the next Present operation and from which buffers are discarded once they have been displayed.

25、An application that uses this swap effect cannot make any assumptions about the contents of a discarded back buffer and should therefore update an entire back buffer before invoking a Present operation that would display it. Although this is not enforced, the debug version of the runtime will overwr

26、ite the contents of discarded back buffers with random data to enable developers to verify that their applications are updating the entire back buffer surfaces correctly. For a full-screen swap chain, the presentation rate is determined by the value assigned to the FullScreen_PresentationInterval me

27、mber of the D3DPRESENT_PARAMETERS structure when the device or swap chain is created. Unless this value is D3DPRESENT_INTERVAL_IMMEDIATE, the presentation will be synchronized with the vertical sync of the monitor. For a windowed swap chain, the presentation is implemented by means of copy operation

28、s and always occurs immediately. D3DSWAPEFFECT_FLIP The swap chain may comprise multiple back buffers and is best envisaged as a circular queue that includes the front buffer. Within this queue, the back buffers are always numbered sequentially from 0 to (N -1), where N is the number of back buffers

29、, so that 0 denotes the least recently presented buffer. When Present is invoked, the queue is rotated so that the front buffer becomes back buffer (N - 1), while back buffer 0 becomes the new front buffer. For a full-screen swap chain, the presentation rate is determined by the value assigned to th

30、e FullScreen_PresentationInterval field of the D3DPRESENT_PARAMETERS structure when the device or swap chain is created. Unless this value is D3DPRESENT_INTERVAL_IMMEDIATE, the presentation will be synchronized with the vertical sync of the monitor. For a windowed swap chain, the flipping is impleme

31、nted by means of copy operations and the presentation always occurs immediately. D3DSWAPEFFECT_COPY This swap effect may be specified only for a swap chain comprising a single back buffer. Whether the swap chain is windowed or full-screen, the runtime will guarantee the semantics implied by a copy-b

32、ased Present operation, namely that the operation leaves the content of the back buffer unchanged, instead of replacing it with the content of the front buffer as a flip-based Present operation would. For a windowed swap chain, a Present operation causes the back buffer content to be copied to the c

33、lient area of the target window immediately. No attempt is made to synchronize the copy with the vertical retrace period of the display adapter, so tearing effects may be observed. Windowed applications may wish to use D3DSWAPEFFECT_COPY_VSYNC instead to eliminate, or at least minimize, such tearing

34、 effects. For a full-screen swap chain, the runtime uses a combination of flip operations and copy operations, supported if necessary by hidden back buffers, to accomplish the Present operation. Accordingly, the presentation is synchronized with the display adapters vertical retrace and its rate is

35、constrained by the chosen presentation interval. A swap chain specified with the D3DPRESENT_INTERVAL_IMMEDIATE flag is the only exception. (Refer to the description of the FullScreen_PresentationInterval member of the D3DPRESENT_PARAMETERS structure.) In this case, a Present operation copies the bac

36、k buffer content directly to the front buffer without waiting for the vertical retrace. D3DSWAPEFFECT_COPY_VSYNC Like D3DSWAPEFFECT_COPY, this swap effect may only be used with a swap chain comprising a single back buffer and guarantees that a Present operation applied to the swap chain will exhibit

37、 copy semantics, as described above for D3DSWAPEFFECT_COPY. For a windowed swap chain, a Present operation causes the back buffer content to be copied to the client area of the target window. The runtime will attempt to eliminate tearing effects by avoiding the copy operation while the adapter is sc

38、anning within the destination rectangle on the display. It will also perform at most one such copy operation during the adapters refresh period and thus limit the presentation frequency. Note, however, that if the adapter does not support the ability to report the raster status, the swap chain will

39、behave as though it had been created with the D3DSWAPEFFECT_COPY swap effect. (Refer to the description of the D3DCAPS_READ_SCANLINE flag value for the Caps member of D3DCAPS8.) For a full-screen swap chain, D3DSWAPEFFECT_COPY_VSYNC is identical to D3DSWAPEFFECT_COPY, except that the D3DPRESENT_INTE

40、RVAL_IMMEDIATE flag is meaningless when used in conjunction with D3DSWAPEFFECT_COPY_VSYNC. (Refer to the description of the FullScreen_PresentationInterval member of the D3DPRESENT_PARAMETERS structure.) Windowed(3/3) 取TRUE(非零)時(shí),以窗體模式運(yùn)行,取FALSE(零)時(shí),以全屏模式運(yùn)行?!暗纫幌拢瑸槭裁次以O(shè)置全屏方式運(yùn)行會(huì)出錯(cuò)呢?”呵呵,那是因?yàn)闆]有設(shè)置后臺(tái)緩沖區(qū)的尺寸大小

41、,只需在.BackBufferFormat = DispMode.Format的后面加兩句: .BackBufferWidth = DispMode.Width .BackBufferHeight = DispMode.Height就可以了。下面看這一句:MyDirect3D8.GetAdapterDisplayMode D3DADAPTER_DEFAULT, DispModeGetAdapterDisplayMode方法,顧名思義就是獲取顯示適配器的顯示模式,第一個(gè)參數(shù)是要查詢的顯卡序號(hào),對(duì)于絕大多數(shù)人來說只有一個(gè)顯卡,令其為0(D3DADAPTER_DEFAULT)就可以了。GetAdap

42、terDisplayMode 方法將查詢到的顯示模式(寬、高、刷新率和顏色模式)放入到結(jié)構(gòu)體DispMode中。我們可以在這一句的后面插入以下三條語句來看看結(jié)果是不是和我們的顯示設(shè)置相一致:Debug.Print DispMode.WidthDebug.Print DispMode.HeightDebug.Print DispMode.RefreshRate接下來到這句:Set MyDirect3DDevice8 = MyDirect3D8.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, Me.hWnd, D3DCREATE_SOFTWARE_

43、VERTEXPROCESSING, d3dpp)上面已經(jīng)說過,這是創(chuàng)建D3D設(shè)備對(duì)象,現(xiàn)在主要解釋一下 CreateDevice 方法的各個(gè)參數(shù):第一個(gè)參數(shù)和 GetAdapterDisplayMode 的第一個(gè)參數(shù)一樣,第五個(gè)(最后一個(gè))參數(shù)上面也已經(jīng)解釋過,就不說了,以下主要說明中間的三個(gè)參數(shù):第二個(gè)參數(shù)是如下的枚舉類型:Enum CONST_D3DDEVTYPE D3DDEVTYPE_HAL = 1 D3DDEVTYPE_REF = 2 D3DDEVTYPE_SW = 3End Enum有三種取值:HAL是硬件加速,REF是軟件模擬,SW是軟件插件。在我(VBProFan)的電腦里,HA

44、L正常,REF黑屏,SW直接出錯(cuò)第三個(gè)參數(shù)是要?jiǎng)?chuàng)建的D3D設(shè)備的窗口(控件)句柄,也就是說后面的3D場(chǎng)景要在哪里展示。本程序中是窗體Form,當(dāng)然也可以是PictureBox,CommandButton,OptionButton,CheckBox,大家不妨在窗體上放上這些控件,然后改一下這個(gè)參數(shù)看看。不過這些控件只能在窗體模式下運(yùn)行,不能全屏。第四個(gè)參數(shù)是行為標(biāo)志,由“或”運(yùn)算組合而成,類似MsgBox的buttons參數(shù)Enum CONST_D3DCREATEFLAGS D3DCREATE_FPU_PRESERVE = 2 D3DCREATE_MULTITHREADED = 4 D3DCRE

45、ATE_PUREDEVICE = 16 (&H10) D3DCREATE_SOFTWARE_VERTEXPROCESSING = 32 (&H20) D3DCREATE_HARDWARE_VERTEXPROCESSING = 64 (&H40) D3DCREATE_MIXED_VERTEXPROCESSING = 128 (&H80)End Enum由名字可大致看出其含義。至此,F(xiàn)orm_Load事件里的代碼基本講解完畢,除了SwapEffect其含義我還沒弄懂,Timer1.Enabled = True 沒講以外。待續(xù)。繼續(xù)講解頂樓的代碼:初始化完畢后,用Timer1.Enabled = T

46、rue來啟動(dòng)時(shí)鐘。這個(gè)時(shí)鐘是用來干什么的呢?用來渲染場(chǎng)景的。我們知道,在2D的編程中,當(dāng) WM_PAINT 消息(Form_Paint)發(fā)生時(shí),系統(tǒng)將會(huì)重繪大多數(shù)界面,用戶數(shù)據(jù)部分由用戶在事件中寫代碼來自己重繪。但 3D 編程就不用了,需要定時(shí)重繪(渲染)整個(gè)場(chǎng)景,當(dāng)然你也可以只重繪發(fā)生變化的部分,但要計(jì)算哪個(gè)矩形區(qū)域發(fā)生變化非常麻煩,還不如重繪整個(gè)場(chǎng)景算了。以下的 Timer 事件就是用來渲染場(chǎng)景的: 1.2. Private Sub Timer1_Timer()3. Const WORLD_COLOR As Long = &HFF00&4.5. With MyDirect3DDevice8

47、6. .Clear 0, ByVal 0, D3DCLEAR_TARGET, WORLD_COLOR, 1#, 07. .BeginScene8. .EndScene9. .Present ByVal 0, ByVal 0, 0, ByVal 010. End With11. End Sub先用 Clear 清除場(chǎng)景,然后在 .BeginScene 和 .EndScene 之間渲染(繪制)場(chǎng)景,現(xiàn)在場(chǎng)景里沒有任何東西,所以這之間暫時(shí)沒有代碼,但這片“空地”就是以后我們需要大做文章的地方。最后用 .Present 來展現(xiàn)整個(gè)場(chǎng)景。下面解釋一下 .Clear 的各個(gè)參數(shù):object.Clear(

48、 _ Count As Long, _ ClearD3DRect As Any, Flags As CONST_D3DCLEARFLAGS, _ Color As Long, _ Z As Single, _ Stencil As Long)Count Number of rectangles in the array at ClearD3DRect. If you set ClearD3DRect to ByVal 0, this parameter must be set to 0. ClearD3DRect First element of an array of D3DRECT typ

49、es that describe the rectangles to clear. Set a rectangle to the dimensions of the rendering target to clear the entire surface. Each rectangle uses screen coordinates that correspond to points on the render target surface. Coordinates are clipped to the bounds of the viewport rectangle. This parame

50、ter can be set to ByVal 0 to indicate that the entire viewport rectangle is to be cleared. (該程序中就是用 ByVal 0 來清除整個(gè)場(chǎng)景)Flags A combination of the flags defined by the CONST_D3DCLEARFLAGS enumeration that indicate which surfaces should be cleared. Note that at least one flag must be used. Color A 32-bit

51、 ARGB color value to which the render target surface is cleared. Z New z value that this method stores in the depth buffer. This parameter can be in the range from 0.0 through 1.0 (for z-based or w-based depth buffers). A value of 0.0 represents the nearest distance to the viewer, and 1.0 the farthe

52、st distance. (我們這里綠色背景放在最遠(yuǎn)端、最背景的位置,所以這里取1)Stencil Integer value to store in each stencil-buffer entry. This parameter can be in the range from 0 through 2n1, where n is the bit depth of the stencil buffer. 注意,Color 的格式是 &HAARRGGBB,而 VB 里的格式則是 &H00BBGGRR,順序剛好相反(所以vbRed、vbGreen 之類的常數(shù)就別想用了),因此這里的 &HFF0

53、0& 就是綠色咯 還有注意 Long 后綴 &,如果缺少這個(gè)就變?yōu)辄S色了,也就是 &H00FFFF00&. 最后解釋一下 .Present 的各個(gè)參數(shù):object.Present( _ SourceRect As Any, _ DestRect As Any, _ DestWindowOverride As Long, _ DirtyRegion As Any)SourceRect Value that must be ByVal 0 unless the swap chain was created with D3DSWAPEFFECT_COPY or D3DSWAPEFFECT_COPY

54、_VSYNC. SourceRect is a type containing the source rectangle. If ByVal 0, the entire source surface is presented. If the rectangle exceeds the source surface, the rectangle is clipped to the source surface. DestRect Value that must be ByVal 0 unless the swap chain was created with D3DSWAPEFFECT_COPY

55、 or D3DSWAPEFFECT_COPY_VSYNC. DestRect is a type containing the destination rectangle, in window client coordinates. If ByVal 0, the entire client area is filled. If the rectangle exceeds the destination client area, the rectangle is clipped to the destination client area. DestWindowOverride Destina

56、tion window whose client area is taken as the target for this presentation. If this value is 0 (zero), then the hWndDeviceWindow member of D3DPRESENT_PARAMETERS is taken. DirtyRegion This parameter is not used and should be set to ByVal 0.有時(shí)候我們需要更高頻率的渲染,而 Timer 的頻率顯得不夠,于是可以改寫為以下架構(gòu):Option ExplicitPrivate MyDirectX8 As New DirectX8Private MyDir

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(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)論