# Indices and me probably being an idiot

## Recommended Posts

Alright I'm having an issue with indices and freaking drawing a simple cube. Either me or the indices are being retarded about this. Its probably me. I think maybe I got the addition for coordinates messed up.

[code] const int size = 2;
UINT numVertices = 24;
vertex* v = NULL;

//vertices for a cube
v[0] = vertex( D3DXVECTOR3(x,y+size,z), D3DXVECTOR4(1,0,0,1) ); //front top left
v[1] = vertex( D3DXVECTOR3(x+size,y+size,z), D3DXVECTOR4(0,1,0,1) ); //front top right
v[2] = vertex( D3DXVECTOR3(x,y,z), D3DXVECTOR4(0,0,1,1) ); //front bottom left
v[3] = vertex( D3DXVECTOR3(x+size,y,z), D3DXVECTOR4(1,1,0,1) ); //front bottom right

v[4] = vertex( D3DXVECTOR3(x,y+size,z+size), D3DXVECTOR4(1,0,0,1) ); //back top left
v[5] = vertex( D3DXVECTOR3(x+size,y+size,z+size), D3DXVECTOR4(0,1,0,1) ); //back top right
v[6] = vertex( D3DXVECTOR3(x,y,z+size), D3DXVECTOR4(0,0,1,1) ); //back bottom left
v[7] = vertex( D3DXVECTOR3(x+size,y,z+size), D3DXVECTOR4(1,1,0,1) ); //back bottom right

pVertexBuffer->Unmap();

//create indexes for a cube
unsigned int* i = NULL;

//front face
i[0] = 0;
i[1] = 1;
i[2] = 3;
i[3] = 2;

i[4] = 0xffffffff; //start new strip

//right face
i[5] = 0;
i[6] = 2;
i[7] = 6;
i[8] = 4;

i[9] = 0xffffffff;

//left face
i[10] = 5;
i[11] = 7;
i[12] = 3;
i[13] = 1;

i[14] = 0xffffffff;

//back face
i[15] = 6;
i[16] = 4;
i[17] = 5;
i[18] = 7;

i[19] = 0xffffffff;

//top face
i[20] = 0;
i[21] = 4;
i[22] = 1;
i[23] = 5;

i[24] = 0xffffffff;

//bottom face
i[25] = 6;
i[26] = 2;
i[27] = 3;
i[28] = 7;

pIndexBuffer->Unmap(); [/code]

##### Share on other sites
So, what result are you getting. I'm guessing that only some parts of it are showing up? It looks to me like your winding order isn't consistent.It may help to draw out the cube on paper and number the verts, so you can have it in front of you, and can trace out the tris/sides to help you get the vert order correct.

##### Share on other sites
[quote name='yckx' timestamp='1317529414' post='4868161']
So, what result are you getting. I'm guessing that only some parts of it are showing up? It looks to me like your winding order isn't consistent.It may help to draw out the cube on paper and number the verts, so you can have it in front of you, and can trace out the tris/sides to help you get the vert order correct.
[/quote]

[img]http://i155.photobucket.com/albums/s301/darkrei9n/asdasdasdasdasd.png[/img]

I tried drawing it out on paper. No luck. Infact that was the first thing I tried.

##### Share on other sites
[quote name='darkreign' timestamp='1317526926' post='4868150']
Alright I'm having an issue with indices and freaking drawing a simple cube. Either me or the indices are being retarded about this. Its probably me. I think maybe I got the addition for coordinates messed up.

[code]
//front face
i[0] = 0;
i[1] = 1;
i[2] = 3;
i[3] = 2;

i[4] = 0xffffffff; //start new strip

//right face
i[5] = 1;
i[6] = 2;
i[7] = 5;
i[8] = 6;

i[9] = 0xffffffff;

//left face
i[10] = 0;
i[11] = 4;
i[12] = 3;
i[13] = 7;

i[14] = 0xffffffff;

//back face
i[15] = 7;
i[16] = 6;
i[17] = 4;
i[18] = 5;

i[19] = 0xffffffff;

//top face
i[20] = 0;
i[21] = 4;
i[22] = 1;
i[23] = 5;

i[24] = 0xffffffff;

//bottom face
i[25] = 6;
i[26] = 2;
i[27] = 3;
i[28] = 7;

pIndexBuffer->Unmap(); [/code]
[/quote]

Changed a couple indices, try it with these.

edit: I am fairly sure some of your groups for faces were incorrect, but my order inside those groups might also be incorrect.

Top: 0,1,4,5
Front: 0,1,2,3
Left: 4,0,3,7
Right: 1,2,5,6
Bottom: 3,2,6,7
Back: 4,5,6,7

##### Share on other sites
[quote name='way2lazy2care' timestamp='1317571837' post='4868294']
Top: 0,1,4,5
Front: 0,1,2,3
Left: 4,0,3,7
Right: 1,2,5,6
Bottom: 3,2,6,7
Back: 4,5,6,7
[/quote]
Heh. I was going to suggest:
Front: 0, 2, 3, 1
Right: 1, 3, 7, 5
Left: 0, 4, 6, 2
Back: 4, 5, 7, 6
Top: 0, 1, 5, 4
Bottom: 2, 6, 7, 3

From the picture, it looks like his normals are flipped for most of the sides, which is why I reversed their winding order. Also, the yellow/green face looks mis-indexed, but it doesn't look like it should be from the code. It would help if each vertex had a unique color instead of having two vertices share a color.

##### Share on other sites
In addition to ordering the strips, remember to allocate the right buffers, 8 vertices in the vertex buffer and 29 in the index buffer (24 + 5). Your numVertices = 24 might mess some things up. Also winding order and culling mode can matter.

Another alternative for the strips, if Y+ is up and Z+ is further in, and you have clockwise frontfaces that should be on the outside of the cube.
0, 1, 2, 3, // front
0xffffffff,
5, 4, 7, 6, // back
0xffffffff,
4, 0, 6, 2, // left
0xffffffff,
1, 5, 3, 7, // right
0xffffffff,
4, 5, 0, 1, // top
0xffffffff,
2, 3, 6, 7 // bottom

##### Share on other sites
[quote name='yckx' timestamp='1317572985' post='4868300']
[quote name='way2lazy2care' timestamp='1317571837' post='4868294']
Top: 0,1,4,5
Front: 0,1,2,3
Left: 4,0,3,7
Right: 1,2,5,6
Bottom: 3,2,6,7
Back: 4,5,6,7
[/quote]
Heh. I was going to suggest:
Front: 0, 2, 3, 1
Right: 1, 3, 7, 5
Left: 0, 4, 6, 2
Back: 4, 5, 7, 6
Top: 0, 1, 5, 4
Bottom: 2, 6, 7, 3

From the picture, it looks like his normals are flipped for most of the sides, which is why I reversed their winding order. Also, the yellow/green face looks mis-indexed, but it doesn't look like it should be from the code. It would help if each vertex had a unique color instead of having two vertices share a color.
[/quote]

[url="http://imageshack.us/photo/my-images/36/vertsides.png/"][img]http://img36.imageshack.us/img36/4641/vertsides.png[/img][/url]

I read my diagram totally wrong I think. I did it very small in pen edit: the bottom right is a 7.

##### Share on other sites
No luck with any of those, it still is messed up. I think the coordinates themselves are messed up.

##### Share on other sites
The coordinates look correct to me. As I said before, it would be easier if each vertex were uniquely colored. That way you identifying which face is which would be easier in the graphic, and you could fix the faces one at a time. I'd just send two faces (after changing to unique vertex colors), and ensure theyre correct before adding a third, and so on. I recommend starting with two so you can easily tell if the normals are flipled.

##### Share on other sites
I've beginning to notice that the major issue is with the left face, no matter what, one point is always connected to vertex 4.

Issue is far from my code, I'm beginning to think either my computer is racist towards cubes, or I messed up something earlier on in the code.

I just took code out of a book I have for drawing a cube and the same crap is also happening.

##### Share on other sites
[quote name='darkreign' timestamp='1317615193' post='4868488']
I've beginning to notice that the major issue is with the left face, no matter what, one point is always connected to vertex 4.[/quote]

Here's one suggestion. Try drawing it using only one strip. You should be able to do a whole cube with only one strip, so try 0,1,2,3,7,1,5,0,4,2,6,7,4,5. I think that's right without breaking the strip.

[quote]Issue is far from my code, I'm beginning to think either my computer is racist towards cubes, or I messed up something earlier on in the code.
[/quote]

I highly doubt the issue is far from your code. I can tell you that your indices in your original post were not correct; even though my original correction was wrong. I'd anticipate it has something to do either with your indices or the way you are starting a new strip. Probably something akin to your new strip not actually being a new strip.

##### Share on other sites
Can you post your entire draw routine?

##### Share on other sites
Fixed it. moved to a backup of my original and apparently I changed something earlier on in the code that effected it.

##### Share on other sites
Ah, cool. For curiosity's sake, can you tell us what the problem was?

##### Share on other sites
[quote name='yckx' timestamp='1317681460' post='4868734']
Ah, cool. For curiosity's sake, can you tell us what the problem was?
[/quote]

Dunno. I copy and pasted a backup's code over all my initialization stuff. I can't remember changing anything there so I can't imagine what it was.

##### Share on other sites
You [b]are[/b] using some kind of version control software, yes?

##### Share on other sites
Well, I ran into another issue after moving over to a mesh format and implementing lighting. I had this issue before and I fixed it but I can't remember how. However the issue now is that it draws (correctly from what I can see) but its only drawing part of it.

[IMG]http://i155.photobucket.com/albums/s301/darkrei9n/peopleperson.png[/IMG]

If I move my camera around it sort of cuts off everything past a certain distance and for some reason that distance is about an inch from the camera.

##### Share on other sites
[quote name='darkreign' timestamp='1318020652' post='4870253']
Well, I ran into another issue after moving over to a mesh format and implementing lighting. I had this issue before and I fixed it but I can't remember how. However the issue now is that it draws (correctly from what I can see) but its only drawing part of it.

[img]http://i155.photobucket.com/albums/s301/darkrei9n/peopleperson.png[/img]

If I move my camera around it sort of cuts off everything past a certain distance and for some reason that distance is about an inch from the camera.
[/quote]
What's your camera's far distance? The problem is probably there.

##### Share on other sites
That's what the near and far plane do, clip things too near or far of the camera. Try adjusting those values and see what it does. As for the lightning problem, make sure that you're normals are correct and to enable lighting in the first place. Why don't you use a 3d modeler?? There's no way you're gonna make more complex shapes than basic primitives by writing vertices one by one anyway, and they will be errors free.

EDIT: if you're drawing a cube with an index array, it might look weird because triangles of different faces use the same normal value instead of there own. You could try averaging them, but im not sure if it will look good on a cube, but that's how you make models look smoother. The problem with the cube is that the angles are too steep.

Here's a cube indexed and normals averaged:
http://www.freewebs.com/vortex666/Cube%20Indexed.jpg

Here's the same cube but without the index array:
http://www.freewebs.com/vortex666/Cube%20Not%20Indexed.jpg

##### Share on other sites
The thing is that near and far plane are set to 1 and 1000 respectively. Its not the far and near plane. Last time I had this issue it had to do with my world and projection matrix and I mixed them up or something. Can't remember how I fixed it though.

##### Share on other sites
Well if that can help that's how i do mine and it work:

D3DXMATRIX matTranslate;
D3DXMATRIX matRotateX, matRotateY, matRotateZ;

D3DXMatrixRotationX(&matRotateX, -Settings.yRotation / 360.0f);
D3DXMatrixRotationY(&matRotateY, -Settings.xRotation / 360.0f);
D3DXMatrixRotationZ(&matRotateZ, 0.0f);
D3DXMatrixTranslation(&matTranslate, Settings.xTranslation, Settings.yTranslation, Settings.zTranslation);
dxdev->SetTransform(D3DTS_WORLD, &(matRotateX * matRotateY * matRotateZ * matTranslate));

D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f, Settings.CameraDistance), &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
dxdev->SetTransform(D3DTS_VIEW, &matView);

##### Share on other sites
[quote name='Vortez' timestamp='1318039492' post='4870347']
Well if that can help that's how i do mine and it work:

D3DXMATRIX matTranslate;
D3DXMATRIX matRotateX, matRotateY, matRotateZ;

D3DXMatrixRotationX(&matRotateX, -Settings.yRotation / 360.0f);
D3DXMatrixRotationY(&matRotateY, -Settings.xRotation / 360.0f);
D3DXMatrixRotationZ(&matRotateZ, 0.0f);
D3DXMatrixTranslation(&matTranslate, Settings.xTranslation, Settings.yTranslation, Settings.zTranslation);
dxdev->SetTransform(D3DTS_WORLD, &(matRotateX * matRotateY * matRotateZ * matTranslate));

D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f, Settings.CameraDistance), &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
dxdev->SetTransform(D3DTS_VIEW, &matView);
[/quote]

Once again I fixed the issue through some really odd tinkering. This time I changed when I used my matrix projectionMatrix and it worked... Also, my mesh cube is more... diamond shaped than cube.

##### Share on other sites
[quote name='darkreign' timestamp='1318044188' post='4870371']
[quote name='Vortez' timestamp='1318039492' post='4870347']
Well if that can help that's how i do mine and it work:

D3DXMATRIX matTranslate;
D3DXMATRIX matRotateX, matRotateY, matRotateZ;

D3DXMatrixRotationX(&matRotateX, -Settings.yRotation / 360.0f);
D3DXMatrixRotationY(&matRotateY, -Settings.xRotation / 360.0f);
D3DXMatrixRotationZ(&matRotateZ, 0.0f);
D3DXMatrixTranslation(&matTranslate, Settings.xTranslation, Settings.yTranslation, Settings.zTranslation);
dxdev->SetTransform(D3DTS_WORLD, &(matRotateX * matRotateY * matRotateZ * matTranslate));

D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f, 0.0f, Settings.CameraDistance), &D3DXVECTOR3 (0.0f, 0.0f, 0.0f), &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
dxdev->SetTransform(D3DTS_VIEW, &matView);
[/quote]

Once again I fixed the issue through some really odd tinkering. This time I changed when I used my matrix projectionMatrix and it worked... Also, my mesh cube is more... diamond shaped than cube.
[/quote]
Might be sheering. That is to say, your World matrix might be concating in a way that sheers the matrix. This is usually the result of concating them in the wrong order. If I have it right, it's Scale * Rotation * Position; Matrices concat in reverse order.

Or at least, that's what I'm using and it's working.

@Vertez: Why store matrix rotation for X, Y and Z? One matrix can handle X, Y and Z rotation itself, unless you have some really specialized needs.

##### Share on other sites
Well that how it was in the example i've read some time ago, im more used to opengl than directx, but that's good to know.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628403
• Total Posts
2982483

• 9
• 10
• 9
• 21
• 24