Directx draw heightmap parts problem

Started by
-1 comments, last by -Tau- 12 years, 9 months ago
Hi, i am trying to draw a heightmap. When i want to draw the entire map it works but when i try to draw its parts it draws only 1 row. I have 1 vertex buffer, 1 index buffer for entire map and 2d array of index buffers for its parts. Which part should be drawn its determined by cameras position.
code:

struct MapPart
{
LPDIRECT3DINDEXBUFFER9 pIB;
word xw;
word zh;
};

class HeightMap
{
public:
float **map;
word x_width;
word z_height;

LPDIRECT3DTEXTURE9 Texture;
LPDIRECT3DVERTEXBUFFER9 pVB;

LPDIRECT3DINDEXBUFFER9 pIB;
bool typmapy;

MapPart **mp;
word mpxw;
word mpzh;

void ReadMap(char*);
float gh_ABD_BCD(float abx, float abz);
float gh_ABC_ACD(float aax, float aaz);

void drawmap(ID3DXEffect*,IDirect3DDevice9*,float pozcx, float pozcz);

HeightMap();
HeightMap(char*,IDirect3DDevice9* pd3dDevice);
HeightMap(word,word);
~HeightMap();
};

HeightMap::HeightMap(char *fname, IDirect3DDevice9* pd3dDevice)
{
FILE *f=NULL;
f=fopen(fname,"rb");
if (!f)
{
x_width=0;
z_height=0;
map=NULL;
return;
}
int a=0,b=0;
fread(&x_width, sizeof(word),1, f);
fread(&z_height, sizeof(word),1, f);

map = new float*[x_width];
for (int i = 0; i < x_width; ++i) map = new float[z_height];

for (int i=0; i<x_width; i++)
for (int j=0; j<z_height; j++) map[j]=1;
CUSTOMVERTEX *Vertices;
UINT *Indices;
Vertices = new CUSTOMVERTEX[x_width*z_height];

// Create the vertex buffer.
if( FAILED( pd3dDevice->CreateVertexBuffer( x_width*z_height* sizeof( CUSTOMVERTEX ),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVB, NULL ) ) )
{
return;
}

Indices = new UINT[(x_width-1)*(z_height-1)*6];
if( FAILED( pd3dDevice->CreateIndexBuffer( (x_width-1)*(z_height-1)*6 *sizeof(UINT),
D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT,
&pIB, NULL ) ) )
{
return;
}

VOID *pVertices;
if( FAILED( pVB->Lock( 0, 0, ( void** )&pVertices, 0 ) ) )
return;

do
{
fread(&map[a], sizeof(float),1, f);
Vertices[a*x_width+b].x=a;
Vertices[a*x_width+b].y=map[a];
Vertices[a*x_width+b].z=b;
Vertices[a*x_width+b].tu=a%2;
Vertices[a*x_width+b].tv=b%2;
a++;
if (a==x_width) {b++;a=0;}
}
while(b<z_height);
memcpy( pVertices, Vertices, sizeof(CUSTOMVERTEX)*x_width*z_height );
pVB->Unlock();
delete []Vertices;

VOID* pIndices;

if( FAILED( pIB->Lock( 0, sizeof(UINT)*(x_width-1)*(z_height-1)*6, (VOID**)&pIndices, 0) ) )
{
SAFE_RELEASE(pIB);
return;
}

int k=0;
for (int i=0; i<x_width-1; i++)
for (int j=0; j<z_height-1; j++)
{
Indices[k]=i*x_width+j;
k++;
Indices[k]=i*x_width+j+1;
k++;
Indices[k]=(i+1)*x_width+j;
k++;

Indices[k]=(i+1)*x_width+j;
k++;
Indices[k]=i*x_width+j+1;
k++;
Indices[k]=(i+1)*x_width+j+1;
k++;
}
memcpy( pIndices, Indices, sizeof(UINT)*(x_width-1)*(z_height-1)*6);
pIB->Unlock();
delete []Indices;

fclose(f);

/********************************************/
mpxw=x_width/mdt;
if (x_width%mdt!=0) mpxw++;
mpzh=z_height/mdt;
if (z_height%mdt!=0) mpzh++;

mp = new MapPart*[mpxw];
for (int i = 0; i < mpxw; ++i) mp = new MapPart[mpzh];

for (int i=0; i<mpxw; i++)
for (int j=0; j<mpzh; j++)
{
if (i<mpxw-1) mp[j].xw=mdt;
else mp[j].xw=x_width%mdt;
if (j<mpzh-1) mp[j].zh=mdt;
else mp[j].zh=z_height%mdt;

pd3dDevice->CreateIndexBuffer( (mp[j].xw-1)*(mp[j].zh-1)*6 *sizeof(UINT),
D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT,
&mp[j].pIB, NULL );

Indices = new UINT[(mp[j].xw-1)*(mp[j].zh-1)*6];
mp[j].pIB->Lock( 0, sizeof(UINT)*(mp[j].xw-1)*(mp[j].zh-1)*6, (VOID**)&pIndices, 0 );
k=0;
for (int m=i*mdt; m<i*mdt+mp[j].xw-1; m++)
for (int n=j*mdt; n<j*mdt+mp[j].zh-1; n++)
{
Indices[k]=m*x_width+n;
k++;
Indices[k]=m*x_width+n+1;
k++;
Indices[k]=(m+1)*x_width+n;
k++;

Indices[k]=(m+1)*x_width+n;
k++;
Indices[k]=m*x_width+n+1;
k++;
Indices[k]=(m+1)*x_width+n+1;
k++;

}
memcpy( pIndices, Indices, sizeof(UINT)*(mp[j].xw-1)*(mp[j].zh-1)*6);
mp[j].pIB->Unlock();
delete []Indices;
}

/********************************************/
Texture=NULL;

if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, L"banana.bmp", &Texture ) ) )
{
// If texture is not in current folder, try parent folder
if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, L"..\\banana.bmp", &Texture ) ) )
{
MessageBox( NULL, L"Could not find banana.bmp", L"Textures.exe", MB_OK );
return;
}
}
}

void HeightMap::drawmap(ID3DXEffect* g_pEffect, IDirect3DDevice9* pd3dDevice, float pozcx, float pozcz)
{
if (pozcx<0 || pozcx>x_width) return;
if (pozcz<0 || pozcz>z_height) return;
UINT iP,cP;
HRESULT hr;
int mpdx=(int)pozcx/mdt;
int mpdz=(int)pozcz/mdt;
pd3dDevice->SetStreamSource( 0, pVB, 0, sizeof( CUSTOMVERTEX ) );
pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
if (typmapy) pd3dDevice->SetIndices(pIB);
else pd3dDevice->SetIndices(mp[mpdx][mpdz].pIB);

V( g_pEffect->Begin( &cP, 0 ) );
for( iP = 0; iP < cP; iP++ )
{
V( g_pEffect->BeginPass( iP ) );
pd3dDevice->SetTexture( 0, Texture );
if (typmapy) pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0,
0,
x_width*z_height,
0,
(x_width-1)*(z_height-1)*2);
else
pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0,
0,
mp[mpdx][mpdz].xw*mp[mpdx][mpdz].zh,
0,
(mp[mpdx][mpdz].xw-1)*(mp[mpdx][mpdz].zh-1)*2);


g_pEffect->EndPass();
}
g_pEffect->End();
return;
}


problem:
what should be drawed, what is drawed

This topic is closed to new replies.

Advertisement