• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天行健 君子當(dāng)自強(qiáng)而不息

            初始化Direct3D(1)

            1.1 Direct3D概述

            Direct3D是一種低層圖形API,它能讓我們利用3D硬件加速來(lái)渲染3D世界。我們可以把Direct3D看作是應(yīng)用程序和圖形設(shè)備之間的中介。例如通知圖形設(shè)備清空屏幕,應(yīng)用程序?qū)⒄{(diào)用Direct3D的IDirect3DDevice9::Clear方法。圖1.1顯示了應(yīng)用程序、Direct3D和圖形設(shè)備之間的關(guān)系。

            圖1.1中Direct3D所表示的是Direct3D中已定義的,供程序員使用的Direct3D接口和函數(shù)的集合。這些接口和函數(shù)代表了當(dāng)前版本的Direct3D所支持的全部特性。注意:僅僅因?yàn)镈irect3D支持某種特性,并不意味著你所使用的圖形硬件(顯卡)也能支持它。

            如圖1.1所示,在Direct3D和圖形設(shè)備之間有一層中介——叫做硬件抽象層(HAL,Hardware Abstraction Layer)。Direct3D不能直接作用于圖形設(shè)備,因?yàn)楝F(xiàn)在市面上的顯卡種類實(shí)在是太多了并且每種顯卡都有不同的性能和處理事件的方式。例如,兩種不同的顯卡實(shí)現(xiàn)清屏的方式也可能是不同的。因此,Direct3D要求設(shè)備制造商實(shí)現(xiàn)HAL。HAL是一組指示設(shè)備執(zhí)行某種操作的特殊設(shè)備代碼的集合。用這種方法,Direct3D避免了必須去了解某個(gè)設(shè)備的特殊細(xì)節(jié),使它能夠獨(dú)立于硬件設(shè)備。

            設(shè)備制造商在HAL中實(shí)現(xiàn)他們的產(chǎn)品所支持的所有特性。HAL將不會(huì)實(shí)現(xiàn)那些Direct3D支持但硬件產(chǎn)品不支持的特性。調(diào)用一個(gè)HAL中沒(méi)有實(shí)現(xiàn)的Direct3D的函數(shù)將會(huì)出錯(cuò),除非它是頂點(diǎn)處理操作,因?yàn)檫@個(gè)功能可以由軟件模擬來(lái)實(shí)現(xiàn)。因此當(dāng)使用某些僅由市面上少數(shù)顯卡所支持的高級(jí)特性時(shí),必須檢測(cè)一下設(shè)備是否支持。

             

            1.1.1 REF設(shè)備

            你也許想把一些你的設(shè)備不支持的Direct3D函數(shù)寫(xiě)入程序。為了達(dá)到這個(gè)目的,Direct3D提供了REF設(shè)備,它用軟件模擬了所有的Direct3D API。這允許你寫(xiě)并測(cè)試那些你的顯卡不支持的Direct3D特性的代碼。懂得REF設(shè)備僅僅用于開(kāi)發(fā)階段,這是很重要的。它只會(huì)和DirectX SDK一起被裝載,而不會(huì)發(fā)布給最終用戶。 另外,REF設(shè)備實(shí)在是太慢了,除了測(cè)試以外它沒(méi)有任何利用價(jià)值。

             

            1.1.2 D3DDEVTYPE

            在代碼中,我們用D3DDEVTYPE_HAL來(lái)定義HAL設(shè)備,它是D3DDEVTYPE枚舉類型的一個(gè)成員。同樣的,REF設(shè)備則由D3DDEVTYPE_REF來(lái)定義,它也屬于D3DDEVTYPE枚舉類型。記住這些類型很重要,因?yàn)樵趧?chuàng)建設(shè)備的時(shí)候我們需要指定我們將要使用的類型。

            Defines device types.

            typedef enum D3DDEVTYPE
            {
            D3DDEVTYPE_HAL = 1,
            D3DDEVTYPE_NULLREF = 4,
            D3DDEVTYPE_REF = 2,
            D3DDEVTYPE_SW = 3,
            D3DDEVTYPE_FORCE_DWORD = 0xffffffff,
            } D3DDEVTYPE, *LPD3DDEVTYPE;

            Constants

            D3DDEVTYPE_HAL
            Hardware rasterization. Shading is done with software, hardware, or mixed transform and lighting.
            D3DDEVTYPE_NULLREF
            Initialize Direct3D on a computer that has neither hardware nor reference rasterization available, and enable resources for 3D content creation. See Remarks.
            D3DDEVTYPE_REF
            Direct3D features are implemented in software; however, the reference rasterizer does make use of special CPU instructions whenever it can.
            D3DDEVTYPE_SW
            A pluggable software device that has been registered with IDirect3D9::RegisterSoftwareDevice.
            D3DDEVTYPE_FORCE_DWORD
            Forces this enumeration to compile to 32 bits in size. Without this value, some compilers would allow this enumeration to compile to a size other than 32 bits. This value is not used.

            Remarks

            All methods of the IDirect3D9 interface that take a D3DDEVTYPE device type will fail if D3DDEVTYPE_NULLREF is specified. To use these methods, substitute D3DDEVTYPE_REF in the method call.

            A D3DDEVTYPE_REF device should be created in D3DPOOL_SCRATCH memory, unless vertex and index buffers are required. To support vertex and index buffers, create the device in D3DPOOL_SYSTEMMEM memory.

            If D3dref9.dll is installed, Direct3D will use the reference rasterizer to create a D3DDEVTYPE_REF device type, even if D3DDEVTYPE_NULLREF is specified. If D3dref9.dll is not available and D3DDEVTYPE_NULLREF is specified, Direct3D will neither render nor present the scene.

            1.2 COM

            組件對(duì)象模型(COM, Component Object Model)是一種能使DirectX獨(dú)立于編程語(yǔ)言和具有向下兼容性的技術(shù)。我們通常把COM對(duì)象作為一個(gè)接口,你可以把它當(dāng)作達(dá)到某種目的的C++類來(lái)使用它。當(dāng)使用C++寫(xiě)DirectX程序的時(shí)候,COM的大部分細(xì)節(jié)對(duì)我們來(lái)說(shuō)是透明。但是有一件事,我們必須知道,那就是我們通過(guò)某個(gè)特殊的COM接口的函數(shù)或指針獲得了另一個(gè)COM接口指針,而不是通過(guò)C++的新關(guān)鍵字來(lái)創(chuàng)建它。當(dāng)我們使用完某個(gè)接口后,調(diào)用它的Release方法比直接Delete它更好。COM對(duì)象具有它們自己的內(nèi)存管理。

            對(duì)COM來(lái)說(shuō)還有很多細(xì)節(jié)可以了解,但是掌握這些細(xì)節(jié)對(duì)于我們有效的使用DirectX不是必須的。

            注意:COM接口都具有前綴大寫(xiě)字母“I”,例如表示一個(gè)表面的COM接口叫做IDirect3DSurface9。

             

            1.3 一些準(zhǔn)備工作

            Direct3D的初始化過(guò)程要求我們對(duì)圖形學(xué)基礎(chǔ)知識(shí)和Direct3D類型有一定了解。這里將介紹這些知識(shí)和類型,以確保以后能把焦點(diǎn)集中在討論Direct3D的初始化上。

            1.3.1 表面

            表面是一個(gè)像素點(diǎn)陣,在Direct3D中主要用來(lái)存儲(chǔ)2D圖形數(shù)據(jù)。圖1.2指明了表面的一些成分。由圖可以看出表面數(shù)據(jù)就像一個(gè)矩陣,像素?cái)?shù)據(jù)實(shí)際上存儲(chǔ)在線性數(shù)組里面。

             

            表面的Width和Height是按像素計(jì)算的。Pitch以字節(jié)為單位。而且Pitch有可能比Width大且依賴于低層硬件,所以不能單純的認(rèn)為Pitch = Width * sizeof (pixelFormat)。

            在代碼中,我們可以使用IDirect3DSurface9接口來(lái)描述表面。這個(gè)接口提供若干方法來(lái)直接讀寫(xiě)表面數(shù)據(jù)并且還有一個(gè)方法用來(lái)返回表面息。IDirect3DSurface9中最重要的方法是:

            l         LockRect——使用這個(gè)方法,我們將獲得一個(gè)指向表面內(nèi)存的指針,然后,通過(guò)一系列指針運(yùn)算,我們可以對(duì)表面上任一個(gè)像素點(diǎn)進(jìn)行讀、寫(xiě)操作。

             

            Locks a rectangle on a surface.

            HRESULT LockRect(
            D3DLOCKED_RECT * pLockedRect,
            CONST RECT * pRect,
            DWORD Flags
            );

            Parameters

            pLockedRect
            [out] Pointer to a D3DLOCKED_RECT structure that describes the locked region.
            pRect
            [in] Pointer to a rectangle to lock. Specified by a pointer to a RECT structure. Specifying NULL for this parameter expands the dirty region to cover the entire surface.
            Flags
            [in] Combination of zero or more locking flags that describe the type of lock to perform. For this method, the valid flags are:
            • D3DLOCK_DISCARD
            • D3DLOCK_DONOTWAIT
            • D3DLOCK_NO_DIRTY_UPDATE
            • D3DLOCK_NOSYSLOCK
            • D3DLOCK_READONLY
            You may not specify a subrect when using D3DLOCK_DISCARD. For a description of the flags, see D3DLOCK.

            Return Values

            If the method succeeds, the return value is D3D_OK.

            If the method fails, the return value can be D3DERR_INVALIDCALL or D3DERR_WASSTILLDRAWING.

            Remarks

            If the D3DLOCK_DONOTWAIT flag is specified and the driver cannot lock the surface immediately, IDirect3DSurface9::LockRect will return D3DERR_WASSTILLDRAWING so that an application can use the CPU cycles while waiting for the driver to lock the surface.

            The only lockable format for a depth-stencil surface is D3DFMT_D16_LOCKABLE. See D3DFORMAT.

            For performance reasons, dirty regions are recorded only for level zero of a texture. Dirty regions are automatically recorded when IDirect3DSurface9::LockRect is called without D3DLOCK_NO_DIRTY_UPDATE or D3DLOCK_READONLY. See IDirect3DDevice9::UpdateTexture for more information.

            A multisample back buffer cannot be locked.

            This method cannot retrieve data from a surface that is is contained by a texture resource created with D3DUSAGE_RENDERTARGET because such a texture must be assigned to D3DPOOL_DEFAULT memory and is therefore not lockable. In this case, use instead IDirect3DDevice9::GetRenderTargetData to copy texture data from device memory to system memory.

             

            l         UnlockRect——當(dāng)你調(diào)用了LockRect和完成了對(duì)表面內(nèi)存的訪問(wèn)后,你必須調(diào)用這個(gè)方法給表面解鎖。

             

            Unlocks a rectangle on a surface.

            HRESULT UnlockRect();

            Parameters

            None.

            Return Values

            If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be D3DERR_INVALIDCALL.

             

            l         GetDesc——這個(gè)方法將通過(guò)填充D3DSURFACE_DESC結(jié)構(gòu)來(lái)返回表面的描述信息。

             

            D3DSURFACE_DESC

            Describes a surface.

            typedef struct D3DSURFACE_DESC {
            D3DFORMAT Format;
            D3DRESOURCETYPE Type;
            DWORD Usage;
            D3DPOOL Pool;
            D3DMULTISAMPLE_TYPE MultiSampleType;
            DWORD MultiSampleQuality;
            UINT Width;
            UINT Height;
            } D3DSURFACE_DESC, *LPD3DSURFACE_DESC;

            Members

            Format
            Member of the D3DFORMAT enumerated type, describing the surface format.
            Type
            Member of the D3DRESOURCETYPE enumerated type, identifying this resource as a surface.
            Usage
            Either the D3DUSAGE_DEPTHSTENCIL or D3DUSAGE_RENDERTARGET values. For more information, see D3DUSAGE.
            Pool
            Member of the D3DPOOL enumerated type, specifying the class of memory allocated for this surface.
            MultiSampleType
            Member of the D3DMULTISAMPLE_TYPE enumerated type, specifying the levels of full-scene multisampling supported by the surface.
            MultiSampleQuality
            Quality level. The valid range is between zero and one less than the level returned by pQualityLevels used by IDirect3D9::CheckDeviceMultiSampleType. Passing a larger value returns the error, D3DERR_INVALIDCALL. The MultisampleQuality values of paired render targets, depth stencil surfaces and the MultiSample type must all match.
            Width
            Width of the surface, in pixels.
            Height
            Height of the surface, in pixels.

            Retrieves a description of the surface.

            HRESULT GetDesc(
            D3DSURFACE_DESC * pDesc
            );

            Parameters

            pDesc
            [out] Pointer to a D3DSURFACE_DESC structure, describing the surface.

            Return Values

            If the method succeeds, the return value is D3D_OK.

            D3DERR_INVALIDCALL is returned if the argument is invalid.

             

            最初鎖定表面和改寫(xiě)每一像素看來(lái)稍微有點(diǎn)迷茫。下面的代碼表示鎖定表面并將每一像素染成紅色:

            // Assume _surface is a pointer to an IDirect3DSurface9 interface.
            // Assumes a 32-bit pixel format for each pixel.

            // Get the surface description.
            D3DSURFACE_DESC surfaceDesc;
            _surface
            ->GetDesc(&surfaceDesc);

            // Get a pointer to the surface pixel data.
            D3DLOCKED RECT lockedRect;

            _surface
            ->LockRect(
                  
            &lockedRect,// pointer to receive locked data
                  0,          // lock entire surface
                  0);         // no lock flags specified

            // Iterate through each pixel in the surface and set it to red.
            DWORD* imageData = (DWORD*)lockedRect.pBits;

            for(int i = 0; i < surfaceDesc.Height; i++)
            {
                  
            for(int j = 0; j < surfaceDesc.Width; j++)
                  {
                        
            // index into texture, note we use the pitch and divide by
                        
            // four since the pitch is given in bytes and there are 4 bytes per DWORD.
                        int index = i * lockedRect.Pitch / 4 + j;

                        imageData[index] 
            = 0xffff0000// red
                  }
            }

            _surface
            ->UnlockRect();

            程序中D3DLOCKED_RECT結(jié)構(gòu)的定義如下:

            typedef struct _D3DLOCKED RECT {
                INT Pitch;   // the surface pitch
                void *pBits; // pointer to the start of the surface memory
            } D3DLOCKED_RECT;

            在這里有一些關(guān)于表面鎖定代碼的一些說(shuō)明。32-bit像素格式這個(gè)設(shè)定很重要,我們把bits轉(zhuǎn)換成DWORDs。這讓我們能把每一個(gè)DWORD視為表示一個(gè)像素。

            posted on 2008-03-12 18:52 lovedday 閱讀(1991) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            公告

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            隨筆分類(178)

            3D游戲編程相關(guān)鏈接

            搜索

            最新評(píng)論

            久久青青草原国产精品免费| 精品国产一区二区三区久久| 77777亚洲午夜久久多喷| 久久夜色精品国产噜噜噜亚洲AV| 亚洲精品无码久久久久AV麻豆| 久久久久国色AV免费看图片| 国产精品狼人久久久久影院| 亚洲国产精品狼友中文久久久| 久久中文字幕无码专区| 亚洲香蕉网久久综合影视 | 少妇被又大又粗又爽毛片久久黑人| 国产真实乱对白精彩久久| 久久精品中文无码资源站| 久久久国产精华液| 青青青青久久精品国产| 久久久午夜精品| 青青青青久久精品国产| 狠狠色婷婷久久一区二区三区| 性做久久久久久久久浪潮| 久久福利片| 国产91久久综合| 一本一道久久精品综合| 岛国搬运www久久| 91精品婷婷国产综合久久| 国产精品久久久久久福利69堂| 久久久久国产精品嫩草影院| 无码久久精品国产亚洲Av影片| 久久精品综合一区二区三区| 99re久久精品国产首页2020| AV无码久久久久不卡蜜桃| 国产精品久久久久久| 久久精品无码一区二区日韩AV| 久久性精品| 亚洲va久久久噜噜噜久久男同| 无码乱码观看精品久久| 欧美久久一级内射wwwwww.| 精品无码久久久久国产| 久久午夜福利电影| 久久久受www免费人成| 久久成人国产精品免费软件| 热久久国产精品|