C ++,DirecX 9:渲染自制的顶点结构

时间:2015-10-24 13:55:08

标签: c++ directx-9

我是StackOverflow的新手,这是我的第一个问题。 我对C ++和DirectX9很陌生,所以他们的代码可能会出现很多问题!

我的问题: 我想渲染一个" D3DXVECTOR3" s的结构,用于从.raw制作高度图。

我有一个工作的FP Camera System,想要实现一个Heightmap。我遵循了DirectX9和C ++中的Tutorial" Terrain" (https://www.tutorials.de/threads/tutorial-terrain-in-directx-9-und-c-teil-1.343473/),但代码不能正常工作(教程在" Bad"德语,我使用它,因为我没有找到任何高度图教程DirectX9 ......我是德国人。

所有输出都是This(忽略圆柱体,它只是一个测试): renderissue

以下是本准则的一些相关部分:

Heightmap.cpp

BOOL LoadMapFromRAW(char* pcHMAP, UINT sizePerSide, BYTE **ppData)
{
    BYTE *pData = NULL;
    FILE *pFile = NULL;

    // Open as Binary
    pFile = fopen(pcHMAP, "rb");
    if (!pFile)
        return false;

    // Memory
    pData = new BYTE[sizePerSide*sizePerSide];
    // Read
    fread(&pData, 1, sizePerSide*sizePerSide, pFile);
    *ppData = pData;

    return true;
}


BOOL CreateVertices(UINT sizePerSide, BYTE *pVertexData, STerrainVector **ppVertices, UINT *uiTriangleCount)
{
    UINT uiSizePerSide = sizePerSide - 1;
    *uiTriangleCount = uiSizePerSide*uiSizePerSide*2;

    // Memory for Vertices
    STerrainVector *pVertices = new STerrainVector[(*uiTriangleCount) * 3];
    // Buffer
    int index = 0;
    for (int x = 0; x < uiSizePerSide; x++)
        for (int z = 0; z < uiSizePerSide; z++)
        {
            index += 6;
            pVertices[index + 0].vPos = D3DXVECTOR3(x, pVertexData[z * sizePerSide + x], z);
            pVertices[index + 1].vPos = D3DXVECTOR3(x, pVertexData[(z + 1) * sizePerSide + x], z + 1);
            pVertices[index + 2].vPos = D3DXVECTOR3(x + 1, pVertexData[z * sizePerSide + x + 1], z);
            pVertices[index + 3].vPos = D3DXVECTOR3(x + 1, pVertexData[(z) * sizePerSide + x + 1], z);
            pVertices[index + 4].vPos = D3DXVECTOR3(x, pVertexData[(z + 1) * sizePerSide + x], z + 1);
            pVertices[index + 5].vPos = D3DXVECTOR3(x + 1, pVertexData[(z + 1) * sizePerSide + x + 1], z + 1);
        }
    // Done
    *ppVertices = pVertices;
    return true;
}

的main.cpp

//////////////UPDATE///////////////////
{
    gDXdevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(160, 200, 255), 1.0f, 0);
    gDXdevice->BeginScene();

        // Get and set the view matrix
        D3DXMATRIX viewMatrix;
        gCamera->CalculateViewMatrix(&viewMatrix);
        gDXdevice->SetTransform(D3DTS_VIEW, &viewMatrix)

        // Draw Heightmap
        gDXdevice->SetFVF(D3DFVF_XYZ);
        gDXdevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, uiTriangleCount, (void*)&pVertices, sizeof(STerrainVector));


        gDXdevice->EndScene();
        gDXdevice->Present(0, 0, 0, 0);}

<pre> <code>
////////////////////////Part in Setup////////////////////////
bool SetupDirect3D(HWND hWnd)
{
    // Standart Directx Init
    gDX3dObject = Direct3DCreate9(D3D_SDK_VERSION);
    if (!gDX3dObject)
        return 0;

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

    presParams.Windowed = TRUE;
    presParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
    presParams.BackBufferFormat = D3DFMT_UNKNOWN;
    presParams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
    presParams.EnableAutoDepthStencil = TRUE;
    presParams.AutoDepthStencilFormat = D3DFMT_D16;

    HRESULT hr = gDX3dObject->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                    D3DCREATE_HARDWARE_VERTEXPROCESSING, &presParams, &gDXdevice);
    if (FAILED(hr))
        return false;

    // Z-Buffer
    gDXdevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

    // Create a Model to View
    hr = D3DXCreateCylinder(gDXdevice, 0.1f, 0.1f, 1.0f, 10, 10, &gCylinderMesh, 0);
    if (FAILED(hr))
        return false;

    hr = D3DXCreatePolygon(gDXdevice, 10.0f, 4, &gPlateMesh, 0);
    if (FAILED(hr))
        return false;


    // Light for Model
    gDXdevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(20, 20, 20));
    gDXdevice->SetRenderState(D3DRS_LIGHTING, TRUE);


    // A light-structure
    D3DLIGHT9 light;
    ZeroMemory(&light, sizeof(D3DLIGHT9));
    light.Type = D3DLIGHT_DIRECTIONAL;
    light.Diffuse.a = 1.0f;
    light.Diffuse.b = 1.0f;
    light.Diffuse.g = 1.0f;
    light.Diffuse.r = 1.0f;
    light.Range = 1000.0f;

    // Direction for Light
    D3DXVECTOR3 vecDir;
    vecDir = D3DXVECTOR3(0.0f, -0.3f, 0.7f);
    D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &vecDir);

    // Turn it on!
    gDXdevice->SetLight(0, &light);
    gDXdevice->LightEnable(0, TRUE);

    // Create World
    hr = LoadMapFromRAW("heightMap.raw", 1024, &pHeightData);
    if (!hr)
        return false;
    hr = CreateVertices(1024, pHeightData, &pVertices, &uiTriangleCount);
    if (!hr)
        return false;

    // Set up a Matrix
    RECT rect;
    GetClientRect(hWnd, &rect);
    D3DXMATRIX matProj;
    float aspect = (rect.right - rect.left) / (float)(rect.bottom - rect.top);
    D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI / 4, aspect, 1.0f, 100.0f);
    gDXdevice->SetTransform(D3DTS_PROJECTION, &matProj);


    return true;
}

....对代码很难......

&#34; * pHeightData; * pVertices; uiTriangleCount&#34;在守则的开头定义。

我希望你能帮助我!

我为糟糕的英语道歉:我是德国人,还在学校里! : - )

提前致谢!

1 个答案:

答案 0 :(得分:1)

所以......我自己解决了。解决方案是:完全重写!对于有相同/类似问题的人:http://www.riemers.net/eng/Tutorials/DirectX/C++/Series1/tut13.php这是我发现的非常好的教程!试试吧!