青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

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

Direct3D程序設(shè)計(jì)基礎(chǔ)(1)

Direct3D對象

Microsoft Direct3D的一種實(shí)現(xiàn)方式是通過組件對象模型(Component Object Model, COM)及其接口實(shí)現(xiàn)的,在用C++語言和COM接口方式開發(fā)的程序中可以直接訪問這些接口和對象。Direct3D對象是Direct3D程序中需要創(chuàng)建的第一個對象,也是需要最后一個釋放的對象,這里所說的對象是指COM對象。通過Direct3D對象,可以枚舉和檢索Direct3D設(shè)備,這樣應(yīng)用程序就可以在不需要創(chuàng)建設(shè)備對象的前提下選擇Direct3D渲染設(shè)備。

在用C++語言編寫Direct3D程序時,需要先獲取一個指向IDirect3D9接口的指針,從而可以通過該接口調(diào)用Direct3D對象的功能。

 

創(chuàng)建Direct3D設(shè)備對象

創(chuàng)建Direct3D設(shè)備對象時,需要先創(chuàng)建Direct3D對象,然后再調(diào)用Direct3D對象的接口函數(shù)IDirect3D9::CreateDevice創(chuàng)建Direct3D設(shè)備對象。通過同一個Direct3D對象創(chuàng)建的所有Direct3D設(shè)備對象共享相同的物理資源(顯卡)。因?yàn)楣蚕硗挥布匀绻ㄟ^一個Direct3D對象創(chuàng)建多個Direct3D渲染設(shè)備對象會明顯降低系統(tǒng)性能。

在創(chuàng)建Direct3D設(shè)備對象之前,還需要先初始化D3DPRESENT_PARAMETERS結(jié)構(gòu),該結(jié)構(gòu)用于創(chuàng)建Direct3D設(shè)備對象。D3DPRESENT_PARAMETERS結(jié)構(gòu)定義了Direct3D設(shè)備對象的相關(guān)信息,而這些信息將會影響Direct3D設(shè)備的顯示方法。該結(jié)構(gòu)的定義如下:

Describes the presentation parameters.

typedef struct D3DPRESENT_PARAMETERS {
UINT BackBufferWidth, BackBufferHeight;
D3DFORMAT BackBufferFormat;
UINT BackBufferCount;
D3DMULTISAMPLE_TYPE MultiSampleType;
DWORD MultiSampleQuality;
D3DSWAPEFFECT SwapEffect;
HWND hDeviceWindow;
BOOL Windowed;
BOOL EnableAutoDepthStencil;
D3DFORMAT AutoDepthStencilFormat;
DWORD Flags;
UINT FullScreen_RefreshRateInHz;
UINT PresentationInterval;
} D3DPRESENT_PARAMETERS, *LPD3DPRESENT_PARAMETERS;

Members

BackBufferWidth, BackBufferHeight
Width and height of the new swap chain's back buffers, in pixels. If Windowed is FALSE (the presentation is full-screen), these values must equal the width and height of one of the enumerated display modes found through IDirect3D9::EnumAdapterModes. If Windowed is TRUE and either of these values is zero, the corresponding dimension of the client area of the hDeviceWindow (or the focus window, if hDeviceWindow is NULL) is taken.
BackBufferFormat
The back buffer format. For more information about formats, see D3DFORMAT. This value must be one of the render-target formats as validated by IDirect3D9::CheckDeviceType. You can use IDirect3DDevice9::GetDisplayMode to obtain the current format.

In fact, D3DFMT_UNKNOWN can be specified for the BackBufferFormat while in windowed mode. This tells the runtime to use the current display-mode format and eliminates the need to call IDirect3DDevice9::GetDisplayMode.

For windowed applications, the back buffer format no longer needs to match the display-mode format because color conversion can now be done by the hardware (if the hardware supports color conversion). The set of possible back buffer formats is constrained, but the runtime will allow any valid back buffer format to be presented to any desktop format. (There is the additional requirement that the device be operable in the desktop mode; devices typically do not operate in 8 bits per pixel modes.)

Full-screen applications cannot do color conversion.

BackBufferCount
This value can be between 0 and D3DPRESENT_BACK_BUFFERS_MAX (or D3DPRESENT_BACK_BUFFERS_MAX_EX when using Direct3D 9Ex). Values of 0 are treated as 1. If the number of back buffers cannot be created, the runtime will fail the method call and fill this value with the number of back buffers that could be created. As a result, an application can call the method twice with the same D3DPRESENT_PARAMETERS structure and expect it to work the second time.

The method fails if one back buffer cannot be created. The value of BackBufferCount influences what set of swap effects are allowed. Specifically, any D3DSWAPEFFECT_COPY swap effect requires that there be exactly one back buffer.

MultiSampleType
Member of the D3DMULTISAMPLE_TYPE enumerated type. The value must be D3DMULTISAMPLE_NONE unless SwapEffect has been set to D3DSWAPEFFECT_DISCARD. Multisampling is supported only if the swap effect is D3DSWAPEFFECT_DISCARD.
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. Paired values of render targets or of depth stencil surfaces and D3DMULTISAMPLE_TYPE must match.
SwapEffect
Member of the D3DSWAPEFFECT enumerated type. The runtime will guarantee the implied semantics concerning buffer swap behavior; therefore, if Windowed is TRUE and SwapEffect is set to D3DSWAPEFFECT_FLIP, the runtime will create one extra back buffer and copy whichever becomes the front buffer at presentation time.

D3DSWAPEFFECT_COPY requires that BackBufferCount be set to 1.

D3DSWAPEFFECT_DISCARD will be enforced in the debug runtime by filling any buffer with noise after it is presented.

hDeviceWindow
The device window determines the location and size of the back buffer on screen. This is used by Direct3D when the back buffer contents are copied to the front buffer during IDirect3DDevice9::Present.
  • For a full-screen application, this is a handle to the top window (which is the focus window).

    For applications that use multiple full-screen devices (such as a multimonitor system), exactly one device can use the focus window as the device window. All other devices must have unique device windows.

  • For a windowed-mode application, this handle will be the default target window for IDirect3DDevice9::Present. If this handle is NULL, the focus window will be taken.

Note that no attempt is made by the runtime to reflect user changes in window size. The back buffer is not implicitly reset when this window is reset. However, the IDirect3DDevice9::Present method does automatically track window position changes.

Windowed
TRUE if the application runs windowed; FALSE if the application runs full-screen.
EnableAutoDepthStencil
If this value is TRUE, Direct3D will manage depth buffers for the application. The device will create a depth-stencil buffer when it is created. The depth-stencil buffer will be automatically set as the render target of the device. When the device is reset, the depth-stencil buffer will be automatically destroyed and recreated in the new size.

If EnableAutoDepthStencil is TRUE, then AutoDepthStencilFormat must be a valid depth-stencil format.

AutoDepthStencilFormat
Member of the D3DFORMAT enumerated type. The format of the automatic depth-stencil surface that the device will create. This member is ignored unless EnableAutoDepthStencil is TRUE.
Flags
One of the D3DPRESENTFLAG constants.
FullScreen_RefreshRateInHz
The rate at which the display adapter refreshes the screen. The value depends on the mode in which the application is running:
  • For windowed mode, the refresh rate must be 0.
  • For full-screen mode, the refresh rate is one of the refresh rates returned by IDirect3D9::EnumAdapterModes.
PresentationInterval
The maximum rate at which the swap chain's back buffers can be presented to the front buffer. For a detailed explanation of the modes and the intervals that are supported, see D3DPRESENT.

 

Direct3D程序基本結(jié)構(gòu)

雖然Direct3D功能非常強(qiáng)大,但是Direct3D程序的基本結(jié)構(gòu)非常簡單清晰,它主要有5個步驟:

(1)創(chuàng)建一個Windows窗口。

(2)初始化Direct3D,包括創(chuàng)建Direct3D對象、Direct3D設(shè)備對象以及要渲染的圖形對象。

(3)消息循環(huán)。

(4)渲染圖形。

(5)清除在初始化時創(chuàng)建的所有COM對象,退出程序。

其中消息循環(huán)和渲染圖形不斷進(jìn)行,如果程序有消息需要處理,則先處理消息,然后再渲染圖形;如果沒有消息處理,則一直不停地渲染圖形,直到退出Direct3D程序。

 

最簡單的Direct3D程序

#include <d3d9.h>

#define CLASS_NAME    "GameApp"

#define release_com(p)    { if(p) { (p)->Release(); (p) = NULL; } }

IDirect3D9
*            g_d3d;
IDirect3DDevice9
*    g_device;

bool init_d3d(HWND hwnd)
{
    g_d3d 
= Direct3DCreate9(D3D_SDK_VERSION);

    
if(g_d3d == NULL)
        
return false;

    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(
&d3dpp, sizeof(d3dpp));

    d3dpp.Windowed            
= TRUE;
    d3dpp.SwapEffect        
= D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat    
= D3DFMT_UNKNOWN;

    
if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                  
&d3dpp, &g_device)))
    {
        
return false;
    }

    
return true;
}

void cleanup()
{
    release_com(g_device);
    release_com(g_d3d);
}

void render()
{
    g_device
->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(4550170), 1.0f0);

    g_device
->BeginScene();

    
// render game scene here 

    g_device
->EndScene();

    g_device
->Present(NULL, NULL, NULL, NULL);
}

LRESULT WINAPI WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    
switch(msg)
    {
    
case WM_KEYDOWN:
        
if(wParam == VK_ESCAPE)
            DestroyWindow(hwnd);
        
break;

    
case WM_DESTROY:        
        PostQuitMessage(
0);
        
return 0;
    }

    
return DefWindowProc(hwnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR, INT)
{
    WNDCLASSEX wc;

    wc.cbSize            
= sizeof(WNDCLASSEX);
    wc.style            
= CS_CLASSDC;
    wc.lpfnWndProc        
= WinProc;
    wc.cbClsExtra        
= 0;
    wc.cbWndExtra        
= 0;
    wc.hInstance        
= inst;
    wc.hIcon            
= NULL;
    wc.hCursor            
= NULL;
    wc.hbrBackground    
= NULL;
    wc.lpszMenuName        
= NULL;
    wc.lpszClassName    
= CLASS_NAME;
    wc.hIconSm            
= NULL;

    
if(! RegisterClassEx(&wc))
        
return -1;

    HWND hwnd 
= CreateWindow(CLASS_NAME, "Direct3D App", WS_OVERLAPPEDWINDOW, 200100600500,
                             NULL, NULL, wc.hInstance, NULL);

    
if(hwnd == NULL)
        
return -1;

    
if(init_d3d(hwnd))
    {
        ShowWindow(hwnd, SW_SHOWDEFAULT);
        UpdateWindow(hwnd);

        MSG msg;
        ZeroMemory(
&msg, sizeof(msg));

        
while(msg.message != WM_QUIT)
        {
            
if(PeekMessage(&msg, NULL, 00, PM_REMOVE))
            {
                TranslateMessage(
&msg);
                DispatchMessage(
&msg);
            }
            
else
            {
                render();
            }
        }
    }

    cleanup();

    UnregisterClass(CLASS_NAME, wc.hInstance);    

    
return 0;
}

 

運(yùn)行截圖:


posted on 2008-04-29 12:57 lovedday 閱讀(2804) 評論(2)  編輯 收藏 引用

評論

# re: Direct3D程序設(shè)計(jì)基礎(chǔ)(1) 2008-07-19 17:58 riki

真佩服你,你是從哪里找來那么全的資料的。我看你資料好久了。  回復(fù)  更多評論   

# re: Direct3D程序設(shè)計(jì)基礎(chǔ)(1) 2008-08-18 05:10 lyra

我也很佩服 人真好啊 弄這么多資料讓我們學(xué)習(xí)  回復(fù)  更多評論   


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


公告

導(dǎo)航

統(tǒng)計(jì)

常用鏈接

隨筆分類(178)

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

搜索

最新評論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            亚洲精品乱码| 美日韩精品视频| 亚洲免费一在线| 小处雏高清一区二区三区| 久久久久久一区二区三区| 欧美顶级大胆免费视频| 欧美日韩国产精品一区| 国产欧美一区二区精品仙草咪| 在线观看国产欧美| 亚洲午夜av| 欧美大胆成人| 亚洲影院一区| 欧美日韩国产色视频| 在线一区免费观看| 亚洲精品免费在线播放| 亚洲国产一区二区三区青草影视| 一区二区久久| 亚洲一区二区三区高清| 红桃视频成人| 午夜精品短视频| 欧美黄色精品| 久久国产精品黑丝| 国产精品美腿一区在线看| 亚洲人在线视频| 免费成人av| 欧美亚洲一区二区在线| 欧美午夜精品| 一本综合久久| 欧美电影在线| 国产精品二区三区四区| 亚洲少妇中出一区| 亚洲精品视频免费观看| 美女黄色成人网| 午夜精品久久久久久久久久久久| 久久全国免费视频| 一区二区三区在线免费视频| 久久久久9999亚洲精品| 亚洲网址在线| 国产精品一卡| 久久亚洲免费| 久久精品国产成人| 亚洲综合丁香| 亚洲婷婷国产精品电影人久久 | 欧美一区二区视频网站| 亚洲人成精品久久久久| 欧美亚洲网站| 亚洲欧美另类在线| 国产精品99久久久久久有的能看| 欧美日韩免费在线视频| 亚洲婷婷在线| 欧美二区在线看| 正在播放欧美视频| 久热国产精品| 亚洲蜜桃精久久久久久久| 亚洲精品日韩激情在线电影| 黄色成人小视频| 欧美一区2区三区4区公司二百| 在线一区免费观看| 欧美激情一区二区久久久| 亚洲婷婷在线| 欧美日韩国产影院| 最新国产拍偷乱拍精品| 欧美先锋影音| 99xxxx成人网| 国产一区在线免费观看| 欧美国产视频日韩| 一区二区在线看| 久久久噜噜噜久久中文字免| 久久亚洲综合色一区二区三区| 国产伦精品一区二区三区免费迷| 亚洲免费中文字幕| 欧美伊人影院| 欧美精品在线一区| 久久av二区| 欧美理论在线播放| 亚洲精品影视在线观看| 亚洲精品一区二区三区av| 欧美88av| 久久这里有精品15一区二区三区 | 欧美成人在线免费观看| 欧美激情在线播放| 日韩视频在线播放| 一区二区三区日韩精品| 在线欧美日韩国产| 亚洲永久免费观看| 久久久久国产精品麻豆ai换脸| 国产亚洲人成网站在线观看| 亚洲精品日韩激情在线电影| 亚洲一卡久久| 国产人久久人人人人爽| 久久嫩草精品久久久久| 91久久国产综合久久91精品网站| 亚洲特黄一级片| 国产婷婷色一区二区三区四区| aaa亚洲精品一二三区| 亚洲欧美资源在线| 激情久久久久| 欧美日韩一卡二卡| 久久激情视频免费观看| 亚洲韩国精品一区| 亚洲欧美一区二区在线观看| 精品成人a区在线观看| 欧美啪啪一区| 欧美在线免费视屏| 久久精品欧美日韩| 国产精品亚洲网站| 亚洲一二三级电影| 欧美大片一区二区| 亚洲综合第一| 亚洲精品国久久99热| 国产精品久久毛片a| 久久免费视频网| 亚洲一区二区三区四区中文| 欧美成人精品激情在线观看| 在线观看国产精品网站| 欧美色网在线| 亚洲一区欧美二区| 亚洲黄色尤物视频| 久久美女性网| 精品91久久久久| 国产精品一区二区三区成人| 欧美成人官网二区| 久久九九国产| 亚洲第一区色| 日韩亚洲视频| 亚洲第一网站免费视频| 国产欧美日韩中文字幕在线| 欧美日韩一区二区精品| 欧美成人黑人xx视频免费观看| 欧美在线视频观看| 午夜宅男欧美| 亚洲在线观看免费| 一区二区三区高清在线| 亚洲日本va在线观看| 一区二区高清| 日韩视频免费观看高清完整版| 亚洲成色999久久网站| 国产中文一区二区| 国产午夜精品美女毛片视频| 国产精品视频福利| 国产精品久久国产精品99gif| 欧美日本在线播放| 欧美日韩国产精品自在自线| 欧美精品999| 欧美片第一页| 国产精品xvideos88| 欧美视频精品在线观看| 欧美午夜精品一区| 国产精品高潮呻吟久久av黑人| 国产精品qvod| 国产欧美日韩精品丝袜高跟鞋 | 欧美精品一区二区三| 欧美成人福利视频| 欧美国产第二页| 欧美视频一区二区在线观看 | 久久综合中文| 欧美暴力喷水在线| 欧美精品日韩一本| 欧美性开放视频| 国产女同一区二区| 一区视频在线播放| 亚洲精品视频二区| 亚洲摸下面视频| 久久久久久久久蜜桃| 欧美成人福利视频| 日韩视频亚洲视频| 欧美v国产在线一区二区三区| 欧美激情精品| 亚洲一区二区成人| 久久精品久久综合| 欧美啪啪成人vr| 国产婷婷色一区二区三区在线 | 国产精品永久| 亚洲电影第三页| 在线午夜精品自拍| 欧美在线在线| 亚洲国产欧美国产综合一区| 亚洲视频一区| 久久久久久综合| 国产精品大全| 亚洲国产精品精华液网站| 国产欧美日韩在线视频| 在线精品视频在线观看高清| 一区二区欧美国产| 久久久视频精品| 亚洲六月丁香色婷婷综合久久| 欧美在线观看网站| 欧美日韩在线一区二区| 在线精品视频免费观看| 亚洲伊人伊色伊影伊综合网| 欧美福利在线观看| 亚洲欧美一区二区激情| 午夜在线一区二区| 欧美另类人妖| 亚洲大胆av| 久久米奇亚洲| 亚洲一区二区免费在线| 欧美激情91| 亚洲欧洲精品一区二区三区| 久久精品国产一区二区三|