新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     >>计算机科学论坛<<     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论高级C/C++编程、代码重构(Refactoring)、极限编程(XP)、泛型编程等话题
    [返回] 计算机科学论坛计算机技术与应用『 C/C++编程思想 』 → Direct3D程序设计基础 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 14673 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: Direct3D程序设计基础 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客楼主
    发贴心情 Direct3D程序设计基础

    Direct3D对象

    Microsoft Direct3D的一种实现方式是通过组件对象模型(Component Object Model, COM)及其接口实现的,在用C++语言和COM接口方式开发的程序中可以直接访问这些接口和对象。Direct3D对象是Direct3D程序中需要创建的第一个对象,也是需要最后一个释放的对象,这里所说的对象是指COM对象。通过Direct3D对象,可以枚举和检索Direct3D设备,这样应用程序就可以在不需要创建设备对象的前提下选择Direct3D渲染设备。

    在用C++语言编写Direct3D程序时,需要先获取一个指向IDirect3D9接口的指针,从而可以通过该接口调用Direct3D对象的功能。

    创建Direct3D设备对象

    创建Direct3D设备对象时,需要先创建Direct3D对象,然后再调用Direct3D对象的接口函数IDirect3D9::CreateDevice创建Direct3D设备对象。通过同一个Direct3D对象创建的所有Direct3D设备对象共享相同的物理资源(显卡)。因为共享同一硬件,所以如果通过一个Direct3D对象创建多个Direct3D渲染设备对象会明显降低系统性能。

    在创建Direct3D设备对象之前,还需要先初始化D3DPRESENT_PARAMETERS结构,该结构用于创建Direct3D设备对象。D3DPRESENT_PARAMETERS结构定义了Direct3D设备对象的相关信息,而这些信息将会影响Direct3D设备的显示方法。该结构的定义如下:

    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.


       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/11/26 8:24:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客2
    发贴心情 
    Direct3D程序基本结构

    虽然Direct3D功能非常强大,但是Direct3D程序的基本结构非常简单清晰,它主要有5个步骤:

    (1)创建一个Windows窗口。

    (2)初始化Direct3D,包括创建Direct3D对象、Direct3D设备对象以及要渲染的图形对象。

    (3)消息循环。

    (4)渲染图形。

    (5)清除在初始化时创建的所有COM对象,退出程序。

    其中消息循环和渲染图形不断进行,如果程序有消息需要处理,则先处理消息,然后再渲染图形;如果没有消息处理,则一直不停地渲染图形,直到退出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(45, 50, 170), 1.0f, 0);

        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, 200, 100, 600, 500,
                                 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, 0, 0, PM_REMOVE))
                {
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
                else
                {
                    render();
                }
            }
        }

        cleanup();

        UnregisterClass(CLASS_NAME, wc.hInstance);    

        return 0;
    }
      
    运行截图:

    按此在新窗口浏览图片

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/11/26 8:25:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客3
    发贴心情 
    创建窗口

    Direct3D是基于Microsoft Windows的图形开发接口,它的使用必须建立在Windows窗口的基础上,这就需要创建一个窗口,而创建窗口首先需要注册一个窗口类。示例程序中注册窗口类并根据窗口类创建窗口的代码如下:

     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, 200, 100, 600, 500,        NULL, NULL, wc.hInstance, NULL);
     if(hwnd == NULL)  return -1;

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/11/26 8:25:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客4
    发贴心情 
    初始化Direct3D

    创建了可供绘制图形的窗口后,在使用Direct3D渲染图形前,还需要进行与Direct3D相关的初始化操作,主要包括创建Direct3D对象并获取其接口指针,通过Direct3D对象创建Direct3D设备对象。

    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;}
    Direct3D设备(Direct3D Device)定义了Direct3D的所有绘图操作,绝大多数Direct3D操作都是通过Direct3D设备接口进行的。比较而言,Direct3D对象更像是DirectX显示信息的说明,而Direct3D设备对象则是3D功能的具体实现。

    要创建Direct3D设备,可调用IDirect3D9::CreateDevice()函数。

    Creates a device to represent the display adapter.

    HRESULT CreateDevice(  UINT Adapter,  D3DDEVTYPE DeviceType,  HWND hFocusWindow,  DWORD BehaviorFlags,  D3DPRESENT_PARAMETERS * pPresentationParameters,  IDirect3DDevice9 ** ppReturnedDeviceInterface);
    Parameters
    Adapter
    [in] Ordinal number that denotes the display adapter. D3DADAPTER_DEFAULT is always the primary display adapter.
    DeviceType
    [in] Member of the D3DDEVTYPE enumerated type that denotes the desired device type. If the desired device type is not available, the method will fail.
    hFocusWindow
    [in] The focus window alerts Direct3D when an application switches from foreground mode to background mode. See Remarks.
    For full-screen mode, the window specified must be a top-level window.
    For windowed mode, this parameter may be NULL only if the hDeviceWindow member of pPresentationParameters is set to a valid, non-NULL value.
    BehaviorFlags
    [in] Combination of one or more options that control device creation. For more information, see D3DCREATE.
    pPresentationParameters
    Pointer to a D3DPRESENT_PARAMETERS structure, describing the presentation parameters for the device to be created. If BehaviorFlags specifies D3DCREATE_ADAPTERGROUP_DEVICE, pPresentationParameters is an array. Regardless of the number of heads that exist, only one depth/stencil surface is automatically created.

    [in, out] For Windows 2000 and Windows XP, the full-screen device display refresh rate is set in the following order:
    User-specified nonzero ForcedRefreshRate registry key, if supported by the device.
    Application-specified nonzero refresh rate value in the presentation parameter.
    Refresh rate of the latest desktop mode, if supported by the device.
    75 hertz if supported by the device.
    60 hertz if supported by the device.
    Device default.
    An unsupported refresh rate will default to the closest supported refresh rate below it. For example, if the application specifies 63 hertz, 60 hertz will be used. There are no supported refresh rates below 57 hertz.

    pPresentationParameters is both an input and an output parameter. Calling this method may change several members including:

    If BackBufferCount, BackBufferWidth, and BackBufferHeight are 0 before the method is called, they will be changed when the method returns.
    If BackBufferFormat equals D3DFMT_UNKNOWN before the method is called, it will be changed when the method returns.
      

    ppReturnedDeviceInterface
    [out, retval] Address of a pointer to the returned IDirect3DDevice9 interface, which represents the created device.
    Return Values
    If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be one of the following: D3DERR_DEVICELOST, D3DERR_INVALIDCALL, D3DERR_NOTAVAILABLE, D3DERR_OUTOFVIDEOMEMORY.

    Remarks
    This method returns a fully working device interface, set to the required display mode (or windowed), and allocated with the appropriate back buffers. To begin rendering, the application needs only to create and set a depth buffer (assuming EnableAutoDepthStencil is FALSE in D3DPRESENT_PARAMETERS).

    When you create a Direct3D device, you supply two different window parameters: a focus window (hFocusWindow) and a device window (the hDeviceWindow in D3DPRESENT_PARAMETERS). The purpose of each window is:

    The focus window alerts Direct3D when an application switches from foreground mode to background mode (via Alt-Tab, a mouse click, or some other method). A single focus window is shared by each device created by an application.
    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.
    This method should not be run during the handling of WM_CREATE. An application should never pass a window handle to Direct3D while handling WM_CREATE. Any call to create, release, or reset the device must be done using the same thread as the window procedure of the focus window.

    Note that D3DCREATE_HARDWARE_VERTEXPROCESSING, D3DCREATE_MIXED_VERTEXPROCESSING, and D3DCREATE_SOFTWARE_VERTEXPROCESSING are mutually exclusive flags, and at least one of these vertex processing flags must be specified when calling this method.

    Back buffers created as part of the device are only lockable if D3DPRESENTFLAG_LOCKABLE_BACKBUFFER is specified in the presentation parameters. (Multisampled back buffers and depth surfaces are never lockable.)

    The methods IDirect3DDevice9::Reset, IUnknown, and IDirect3DDevice9::TestCooperativeLevel must be called from the same thread that used this method to create a device.

    D3DFMT_UNKNOWN can be specified for the windowed mode back buffer format when calling IDirect3D9::CreateDevice, IDirect3DDevice9::Reset, and IDirect3DDevice9::CreateAdditionalSwapChain. This means the application does not have to query the current desktop format before calling IDirect3D9::CreateDevice for windowed mode. For full-screen mode, the back buffer format must be specified.

    If you attempt to create a device on a 0x0 sized window, IDirect3D9::CreateDevice will fail.

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/11/26 8:25:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客5
    发贴心情 
    消息循环

    在Direct3D中,渲染图形通常是在消息循环中进行的:

      MSG msg;  ZeroMemory(&msg, sizeof(msg));
      while(msg.message != WM_QUIT)  {   if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))   {    TranslateMessage(&msg);    DispatchMessage(&msg);   }   else   {    render();   }  }
    这段代码的主要部分由PeekMessage()、TranslateMessage()和DispatchMessage()构成,它们是Windows程序标准的消息循环处理代码。当应用程序消息队列中出现一条消息时,PeekMessage()返回布尔值TRUE,执行TranslateMessage()进行消息转换,然后由DispatchMessage()把消息传递给窗口过程函数。

    特别需要注意的是,在消息循环中使用的是PeekMessage()函数,而不是GetMessage()函数。函数PeekMessage()和GetMessage()的功能大体相同,作用都是从消息队列中取一条消息出来,唯一不同的是,每当GetMessage()发现消息队列中没有消息时,过门不入,而PeekMessage()发现消息队列中没有消息时,会取回系统控制权,让程序在此停留一段时间,应用程序就是在这时候处理render()函数。也就是说,渲染函数render()都是在程序运行时的空闲时间调用的。

    注意理解PeekMessage()和GetMessage()运行机制的区别,这对理解Direct3D程序中如何实现动画是很关键的。

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/11/26 8:26:00
     
     卷积内核 帅哥哟,离线,有人找我吗?
      
      
      威望:8
      头衔:总统
      等级:博士二年级(版主)
      文章:3942
      积分:27590
      门派:XML.ORG.CN
      注册:2004/7/21

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给卷积内核发送一个短消息 把卷积内核加入好友 查看卷积内核的个人资料 搜索卷积内核在『 C/C++编程思想 』的所有贴子 访问卷积内核的主页 引用回复这个贴子 回复这个贴子 查看卷积内核的博客6
    发贴心情 
    渲染图形

    使用Direct3D绘制三维图形和使用GDI绘制二维图形的方法非常类似,Direct3D程序中的Direct3D设备对象相当于GDI程序中的hdc(设备描述表),使用 GDI绘制图形前,通常需要先利用hdc进行相关设置,然后通过hdc进行绘图。同样在Direct3D程序中通常先通过Direct3D设备接口进行相关的渲染设备设置,然后再渲染图形。而且所有的渲染图形操作必须在函数BeginScene()和EndScene()之间进行。

    void render(){ g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_X#2d32aa, 1.0f, 0);
     g_device->BeginScene();
     // render game scene here
     g_device->EndScene();
     g_device->Present(NULL, NULL, NULL, NULL);}
    IDirect3DDevice9::Clear()函数的作用是清空一个或多个表面的内容。Direct3D在绘制图形前,需要使用该函数清空视口(viewport)的颜色缓冲区、深度缓冲区或者模板缓冲区。

    Clears one or more surfaces such as a render target, multiple render targets, a stencil buffer, and a depth buffer.

    HRESULT Clear(  DWORD Count,  CONST D3DRECT * pRects,  DWORD Flags,  D3DCOLOR Color,  float Z,  DWORD Stencil);
    Parameters
    Count
    [in] Number of rectangles in the array at pRects. Must be set to 0 if pRects is NULL. May not be 0 if pRects is a valid pointer.
    pRects
    [in] Pointer to an array of D3DRECT structures 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. Coordinates are clipped to the bounds of the viewport rectangle. To indicate that the entire viewport rectangle is to be cleared, set this parameter to NULL and Count to 0.
    Flags
    [in] Combination of one or more D3DCLEAR flags that specify the surface(s) that will be cleared.
    Color
    [in] Clear a render target to this ARGB color.
    Z
    [in] Clear the depth buffer to this new z value which ranges from 0 to 1. See remarks.
    Stencil
    [in] Clear the stencil buffer to this new value which ranges from 0 to 2n - 1 (n is the bit depth of the stencil buffer). See remarks.
    Return Values
    If the method succeeds, the return value is D3D_OK. If the method fails, the return value can be: D3DERR_INVALIDCALL.

    Remarks
    Use this method to clear a surface including: a render target, all render targets in an MRT, a stencil buffer, or a depth buffer. Flags determines how many surfaces are cleared. Use pRects to clear a subset of a surface defined by an array of rectangles.

    IDirect3DDevice9::Clear will fail if you:

    Try to clear either the depth buffer or the stencil buffer of a render target that does not have an attached depth buffer.
    Try to clear the stencil buffer when the depth buffer does not contain stencil data.
    大多数三维图形程序拥有2个或者更多颜色缓冲区。用于当前屏幕刷新的颜色缓冲区称为前台缓冲区,而将用于图形绘制的其他颜色缓冲区称为后台缓冲区。同时拥有前台缓冲区和后台缓冲区的图形程序可以同时进行图形绘制和屏幕刷新工作,系统的运行性能优于仅有单个颜色缓冲区的图形程序。在Direct3D程序中最常见的情况是有一个前台缓冲区和一个后台缓冲区。

    在绘制图形开始之前,必须先调用函数IDirect3DDevice9::BeginScene()函数,通知Direct3D设备开始绘制图形(又称渲染),否则Direct3D程序就会出错返回;渲染程序完成后,也必须调用函数IDirect3DDevice9::EndScene()通知Direct3D设备结束渲染。而且这两个函数必须成对出现,不允许嵌套和交错的情况发生。任何Direct3D渲染函数都必须在函数BeginScene()和函数EndScene()的中间出现。

    Direct3D在后台缓冲区进行图形绘制操作,当所有的图形绘制操作结束之后,调用函数IDirect3DDevice9::Present()将在后台缓冲区绘制的内容提交到前台显示,这样绘制的图形就显示在屏幕上了。

    Presents the contents of the next buffer in the sequence of back buffers owned by the device.

    HRESULT Present(  CONST RECT * pSourceRect,  CONST RECT * pDestRect,  HWND hDestWindowOverride,  CONST RGNDATA * pDirtyRegion);
    Parameters
    pSourceRect
    [in] Pointer to a value that must be NULL unless the swap chain was created with D3DSWAPEFFECT_COPY. pSourceRect is a pointer to a RECT structure containing the source rectangle. If NULL, the entire source surface is presented. If the rectangle exceeds the source surface, the rectangle is clipped to the source surface.
    pDestRect
    [in] Pointer to a value that must be NULL unless the swap chain was created with D3DSWAPEFFECT_COPY. pDestRect is a pointer to a RECT structure containing the destination rectangle, in window client coordinates. If NULL, the entire client area is filled. If the rectangle exceeds the destination client area, the rectangle is clipped to the destination client area.
    hDestWindowOverride
    [in] Pointer to a destination window whose client area is taken as the target for this presentation. If this value is NULL, then the hWndDeviceWindow member of D3DPRESENT_PARAMETERS is taken.
    pDirtyRegion
    [in] Value must be NULL unless the swap chain was created with D3DSWAPEFFECT_COPY. For more information about swap chains, see Flipping Surfaces (Direct3D 9) and D3DSWAPEFFECT. If this value is non-NULL, the contained region is expressed in back buffer coordinates. The rectangles within the region are the minimal set of pixels that need to be updated. This method takes these rectangles into account when optimizing the presentation by copying only the pixels within the region, or some suitably expanded set of rectangles. This is an aid to optimization only, and the application should not rely on the region being copied exactly. The implementation can choose to copy the whole source rectangle.
    Return Values
    Possible return values include: D3D_OK or D3DERR_DEVICEREMOVED (see D3DERR).

    Remarks
    If necessary, a stretch operation is applied to transfer the pixels within the source rectangle to the destination rectangle in the client area of the target window.

    IDirect3DDevice9::Present will fail, returning D3DERR_INVALIDCALL, if called between BeginScene and EndScene pairs unless the render target is not the current render target (such as the back buffer you get from creating an additional swap chain). This is a new behavior for Direct3D 9.

    ----------------------------------------------
    事业是国家的,荣誉是单位的,成绩是领导的,工资是老婆的,财产是孩子的,错误是自己的。

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2008/11/26 8:26:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 C/C++编程思想 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/4/28 9:17:48

    本主题贴数6,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    140.625ms