# [.net] [Solved] IndexBuffer : Render triangles one by one : problem

This topic is 4599 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi every one! I'm currently working on directX, and i'm facing a problem. When i create a cube with an array of vertices and an array of indices, i can render it with no problem with a "global" drawIndexedPrimitives call. But if i try to render it triangle by triangle, i get huge problems. First, here is my cube declaration :
vertices = new CustomVertex.PositionColored[8];
vertices[0].Position = new Vector3(0f, 0f, 0f);
vertices[0].Color = Color.Blue.ToArgb();
vertices[1].Position = new Vector3(0f, 10f, 0f);
vertices[1].Color = Color.Red.ToArgb();
vertices[2].Position = new Vector3(10f, 10f, 0f);
vertices[2].Color = Color.Green.ToArgb();
vertices[3].Position = new Vector3(10f, 0f, 0f);
vertices[3].Color = Color.White.ToArgb();
vertices[4].Position = new Vector3(0f, 0f, 10f);
vertices[4].Color = Color.Yellow.ToArgb();
vertices[5].Position = new Vector3(0f, 10f, 10f);
vertices[5].Color = Color.LightGray.ToArgb();
vertices[6].Position = new Vector3(10f, 10f, 10f);
vertices[6].Color = Color.Purple.ToArgb();
vertices[7].Position = new Vector3(10f, 0f, 10f);
vertices[7].Color = Color.LightBlue.ToArgb();

indices = new short[36];
indices[0]=0;
indices[1]=1;
indices[2]=2;
indices[3]=3;
indices[4]=0;
indices[5]=2;

indices[6]=7;
indices[7]=6;
indices[8]=5;
indices[9]=4;
indices[10]=7;
indices[11]=5;

indices[12]=3;
indices[13]=2;
indices[14]=6;
indices[15]=7;
indices[16]=3;
indices[17]=6;

indices[18]=4;
indices[19]=5;
indices[20]=1;
indices[21]=0;
indices[22]=4;
indices[23]=1;

indices[24]=1;
indices[25]=5;
indices[26]=6;
indices[27]=2;
indices[28]=1;
indices[29]=6;

indices[30]=4;
indices[31]=0;
indices[32]=3;
indices[33]=7;
indices[34]=4;
indices[35]=3;

Then to render, i first prepare the rendering :
device.VertexFormat = CustomVertex.PositionColored.Format;
device.SetStreamSource(0, vb, 0);
device.Indices = ib;

And finally i render the whole cube with :
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 12);

which actually works perfectly. But my point is i want to be able to render the cube triangle by triangle, doing something like this :
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 3, 0, 1);
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 3, 3, 0, 1);
...

where the idea is to keep the vertex buffer, and render each triangle by taking each time the three next indices in the indexbuffer. But this doesn't work. I tried many paramaters with no good result. Basically the first triangle renders as normal, but the second is not displayed, and so on... I also get some very strange triangles sometimes, as if i was doing wrong with the indices. I tried to disable culling to see if they were created not facing camera, but no better result. Please may someone help me? [Edited by - vk_phoenixfr on October 21, 2005 4:16:18 AM]

##### Share on other sites
vk_phoenixfr,

I'll be honest here, I don't have enough time to jazz up an example to test my idea, and I've never used indexed geometry in MDX before. Frankly, I can't think of a reason why you'd want to draw indexed geometry primitive by primitive.[smile] But, I'm sure you have a reason, so here goes.

If I'm reading the API docs properly, you are using DrawIndexedPrimitive incorrectly. And if I'm reading it correctly, your initial DrawIndexPrimitives and first DrawIndexedPrimitive call below would still work (as they do). As far as I can tell, it works like this:

DrawIndexedPrimitive(
PrimitiveType primitiveType: You've got this one down.
int baseVertex: This one is kinda tricky, and will almost always be 0. The number here is actually added to the all of the used indexes. You won't be using it here.
int minIndex: This is the first vertex you will be using from your stream source. This could change with every call, but it's more effective to leave it for the duration, since DX will pre-transform the specified vertices and leave them that way while it can. You kinda need to use 0 here.
int numVertices: This is the number of vertices you will be using from your stream source. Since your index call could point to any of the eight vertices in the stream source, you need to use 8 here. IE, the number of vertices your index buffer addresses is 8.
int startIndex: This is the first index to use from your index buffer during this call. This value will change in your individual primtive calls.
int primitiveCount: You're drawing one triangle at a time, so: 1
);

[source language="cs"]device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 1);device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 3, 1);device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 6, 1);device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 9, 1);// etc., etc., etc.

What you need to remember is the first three arguments define the portion of the Stream Source that will be used during this call. It has nothing to do with the index buffer itself (Except baseVertexIndex which kind of indirectly does). Here, you don't need to use baseVertexIndex, and your vertex data starts at index 0 and is 8 vertices in length, hence: "0, 0, 8".

I hope this clarifies things. The API doc is not so clear when read. Let me know if this was correct or not.

-Dave

##### Share on other sites
Thank you very much, it actually works great !

I didn't try to use the startIndex, i should have, but i thought it was better to mess with the baseVertex and the index in the indexBuffer.

Didn't get that i should give the eight vertices instead of three, as my indices may point to any of them...

As you said, the doc isn't so clear (actually the msdn doc is still temporary and they only give a few hint on method parameters - and not very clear).

Your explanation is much clearer and very useful to me.

Once again, thank you very much !!

##### Share on other sites
Not a problem. Glad I could be of help.
-Dave

• 9
• 9
• 13
• 41
• 15