Reputation: 5
I am having trouble generating a flat terrain with quads, I think the quads are located correctly but the indices are incorrect.. Can someone please have a look see and tell me what i am doing wrong or how i can fix it? Basically the terrain is displayed but totally wrong. I THINK it is the indices positioning, please help me it would be highly appreciated.
FOR LOOP:
for(int i = 0; i < NUM_VERTS; i += 4){
for(int x = 0; x < j; x ++){
for(int z = 0; z < j; z ++){
verts[i] = D3DVertex::VertexPositionNormalTexture(x, -1.0f, z+1, n, 1.0f, g, 0.0f, 0.0f);
verts[i+1] = D3DVertex::VertexPositionNormalTexture(x+1, -1.0f, z+1, n, 1.0f, g, 1.0f, 0.0f);
verts[i+2] = D3DVertex::VertexPositionNormalTexture(x, -1.0f, z, n, 1.0f, g, 0.0f, 1.0f);
verts[i+2] = D3DVertex::VertexPositionNormalTexture(x+1, -1.0f, z, n, 1.0f, g, 0.0f, 1.0f);
indices[i] = i;
indices[i+1] = i+1;
indices[i+2] = i+2;
indices[i+3] = i+2;
indices[i+4] = i+1;
indices[i+5] = i+3;
}
}
//MessageBox(NULL, L"Test", NULL, NULL);
}
Upvotes: 0
Views: 447
Reputation: 32627
Your outer-most loop does not make sense. Instead, maintain two indices for the vertex buffer and the index buffer like so:
int iVertex = 0;
int iIndex = 0;
for(int x = 0; x < j; x ++){
for(int z = 0; z < j; z ++){
verts[iVertex ] = D3DVertex::VertexPositionNormalTexture(x, -1.0f, z+1, n, 1.0f, g, 0.0f, 0.0f);
verts[iVertex+1] = D3DVertex::VertexPositionNormalTexture(x+1, -1.0f, z+1, n, 1.0f, g, 1.0f, 0.0f);
verts[iVertex+2] = D3DVertex::VertexPositionNormalTexture(x, -1.0f, z, n, 1.0f, g, 0.0f, 1.0f);
verts[iVertex+3] = D3DVertex::VertexPositionNormalTexture(x+1, -1.0f, z, n, 1.0f, g, 0.0f, 1.0f);
indices[iIndex ] = iVertex;
indices[iIndex+1] = iVertex+1;
indices[iIndex+2] = iVertex+2;
indices[iIndex+3] = iVertex+2;
indices[iIndex+4] = iVertex+1;
indices[iIndex+5] = iVertex+3;
iVertex += 4;
iIndex += 6;
}
}
I assume that you're aware that the terrain will stretch from 0
to j + 1
in both x
and z
direction.
If it's just a plane, you might also consider using a single quad. If you need multiple triangles, you might consider converting the triangle list to a triangle strip. If you want to stick to the triangle list, you should at least consider removing the duplicate vertices (every vertex in the terrain's center exists four times).
Upvotes: 1