简体   繁体   English

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

[英]C++, DirecX 9: Render Self-made Vertex Struct

I'm new on StackOverflow and this is my first question. 我是StackOverflow的新手,这是我的第一个问题。 I'm very new to C++ and DirectX9 so their may be a lot of Issues in my Code! 我是C ++和DirectX9的新手,所以在我的代码中可能是很多问题!

My Question: I would like to Render an Structure of "D3DXVECTOR3"s for making a Heightmap from a .raw. 我的问题:我想渲染“ D3DXVECTOR3”的结构,以便从.raw生成高度图。

I have a working FP Camera System and want to implement a Heightmap. 我有一个正常工作的FP摄像头系统,并且想要实现一个Heightmap。 I followed the Tutorial "Terrain in DirectX9 and C++" ( https://www.tutorials.de/threads/tutorial-terrain-in-directx-9-und-c-teil-1.343473/ ), but the Code don't work properly (The Tutorial is in "Bad" German, I used It because I didn't found any Tutorial for a Heightmap in DirectX9. ...And I'm german). 我遵循了“ DirectX9和C ++中的地形”教程( https://www.tutorials.de/threads/tutorial-terrain-in-directx-9-und-c-teil-1.343473/ ),但该代码没有正常工作(该教程使用的是“ Bad”德语),我之所以使用它,是因为我在DirectX9中没有找到有关Heightmap的任何教程。...而且我是德语。

All it output is This (Ignore the Cylinder, it's only a test): renderissue 它输出的全部是This(忽略Cylinder,这只是一个测试): renderissue

Here are some relevant Parts of the Code: 以下是守则的一些相关部分:

Heightmap.cpp 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 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;
}

....Had a Hard time with the Code..... ....在《准则》中度过了艰难的时光.....

The "*pHeightData; *pVertices; uiTriangleCount" are defined at the start of the Code. 在代码开头定义了“ * pHeightData; * pVertices; uiTriangleCount”。

I Hope you can help me! 我希望你能帮帮我!

I apologize for the the bad english: I'm German and still in School! 我为英语不好而道歉:我是德国人,还在上学! :-) :-)

Thanks in Advance! 提前致谢!

So... I solved by myself. 所以...我自己解决了。 The solution was: Complete rewrite! 解决方案是:完全重写! For anyone with the same/similar problem: http://www.riemers.net/eng/Tutorials/DirectX/C++/Series1/tut13.php That is a very good Tutorial I've found! 对于有相同/相似问题的任何人: http : //www.riemers.net/eng/Tutorials/DirectX/C++/Series1/tut13.php这是我找到的非常好的教程! Try It! 试试吧!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM