Skip to main content
GameDev.net gamedev.net
🔒 Locked

Height map

Started by Feaerenil Jun 1, 2005 at 7:52 AM 0 replies 950+ views
Original Post
Feaerenil
Feaerenil
Hello everyone! I have a problem with the Height map realisation. unfortunately, in net are only OpenGL height map samples (I didn't find for DX), and I can't port in to DX9 because my knowladge in OpenGL is..modest when I try to fill Vertex and Index buffers with coordinates from map file, I always get a crush I'm in the dead end can enyone show me little example, just loading height map from .RAW-format file, fill it into vertex and index buffers and then render? and please make detailed commentaries ! thousand of thousands of thanks :)
renqilin
renqilin
here is something i wrote long time ago. there is no comment in the code, for i'm lazy.. i hope this can help you.

BW3DCORE_API BOOL CTerrain::Create(                        LPDIRECT3DDEVICE8 pD3DDevice,                         const SMeshMapParam& param,                         const CHeightMap* pHMap,                         const CColorMap* pCrMap){    bw_assert(pHMap != NULL && pHMap->m_pHeightMap != NULL);#if (BW_DEBUG == BW_TRUE)    if (pCrMap != NULL)    {        bw_assert(pCrMap != NULL && pCrMap->m_pColorMap != NULL);        bw_assert(pHMap->m_nWidth == pCrMap->m_nWidth);        bw_assert(pHMap->m_nHeight == pCrMap->m_nHeight);    }#endif    bw_assert(pHMap->m_nWidth >= 2);    bw_assert(pHMap->m_nHeight >= 2);    bw_assert(param.fMeshWidth >= 0.0f);    bw_assert(param.fMeshHeight >= 0.0f);    bw_assert(param.fScale >= 0.0f);    Release();    int m, n;    const int nWidth  = pHMap->m_nWidth;    const int nHeight = pHMap->m_nHeight;    m_nXBlock   = nWidth - 1;    m_nZBlock   = nHeight - 1;    const int nBlockCount = m_nXBlock * m_nZBlock;    const int cx         = nWidth / 2;    const int cz         = nHeight / 2;    const BW_Vector3 ptOrigin(0.0f, 0.0f, 0.0f);    STerrainBlock<Vertex_PNDT>* pBlocks =         BW_NEW STerrainBlock<Vertex_PNDT>[nBlockCount];    STerrainIndexBlock* pIndexBlock =         BW_NEW STerrainIndexBlock[nBlockCount];    m_pHeightMap = BW_NEW STerrainHeightBlock[nBlockCount];    for (n = 0; n < m_nZBlock; ++n)    {        for (m = 0; m < m_nXBlock; ++m)        {            const int nCurrentBlock = n*m_nXBlock + m;            Vertex_PNDT* const v = pBlocks[nCurrentBlock].m_v;            BW_Index* const index = pIndexBlock[nCurrentBlock].m_i;            bw_real* const heights = m_pHeightMap[nCurrentBlock].h;            heights[0] =                 ptOrigin.y +                 ((bw_real)(pHMap->m_pHeightMap[(n)*nWidth + m])) /                 param.fScale;            heights[1] =                 ptOrigin.y +                 ((bw_real)(pHMap->m_pHeightMap[(n)*nWidth + m + 1])) /                 param.fScale;            heights[2] =                 ptOrigin.y +                 ((bw_real)(pHMap->m_pHeightMap[(n+1)*nWidth + m + 1])) /                 param.fScale;            heights[3] =                 ptOrigin.y +                 ((bw_real)(pHMap->m_pHeightMap[(n+1)*nWidth + m])) /                 param.fScale;            v[0].pos.x  = ptOrigin.x + (m - cx) * param.fMeshWidth;            v[0].pos.y  = heights[0];            v[0].pos.z  = ptOrigin.z + (cz - n) * param.fMeshHeight;            v[1].pos.x  =                 ptOrigin.x +                 (m - cx) * param.fMeshWidth +                 param.fMeshWidth;            v[1].pos.y  = heights[1];            v[1].pos.z  = v[0].pos.z;            v[2].pos.x  = v[1].pos.x;            v[2].pos.y  = heights[2];            v[2].pos.z  =                 ptOrigin.z +                 (cz - n) * param.fMeshHeight -                 param.fMeshHeight;            v[3].pos.x  = v[0].pos.x;            v[3].pos.y  = heights[3];            v[3].pos.z  = v[2].pos.z;            v[0].tex.x  = 0.0f;            v[0].tex.y  = 0.0f;            v[1].tex.x  = 1.0f;            v[1].tex.y  = 0.0f;            v[2].tex.x  = 1.0f;            v[2].tex.y  = 1.0f;            v[3].tex.x  = 0.0f;            v[3].tex.y  = 1.0f;            BW_Vector3 v1, v2;            D3DXVec3Subtract(&v1, &v[1].pos, &v[0].pos);            D3DXVec3Subtract(&v2, &v[3].pos, &v[0].pos);            D3DXVec3Cross(&v[0].nor, &v1, &v2);            D3DXVec3Normalize(&v[0].nor, &v[0].nor);            D3DXVec3Subtract(&v1, &v[2].pos, &v[1].pos);            D3DXVec3Subtract(&v2, &v[0].pos, &v[1].pos);            D3DXVec3Cross(&v[1].nor, &v1, &v2);            D3DXVec3Normalize(&v[1].nor, &v[1].nor);            D3DXVec3Subtract(&v1, &v[3].pos, &v[2].pos);            D3DXVec3Subtract(&v2, &v[1].pos, &v[2].pos);            D3DXVec3Cross(&v[2].nor, &v1, &v2);            D3DXVec3Normalize(&v[2].nor, &v[2].nor);            D3DXVec3Subtract(&v1, &v[0].pos, &v[3].pos);            D3DXVec3Subtract(&v2, &v[2].pos, &v[3].pos);            D3DXVec3Cross(&v[3].nor, &v1, &v2);            D3DXVec3Normalize(&v[3].nor, &v[3].nor);            if (pCrMap != NULL)            {                v[0].clr = pCrMap->m_pColorMap[n * nWidth + m];                v[1].clr = pCrMap->m_pColorMap[n * nWidth + m + 1];                v[2].clr = pCrMap->m_pColorMap[(n + 1) * nWidth + m + 1];                v[3].clr = pCrMap->m_pColorMap[(n + 1) * nWidth + m];            }            else            {                v[0].clr = 0xFFFFFFFF;                v[1].clr = 0xFFFFFFFF;                v[2].clr = 0xFFFFFFFF;                v[3].clr = 0xFFFFFFFF;            }            const int nCurrentVertex = nCurrentBlock * 4;            index[0]    = nCurrentVertex;            index[1]    = nCurrentVertex + 1;            index[2]    = nCurrentVertex + 2;            index[3]    = nCurrentVertex;            index[4]    = nCurrentVertex + 2;            index[5]    = nCurrentVertex + 3;        }    }    // average normals of every adjacent plane.    for (n = 0; n < m_nZBlock; ++n)    {        for (m = 0; m < m_nXBlock; ++m)        {            const int nCurrentBlock = n*m_nXBlock + m;            const int nTopBlock = (n-1)*m_nXBlock + m;            const int nLeftBlock = n*m_nXBlock + m -1;            const int nLeftTopBlock = (n-1)*m_nXBlock+m-1;            Vertex_PNDT* v = &pBlocks[nCurrentBlock].m_v[0];            Vertex_PNDT* lV;            Vertex_PNDT* tV;            Vertex_PNDT* ltV;            if (n > 0 && m > 0)            {                lV  = &pBlocks[nLeftBlock].m_v[1];                tV  = &pBlocks[nTopBlock].m_v[3];                ltV = &pBlocks[nLeftTopBlock].m_v[2];                D3DXVec3Add(&(v->nor), &(v->nor), &(lV->nor));                D3DXVec3Add(&(v->nor), &(v->nor), &(tV->nor));                D3DXVec3Add(&(v->nor), &(v->nor), &(ltV->nor));                D3DXVec3Normalize(&(v->nor), &(v->nor));            }            else if (m == 0 && n > 0)            {                tV  = &pBlocks[nTopBlock].m_v[3];                D3DXVec3Add(&(v->nor), &(v->nor), &(tV->nor));                D3DXVec3Normalize(&(v->nor), &(v->nor));            }            else if (n == 0 && m > 0)            {                lV  = &pBlocks[nLeftBlock].m_v[1];                D3DXVec3Add(&(v->nor), &(v->nor), &(lV->nor));                D3DXVec3Normalize(&(v->nor), &(v->nor));            }        }    }    if (!CDirect3DMesh::Create(            pD3DDevice,             nBlockCount * 4,             nBlockCount * 6,             D3DPT_TRIANGLELIST,             Vertex_PNDT::fvf,             sizeof(Vertex_PNDT)))    {        delete[] pBlocks;        delete[] pIndexBlock;        Release();        return FALSE;    }    if (!CDirect3DMesh::SetData(            pBlocks, reinterpret_cast<BW_Index*>(pIndexBlock)))    {        delete[] pBlocks;        delete[] pIndexBlock;        Release();        return FALSE;    }    delete[] pBlocks;    delete[] pIndexBlock;    return TRUE;}


[Edited by - Coder on June 1, 2005 12:03:23 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.