| Topic: Error in Indices Loop Code?
|
|
 | Error in Indices Loop Code? | |  |
| Poster | : PointMan | | Posts | : 4 | | Country | : Canada | | City | : Toronto |
| | | | Posted by PointMan on 11/08/2008 at 23:35:59
| | I'm somewhat of a DirectX noob, although I have tinkered with it for several years, and I'm also a professional software dev (.Net and Java). That said, I think I've found an issue with the code in this tutorial, although I certainly could be mistaken.
Using the code provided, I cannot get anything better than a line to draw. I have found the reason to be erronous indices in the IndexBuffer.
The following code snippet is the vertices loop from the tutorial:
for (int x=0;x< WIDTH;x++) {
for (int y=0; y< HEIGHT;y++) {
vertices[x+y*WIDTH].Position = new Vector3(x, y, heightData[x,y]);
vertices[x+y*WIDTH].Color = Color.White.ToArgb();
}
}
|
Using the above loop (and ignoring z-coordinate [height] for simplicity), I get the following 12 vectors which I believe are correct (note that the index is not in order, I've put it in the order that the 'cursor' progresses through the loop):
vertices[0] = (0, 0, ..)
vertices[4] = (0, 1, ..)
vertices[8] = (0, 2, ..)
vertices[1] = (1, 0, ..)
vertices[6] = (1, 1, ..)
vertices[9] = (1, 2, ..)
vertices[2] = (2, 0, ..)
vertices[6] = (2, 1, ..)
vertices[10] = (2, 2, ..)
vertices[3] = (3, 0, ..)
vertices[7] = (3, 1, ..)
vertices[11] = (3, 2, ..)
Using the LH coordinate system, which the camera is using, these will map to the following points onto the screen:

The problem lies in the indices loop. The following is pulled directly out of the debugger, setting a watch on the indices array:
[0] 5 int
[1] 1 int
[2] 0 int
[3] 5 int
[4] 0 int
[5] 4 int
[6] 6 int
[7] 2 int
[8] 1 int
[9] 6 int
[10] 1 int
[11] 5 int
[12] 7 int
[13] 3 int
[14] 2 int
[15] 7 int
[16] 2 int
[17] 6 int
[18] 9 int
[19] 5 int
[20] 4 int
[21] 9 int
[22] 4 int
[23] 8 int
[24] 10 int
[25] 6 int
[26] 5 int
[27] 10 int
[28] 5 int
[29] 9 int
[30] 11 int
[31] 7 int
[32] 6 int
[33] 11 int
[34] 6 int
[35] 10 int
As you should be able to tell from the first 2 triple-sets, the triangles those sets of indices form are certainly not the desired result. 5-0-4 is not even a triangle used.
I manually created the 36 indices to follow the pattern shown in the tutorial screenshot, and I was able to reproduce the desired pattern myself. This further indicates that the indices are erroneous.
Analyzing the indices loop and it's results showed me that the equations/formulae were correct, but the "mapped" index order was wrong. Here is how I modified the indices loop to be correct, starting with the old code, commented out with the new index 'offset' given:
//indices[(x + y * (WIDTH - 1)) * 6] = (x + 1) + (y + 1) * WIDTH; // Set as 1
//indices[(x + y * (WIDTH - 1)) * 6 + 1] = (x + 1) + y * WIDTH; // Set as 2
//indices[(x + y * (WIDTH - 1)) * 6 + 2] = x + y * WIDTH; // Set as 0
//indices[(x + y * (WIDTH - 1)) * 6 + 3] = (x + 1) + (y + 1) * WIDTH; // Set as 5
//indices[(x + y * (WIDTH - 1)) * 6 + 4] = x + y * WIDTH; // Set as 3
//indices[(x + y * (WIDTH - 1)) * 6 + 5] = x + (y + 1) * WIDTH; // Set as 4
indices[(x + y * (WIDTH - 1)) * 6 + 1] = (x + 1) + (y + 1) * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 2] = (x + 1) + y * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6] = x + y * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 5] = (x + 1) + (y + 1) * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 3] = x + y * WIDTH;
indices[(x + y * (WIDTH - 1)) * 6 + 4] = x + (y + 1) * WIDTH;
|
This now works for me. Not really sure how this ended up like this, or why noone else found the issue.
| |
|
| | | | | | Poster | : PointMan | | Posts | : 4 | | Country | : Canada | | City | : Toronto |
| | | | Posted by PointMan on 11/08/2008 at 23:50:31
| | Oops, made one slight mistake above..
Turns out the triangles created by those triple-sets WERE real triangles, but they were not drawing clockwise relative to the camera. Isn't this a problem? I know setting cull mode to 'none' is supposed to help this, but it didn't seem to change anything for me.
I'll level with you about one thing... I'm actually porting this code for use with SlimDX, the new alternative for MDX (now that Microsoft has abandoned it), created by the SlimDX Group. I suppose the cause of this whole this is that the tutorial is having the triangles drawn backwards, and that SlimDX won't draw triangles with coordinates 'backwards', even with culling off. Regardless, I figured my findings might be helpful to others. | |
|
| | | | | | Poster | : PointMan | | Posts | : 4 | | Country | : Canada | | City | : Toronto |
| | | | Posted by PointMan on 11/08/2008 at 23:55:40
| | Heh, one more mistake (an edit button would be nice.. or maybe I should stop posting pre-maturely :p)
The line:
vertices[6] = (1, 1, ..)
Should actually read:
vertices[5] = (1, 1, ..)
The image is still correct though. | |
|
| | | | | | Poster | : PointMan | | Posts | : 4 | | Country | : Canada | | City | : Toronto |
| | | | Posted by PointMan on 12/08/2008 at 08:58:14
| | Haha, I was really out of it last night..
This topic should be moved to Tutorial 7: Terrain Creation. | |
|
| | | | | | Poster | : riemer | | Posts | : 1392 | | Country | : Belgium | | City | : Antwerp |
| | | | Posted by riemer on 13/08/2008 at 14:50:56
| | Wow, quite some information ...
Well you definately seem to understand the concepts behind indices and culling. But to be honest, I have no clue to what the problem might be related. I have tested the code, and here it works. Maybe it's related to your version of MDX ... is it also left-handed such as MDX, or right-handed such as XNA? | |
|
|
 | | |  |
|
|
|
If you appreciate the amount of time I spend creating and updating these pages, feel free to donate -- any amount is welcome !
|
- Website design & DirectX code : Riemer Grootjans - ©2006 Riemer Grootjans
|
|