• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Terrain rendering again...

## Recommended Posts

shakazed    138
I have a slight problem when it gets to rendering my heightmap. I think it´s something wrong with the index buffer since drawindexedprimitive fails. Here´s the code for initializing vertexbuffer and indexbuffer.
CUSTOMVERTEX vertices[128*128];

for(int i = 0; i<128; ++i)
{
for(int j = 0; j<128; ++j)
{

vertices[TERRAIN_Z*i+j].x = (float)j;
vertices[TERRAIN_Z*i+j].y = 0.0f;//iHeightMapArray[i*TERRAIN_Z+j];

vertices[TERRAIN_Z*i+j].z = (float)i;
vertices[TERRAIN_Z*i+j].d3dColor = D3DCOLOR_XRGB(0,255,0);
}
}

if(FAILED(p_iD3DDevice9->CreateVertexBuffer((128*128)*sizeof(CUSTOMVERTEX),
0, CUSTOMVERTEX_FVF, D3DPOOL_DEFAULT,&p_iD3DVertexBuffer9,NULL)))
{
MessageBox(NULL, "Can´t create vertexbuffer","Error", MB_OK);
return false;
}

VOID* pVertices;
if( FAILED(p_iD3DVertexBuffer9->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
return false;

memcpy( pVertices, vertices, sizeof(vertices) );
p_iD3DVertexBuffer9->Unlock();

//Now set up the index buffer

long dIndices[127*127*6];

long dPos = 0;

for(int y = 0;y < 127;++y)
{
dPos = 0;

for(int x = 0;x < 127;++x)
{
dIndices[TERRAIN_Z*y+dPos] = x + y * (TERRAIN_X + 1);
dPos++; //Go next

dIndices[TERRAIN_Z*y+dPos]= x + 1 + y * (TERRAIN_X + 1);					//v2

dPos++; //Go next

dIndices[TERRAIN_Z*y+dPos] = x + 1 + (y + 1) * (TERRAIN_X + 1);				//v4

dPos++; //Go next

dIndices[TERRAIN_Z*y+dPos] = x + y * (TERRAIN_X + 1);					//v1

dPos++; //Go next

dIndices[TERRAIN_Z*y+dPos] = x + 1 + (y + 1) * (TERRAIN_X + 1);				//v4

dPos++; //Go next

dIndices[TERRAIN_Z*y+dPos] = x + (y + 1) * (TERRAIN_X + 1);					//v3

dPos++;
}
}

if(FAILED(p_iD3DDevice9->CreateIndexBuffer((127*127*6)*sizeof(long),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,
&p_iD3DIndexBuffer9,NULL)))
{
MessageBox(NULL,"Failed to create index buffer", "Error", MB_OK);
return false;
}

VOID *p_vData;
if(FAILED(p_iD3DIndexBuffer9->Lock(0,sizeof(dIndices),(VOID**)&p_vData,0)))
{
MessageBox(NULL,"Failed to lock index buffer", "Error", MB_OK);
return false;
}
memcpy(p_vData,dIndices,sizeof(dIndices));					//copy data

p_iD3DIndexBuffer9->Unlock();

return true;

And this is the code I use to present the scene.

p_iD3DDevice9->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255),1.0f,0);

p_iD3DDevice9->BeginScene();
SetupMatrices(); //Setup camera

// Set the location source of our vertex input stream

p_iD3DDevice9->SetStreamSource( 0, p_iD3DVertexBuffer9,0, sizeof(CUSTOMVERTEX));

// Set the location of our index information to our Index Buffer

if(FAILED(p_iD3DDevice9->SetIndices( p_iD3DIndexBuffer9 )))
{
MessageBox(NULL,"Couldn´t set indices", "Error", MB_OK);
return false;
}

if(FAILED(p_iD3DDevice9->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,
0,
0,
127*127*2*3,
0,
127*127*2)))
{
MessageBox(NULL,"Failed to draw indexed primitive", "Error", MB_OK);
return false;
}

p_iD3DDevice9->EndScene();

RECT rRect;
rRect.left = 0;
rRect.top  = 0;
rRect.right = 480;
rRect.bottom = 480;

//Render only from and to the given rect

p_iD3DDevice9->Present(&rRect,&rRect,NULL,NULL);

return true;


Have tried all sorts of things but there´s prolly something wrong in my thinking then Any help REALLY appreciated.

##### Share on other sites
Pipo DeClown    804
Did you just.. crosspost??

shakazed    138
Nope!

##### Share on other sites
higherspeed    230
Check the memcpy lines. Is sizeof() returning what you want?

Once I had a similar problem, because I had got the memcpy bits wrong. Work out the size manually, eg 128 * 128 * sizeof(CUSTOMVERTEX).

Hmm, another potential problem I''ve just spotted is with the indexedprimitive call. Shouldn''t the vertex count be 128 * 128. I think you''ve corrected for both, when you should have just done the indices.

##### Share on other sites
shakazed    138
Changed the vertex count (thanks for noticing) but still nothing happened. The vertices shouldn´t be a problem since I tried only to render them without an index buffer and it worked fine. Hmm...

[edited by - shakazed on July 6, 2003 7:05:20 AM]

##### Share on other sites
higherspeed    230
Ok, so your two numbers shoud be 128*128 and 127*127*2. There are other things that can cause the problems. Have you set the FVF right? I can''t see it anywhere, even if you have put it somewhere else, try moving it to infront of the call, to make it clear that it''s right.

Also should the indices be created as long? I think I''ve always used WORD, which is an unsigned short. If you keep the longs, then perhaps it should be D3DFMT_INDEX32.

Still check the sizeof(vertices), to make sure it''s sensible. Also the sizeof(dIndices) as well. As it''s just an array that shouldn''t be a problem, but it''s worth checking.

Anyway GoodLuck!

##### Share on other sites
shakazed    138
There is something wrong with the vertex creation. Ran a sizeof and it gave me 262144, but it should be (128*128*14)229376 Also changed one thing in the vertex creation. It made two variables get the same value when the first for loop restarted.

[edited by - shakazed on July 6, 2003 8:16:49 AM]

[edited by - shakazed on July 6, 2003 8:21:48 AM]

[edited by - shakazed on July 6, 2003 8:23:52 AM]

##### Share on other sites
higherspeed    230
The value given by sizeof could be right. I think the struct always has a value divisible by 4. So the actual sizeof your vertex is 16.

Have you checked the format of the indices though? Try using WORDs, see if that helps.

##### Share on other sites
shakazed    138
Changed to Word, didn´t help of course The fvf I use are diffuse and xyz. Posted the project on my site. it´s under the projects tab.

##### Share on other sites
higherspeed    230
The debug runtimes give me these two error, one we already know about.

Direct3D9: (ERROR) :Invalid index in the index stream: 52428
Direct3D9: (ERROR) :DrawIndexedPrimitive failed.

Now debugging the project I found that the indices were set to 52428 on initialisation. Using ZeroMemory on the array is a simple hack to stop the error occuring. There''s still something wrong, but it''s at least partly working and now we know the problem is with the loop assigning the indices, it''s missing one out somewhere.

On further inspection I''ve seen that you''ve missed out a *6, from the end of each TERRAIN_Z*y, which is where you''re not assigning an index.

##### Share on other sites
shakazed    138
YAAY!!! Got it wokring. Higherspeed, I´m in great debt. I don´t have much to offer but you can marry my firstborn if you like (whenever I get a squirt that is) Seriously thank you REALLY much for taking the time.