Forum
Contact





DirectX using C#
DirectX using C++
DirectX using Visual Basic



Latest Forum posts

 Tutorial 3 for Windows Phone 7
  Posted by: Anonymous
  When: 20/05/2013 at 02:30:13

 No download link for 2d series: shooter
  Posted by: zaboleq
  When: 07/05/2013 at 15:46:28

 Collision Class?
  Posted by: Anonymous
  When: 05/05/2013 at 19:03:59

 stack overflow
  Posted by: cityguy
  When: 07/04/2013 at 01:58:38

 Meshes looks strange.
  Posted by: ab_saratov
  When: 01/04/2013 at 04:31:08

 Lamppost Not loaded
  Posted by: Anonymous
  When: 22/03/2013 at 06:43:52

 Collision Class?
  Posted by: Da_Boom
  When: 21/03/2013 at 01:23:09

 Math boggles me
  Posted by: cityguy
  When: 17/03/2013 at 03:44:48

 Collision Class?
  Posted by: Da_Boom
  When: 16/03/2013 at 03:44:42

 Tree update
  Posted by: Anonymous
  When: 15/03/2013 at 21:11:22

 XNA 4.0
  Posted by: Anonymous
  When: 15/03/2013 at 19:43:57

 Error when I try to run.
  Posted by: Anonymous
  When: 15/03/2013 at 19:21:06

 Error With the Effect File
  Posted by: Anonymous
  When: 15/03/2013 at 18:21:01

 Can only get shadowmap
  Posted by: Anonymous
  When: 15/03/2013 at 15:48:52

 Vertex and Pixel Shader Versions?
  Posted by: Anonymous
  When: 15/03/2013 at 15:07:16

 Unsupported properties
  Posted by: Anonymous
  When: 15/03/2013 at 14:23:00

 Problem Loading Skybox
  Posted by: Rana
  When: 15/03/2013 at 10:34:45

 Black Screen Of Death - Help!
  Posted by: Anonymous
  When: 15/03/2013 at 03:43:43

 2.0 anyone?
  Posted by: Anonymous
  When: 15/03/2013 at 02:19:48

 Defitinition of tha rotation axis
  Posted by: Anonymous
  When: 15/03/2013 at 00:55:14




Topic: Error in Indices Loop Code?



  
Goto parent category
  
Create a new user account


   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?

  
Post a new reply
 





Google
 
Web www.riemers.net
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


News
Home
Forum
XNA 2.0 Recipes Book (8)
XNA 3.0 Recipes Book (8)
Downloads
Extra Reading (3)
Matrices: geometrical
Matrix Mathematics
Homogenous matrices
Community Projects (1)
Tutorials (160)
XNA 4.0 using C# (89)
DirectX using C# (54)
Series 1:Terrain (14)
Opening a window
Linking to the Device
Drawing a triangle
Camera
Rotation - Translation
Indices
Terrain creation
Terrain from file
DirectInput
Importing bmp files
Colored vertices
DirectX Light basics
Mesh creation
Mesh lighting
Series 2: Flightsim (19)
Series 3: HLSL (19)
Short Tuts (2)
Resizing problem
Checking Device caps
DirectX using C++ (15)
DirectX using VB (2)