| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 542 人关注过本帖
标题:DX锁定顶点缓存的疑惑
只看楼主 加入收藏
Nitpicker
Rank: 1
等 级:新手上路
帖 子:26
专家分:8
注 册:2012-3-28
结帖率:83.33%
收藏
 问题点数:0 回复次数:0 
DX锁定顶点缓存的疑惑
//下面的是那本DirectX教程书附带的源码,目的就是简单的画个四边形
//但是作者太懒,都不试试自己的代码行不行就拿出来卖了
//有些疑惑,尤其是红色的部分 望高手指教
#include <d3d9.h>                   // Direct3D头文件

#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=NULL; } }//防止二次释放

wchar_t *g_pClassName  = L"VertexBuffer";   // 窗口类名
wchar_t *g_pWindowName = L"顶点缓存示例";   // 窗口标题名

LPDIRECT3DDEVICE9       g_pd3dDevice = NULL;    // Direct3D设备接口
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuf = NULL;    // 顶点缓存接口

struct CUSTOMVERTEX
{
    FLOAT _x, _y, _z, _rhw;         // 顶点的位置
    DWORD _color;                   // 顶点的颜色
    CUSTOMVERTEX(FLOAT x, FLOAT y, FLOAT z, FLOAT rhw, DWORD color)
        : _x(x), _y(y), _z(z), _rhw(rhw), _color(color) {}
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)


HRESULT InitDirect3D(HWND hWnd);    // 初始化Direct3D
VOID Direct3DRender();              // 渲染图形
VOID Direct3DCleanup();             // 清理Direct3D资源

// 窗口消息处理函数声明
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

//--------------------------------------------------------------------------------------
// Name: WinMain();
// Desc: Windows应用程序入口函数
//--------------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nShowCmd)
{
    // 初始化窗口类
    WNDCLASS wndclass;
    wndclass.cbClsExtra     = 0;
    wndclass.cbWndExtra     = 0;
    wndclass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);  // 窗口背景
    wndclass.hCursor        = LoadCursor(NULL, IDC_ARROW);          // 光标形状
    wndclass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);      // 窗口小图标
    wndclass.hInstance      = hInstance;
    wndclass.lpfnWndProc    = WndProc;
    wndclass.lpszClassName  = g_pClassName;
    wndclass.lpszMenuName   = NULL;
    wndclass.style          = CS_HREDRAW | CS_VREDRAW;

    // 注册窗口类
    if (!RegisterClass(&wndclass))
        return 0;

    // 创建窗口
    HWND hWnd = CreateWindow(g_pClassName, g_pWindowName, WS_OVERLAPPEDWINDOW,
        100, 100, 640, 480, NULL, NULL, wndclass.hInstance, NULL);

    // 初始化Direct3D
    InitDirect3D(hWnd);

    // 显示、更新窗口
    ShowWindow(hWnd, SW_SHOWDEFAULT);
    UpdateWindow(hWnd);

    // 消息循环
    MSG msg;
    ZeroMemory(&msg, sizeof(msg));
    while (msg.message!=WM_QUIT)
    {
        if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            Direct3DRender();       // 绘制3D场景
        }
    }

    UnregisterClass(g_pClassName, wndclass.hInstance);
    return 0;
}

//--------------------------------------------------------------------------------------
// Name: WndProc()
// Desc: 窗口消息处理函数
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_PAINT:                  // 客户区重绘消息
        Direct3DRender();           // 渲染图形
        ValidateRect(hWnd, NULL);   // 更新客户区的显示
        break;
    case WM_KEYDOWN:                // 键盘按下消息
        if (wParam == VK_ESCAPE)    // ESC键
            DestroyWindow(hWnd);    // 销毁窗口, 并发送一条WM_DESTROY消息
        break;
    case WM_DESTROY:                // 窗口销毁消息
        Direct3DCleanup();          // 清理Direct3D
        PostQuitMessage(0);         // 退出程序
        break;
    }
    // 默认的消息处理
    return DefWindowProc( hWnd, message, wParam, lParam );
}

//--------------------------------------------------------------------------------------
// Name: InitDirect3D()
// Desc: 初始化Direct3D
//--------------------------------------------------------------------------------------
HRESULT InitDirect3D(HWND hWnd)
{
    // 创建IDirect3D接口
    LPDIRECT3D9 pD3D = NULL;                    // IDirect3D9接口
    pD3D = Direct3DCreate9(D3D_SDK_VERSION);    // 创建IDirect3D9接口对象
    if (pD3D == NULL) return E_FAIL;

    // 获取硬件设备信息
    D3DCAPS9 caps; int vp = 0;
    pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps );
    if( caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT )
        vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
    else
        vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;

    // 创建Direct3D设备接口
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.BackBufferWidth            = 640;
    d3dpp.BackBufferHeight           = 480;
    d3dpp.BackBufferFormat           = D3DFMT_A8R8G8B8;
    d3dpp.BackBufferCount            = 1;
    d3dpp.MultiSampleType            = D3DMULTISAMPLE_NONE;
    d3dpp.MultiSampleQuality         = 0;
    d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow              = hWnd;
    d3dpp.Windowed                   = true;
    d3dpp.EnableAutoDepthStencil     = true;
    d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
    d3dpp.Flags                      = 0;
    d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    d3dpp.PresentationInterval       = D3DPRESENT_INTERVAL_IMMEDIATE;

    pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
        hWnd, vp, &d3dpp, &g_pd3dDevice);
    pD3D->Release();

//////****************************************************************
    // 创建顶点缓存
    g_pd3dDevice->CreateVertexBuffer(6 * sizeof(CUSTOMVERTEX), 0,
        D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVertexBuf, NULL);

    // 填充顶点数据
    CUSTOMVERTEX *pVertices = NULL;
    g_pVertexBuf->Lock(0,6 * sizeof(CUSTOMVERTEX), (void**)&pVertices, 0);
                      //在其他成功的例子里上面的第二个参数设为0或整个数组的大小结果都行得通
    //然后: 前面声明一个指向一个结构的指针,在下面就直接用pVertice[0]...pVertice[i]
   //这样算什么写法? 作者到底为什么会这样?(虽然没达到效果,但是编译是没错的)
    pVertices[0] = CUSTOMVERTEX( 220.0f, 120.0f, 0.5f, -1.0f, D3DCOLOR_XRGB(255, 0, 0));     // V0
    pVertices[1] = CUSTOMVERTEX( 420.0f, 120.0f, 0.5f, -1.0f, D3DCOLOR_XRGB(0, 255, 0));     // V1
    pVertices[2] = CUSTOMVERTEX( 220.0f, 320.0f, 0.5f, -1.0f, D3DCOLOR_XRGB(255, 255, 0));   // V2

    pVertices[3] = CUSTOMVERTEX( 420.0f, 120.0f, 0.5f, -1.0f, D3DCOLOR_XRGB(0, 255, 0));     // V1
    pVertices[4] = CUSTOMVERTEX( 420.0f, 320.0f, 0.5f, -1.0f, D3DCOLOR_XRGB(0, 0, 255));     // V3
    pVertices[5] = CUSTOMVERTEX( 220.0f, 320.0f, 0.5f, -1.0f, D3DCOLOR_XRGB(255, 255, 0));   // V2
   
    g_pVertexBuf->Unlock();
///////********************************8
   

 return S_OK;
}

//--------------------------------------------------------------------------------------
// Name: Direct3DRender()
// Desc: 绘制3D场景
//--------------------------------------------------------------------------------------
VOID Direct3DRender()
{
    g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(45, 50, 170), 1.0f, 0);
    g_pd3dDevice->BeginScene();                     // 开始绘制

    // 渲染正方形
       g_pd3dDevice->SetStreamSource(0, g_pVertexBuf, 0, sizeof(CUSTOMVERTEX));
      g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
      g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

    g_pd3dDevice->EndScene();                       // 结束绘制
    g_pd3dDevice->Present(NULL, NULL, NULL, NULL);  // 翻转
}

//--------------------------------------------------------------------------------------
// Name: Direct3DCleanup()
// Desc: 清理Direct3D, 并释放COM接口
//--------------------------------------------------------------------------------------
VOID Direct3DCleanup()
{
    SAFE_RELEASE(g_pVertexBuf);     // 释放顶点缓存接口
    SAFE_RELEASE(g_pd3dDevice);     // 释放D3D设备接口
}


[ 本帖最后由 Nitpicker 于 2013-1-3 18:43 编辑 ]
搜索更多相关主题的帖子: DirectX include 四边形 
2013-01-03 15:40
快速回复:DX锁定顶点缓存的疑惑
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028289 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved