heightmap

This topic is 5408 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am working on a heightmap, first i am trying to render it flat, but i am having trouble. i cant even make a 3x3 My render looks like this
void Terrain::draw(){
// plassering
D3DXMATRIX matWorld;
D3DXMatrixIdentity(&matWorld);
g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );

g_pd3dDevice->SetMaterial( material );
g_pd3dDevice->SetStreamSource( 0, vb, 0, sizeof(Vertex) );
g_pd3dDevice->SetIndices(ib);
g_pd3dDevice->SetFVF( VERTEXFORMAT );

//g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, w*h, 0, (w-1)*(h-1)*2);
g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 9, 0, 8);
}


I build my Vertex buffer and index buffer like this
void Terrain::init(int ww, int hh){
w = ww;
h = hh;
data = new char[w*h];
}

void Terrain::generate(){
Vertex *vdata;      // pointer to the data in th VertexBuffer
int *idata;         // pointer to the index data;
int i = 0;
// create vb
g_pd3dDevice->CreateVertexBuffer(w*h*sizeof(Vertex), 0, VERTEXFORMAT, D3DPOOL_DEFAULT, &vb, NULL);
// create IndexBuffer
g_pd3dDevice->CreateIndexBuffer((w-1)*(h-1)*6, 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &ib, NULL);
// legge på plass verticene og fylle data
vb->Lock( 0, 0, (void**)&vdata, 0 );
for(int x=0; x < w; x++){
for(int y=0; y < h; y++){
data[x+y*w] = rand()%255;
vdata[x+y*w].diffuse = 0xFFFFFF44;
vdata[x+y*w].position.x = (float)-w/2.0f+(float)x*4.0f;
vdata[x+y*w].position.y = 2.0f;//data[x+y*w]*0.1f;
vdata[x+y*w].position.z = (float)-h/2.0f+(float)y*4.0f;;
vdata[x+y*w].normal.x = 0;
vdata[x+y*w].normal.y = 1;
vdata[x+y*w].normal.z = 0;
}
}
vb->Unlock();
// lage indexarray
ib->Lock( 0, 0, (void**)&idata, 0 );
for(int x=0; x < w-1; x++){
for(int y=0; y < h-1; y++){
idata[i++] = x+y*w;
idata[i++] = x+y*w+1;
idata[i++] = x+y*w+w;

idata[i++] = x+y*w+w+1;
idata[i++] = x+y*w+w;
idata[i++] = x+y*w+1;
}
}
ib->Unlock();
}


The result should be something like
_______
|/|/|/|
|/|/|/|
|/|/|/|

__
|/_
| /
|/


cant figure out what is wrong, Tried writing all the data to file and "render" it on paper, and everything is the way it should be there :(

Share on other sites

Try this approch:

Vertex *vdata = NULL;
:

lock(..)
loop
{
vdata->diffuse = 0xFFFFFF44;
vdata->position.x = (float)-w/2.0f+(float)x*4.0f;
:
vdata++;
}
unlock(..)

Share on other sites
if w and h are in terms of quads, then:

g_pd3dDevice->CreateVertexBuffer(w*h*sizeof(Vertex)...
should be:
g_pd3dDevice->CreateVertexBuffer((w+1)*(h+1))*sizeof(Vertex)...
because this is the number of vertices (and you're using indices), not the number of quads.

and:
g_pd3dDevice->CreateIndexBuffer((w-1)*(h-1)*6...
should be:
g_pd3dDevice->CreateIndexBuffer(w*h*6...

The looping routine looks right at first glance. Show us the code you use to render the primitives.

Chris

Share on other sites
Tried Milles suggestion, with no sucess. Got the same result.

The rendering code is in the first codeblock in original post.

wrote all the data to a file and it looks like this

Vertexbuffer0 -1.5 2 -1.51 2.5 2 -1.52 6.5 2 -1.53 -1.5 2 2.54 2.5 2 2.55 6.5 2 2.56 -1.5 2 6.57 2.5 2 6.58 6.5 2 6.5Indexbuffer 0 1 3 4 3 1 1 2 4 5 4 2 3 4 6 7 6 4 4 5 7 8 7 5

[Edited by - zauron on September 27, 2004 4:37:11 AM]

Share on other sites
if anyone is interested i fixed it, the problem was i used a int pointer to my indexbuffer, but the indexes was only 16bit...

Changing it to 32 bit or the int to WORD and i get just what i want

• 21
• 16
• 9
• 17
• 13