我是StackOverflow的新手,这是我的第一个问题。
我是C++和DirectX9的新手,所以在我的代码中可能是很多问题!

我的问题:
我想渲染“D3DXVECTOR3”的结构,以便从.raw生成高度图。

我有一个正常工作的FP摄像头系统,并且想要实现一个Heightmap。我遵循了教程“DirectX9和C++中的地形”(https://www.tutorials.de/threads/tutorial-terrain-in-directx-9-und-c-teil-1.343473/),但是代码无法正常工作(该教程使用的语言是“Bad”德语,我之所以使用它,是因为我没有在DirectX9中找到关于Heightmap的任何教程。 ...而且我是德国人)。

它输出的全部是This(忽略Cylinder,这只是一个测试):
renderissue

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

高度图

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;
}

....在《准则》中度过了艰难的时光.....

在代码开头定义了“* pHeightData; * pVertices; uiTriangleCount”。

我希望你可以帮助我!

我为英语不好而道歉:我是德国人,还在上学! :-)

提前致谢!

最佳答案

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

09-13 03:58