# [Solved] IndexBuffer : Rendering triangles one by one : Problem

This topic is 4627 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:09 AM]

##### Share on other sites
I think you have wrong DrawIndexedPrimitive call.

device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 0, 1);
device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 8, 3, 1);

It would be better. You can optimize range of indices 0 and 8 but it is not necessary. The 3 as fifth parameter means the beginning in the index stream to start reading indices.

##### Share on other sites
Wow it works perfectly ! Thanks a lot !

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...

Thx again !

##### Share on other sites
Parameters of these drawing functions may be little confusing, but you can find a short article in C++ help in DX SDK (I dont know if this article is also in Managed version of the help). The name is: "Rendering from Vertex and Index Buffers".
And there are these parameters explained perfectly.

Jiri

• 11
• 18
• 12
• 22
• 9
• ### Forum Statistics

• Total Topics
631397
• Total Posts
2999822
×