#### Archived

This topic is now archived and is closed to further replies.

# INDEX BUFFERS... again... lol

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

## Recommended Posts

Hello again... in DirectX 9 when you want to draw an IndexedPrimative you have the following function:

HRESULT DrawIndexedPrimitive
(
D3DPRIMITIVETYPE Type,
INT BaseVertexIndex,
UINT MinIndex,
UINT NumVertices,
UINT StartIndex,
UINT PrimitiveCount,
);


right ive looked at MSDN and no matter how many times i look at it..my mind just ends up telling me i need to go drink some OJ... so ... Can anyone please explain to me what each one of these parts means, (in english) MSDN is sooooo vauge that im not sure what parameters need what values... Here is what i currently think: D3DPRIMITIVETYPE: well thats an easy one thats just the Triangle_Strip or whatever.... BaseVertexIndex: well this ones the offset from the begining of the index buffer, which is 0 as its all index information.... MinIndex: ?? this one got me puzzled... is this the minimum index that you will use to render from..? can we set this to 0? or does it have to have a value relative to where your reading from? NumVertices: ?? I thought this one was the amount of vertices that i would use in this call...so one 2 triangle strips would be 4 vertices.. but this seems to defy me.. StartIndex: Im sure this is the same as the normal DrawPrimitive one where you specify the offset from begining to where your first vertice is indexed... PrimitiveCount: Well this one is the amount of primitives you want.. well anyway... if anyone can help me understand this i would be very gratefull... thanks again people

##### Share on other sites
This question has been asked and answered many times on this forum. Here is a very good response by SC1A, who unfortunately no longer visits this forum. His knowledge and willingness to help is sorely missed.
quote:
Original post by SC1A
StartIndex: the first index in the INDEX BUFFER that this primitive will use.

MinIndex: lowest vertex in the VERTEX BUFFER that this primitive will use. Bear in mind that BaseVertexIndex will also be applied.

NumVertices: the number of vertices that need to be transformed, that any primitives described in the index buffer might need to reference.

Some pseudocode of what happens conceptually:
MyVertex verts[ 1000 ];MyTransformed trans_verts[ 1000 ];WORD indices[ 1500 ];..for (vtx=BaseVertexIndex+MinIndex; vtx<baseVertexIndex+MinIndex+NumVertices; ++vtx){  transform_and_light( verts[vtx], trans_verts[vtx] );}..// assuming triangle listsfor (tri=0; tri{  idx = tri*3; // 3 indices per triangle  // get the 3 indices for the triangle  idx_a = indices[ idx + 0 + StartIndex ];  idx_b = indices[ idx + 1 + StartIndex ];  idx_c = indices[ idx + 2 + StartIndex ];  MyTransformedVertex vert_a, vert_b, vert_c;  vert_a = trans_verts[ idx_a + BaseVertexIndex ];  vert_b = trans_verts[ idx_b + BaseVertexIndex ];  vert_c = trans_verts[ idx_c + BaseVertexIndex ];  RenderTriangle( vert_a, vert_b, vert_c );}

BTW: The range is used when you''re using software vertex processing. Many hardware devices are capable of dereferencing through the indices before transformation. However this is complicated by pre and post T&L caches. The "conceptual" model that software processing does is the most important to remember.

##### Share on other sites
sorry i looked but never found anything on it... but anyway... he says:

(i would quote but cant remember how)

// is this a value added to offset the index that is read. or is this a value that is used to actually change the data held in that index?

MinIndex: lowest vertex in the VERTEX BUFFER that this primitive will use. Bear in mind that BaseVertexIndex will also be applied.

// Does this mean that we set the offset to start reading from and then set this as 0 to read from the specified BaseVertexIndex value, or do we set this the same as BaseVertexIndex value?

NumVertices: the number of vertices that need to be transformed, that any primitives described in the index buffer might need to reference.

// Is this just the number of vertices used in the call, such as 2 triangle strips would give a value of 4 here? or do we need to set another value such as the amount of vertices per primitive..

--------------------------------

sorry for sounding dim.. but his explanation is still faily vauge... thanks though

##### Share on other sites
A nice visual representation can be found here (pdf - page 18).

##### Share on other sites
thanks...thats quite a nice document...

##### Share on other sites
quote:
Original post by Grofit
sorry i looked but never found anything on it... but anyway... he says:

(i would quote but cant remember how)

// is this a value added to offset the index that is read. or is this a value that is used to actually change the data held in that index?

The SDK says "MinIndex and all the indices in the index stream are relative to the BaseVertexIndex." That leads me to believe that the value of BaseVertexIndex is added to every index value read from the Index Buffer. MinIndex is relative to it too.

quote:

MinIndex: lowest vertex in the VERTEX BUFFER that this primitive will use. Bear in mind that BaseVertexIndex will also be applied.

// Does this mean that we set the offset to start reading from and then set this as 0 to read from the specified BaseVertexIndex value, or do we set this the same as BaseVertexIndex value?

This is the lowest index value that will be allowed to read from the index buffer, after applying BaseVertexIndex. If the index buffer contains 0, 1, 2, 3... and you set BaseVertexIndex to 0 and MinIndex to 3...if you read in 0,1, or 2 from the index buffer, the draw call will fail. In this scenario, where the index buffer starts at 0 and increases to some value, say 100...if you were to set the StartIndex to 3, the first value read in and every value after that would be 3 or greater and so would pass the MinIndex test.

If you set BaseVertexIndex to 3, and your MinIndex is 0, then if you read in 0 from the index buffer (StartIndex=0), BaseVertexIndex is added to make it 3, and it satisfies the MinIndex test (BaseVertexIndex + MinIndex = 3), allowing the draw to move to the next index. Does that make sense?

quote:

NumVertices: the number of vertices that need to be transformed, that any primitives described in the index buffer might need to reference.

// Is this just the number of vertices used in the call, such as 2 triangle strips would give a value of 4 here? or do we need to set another value such as the amount of vertices per primitive..

Same thing as before, the NumVertices is used to calculate the max index value that can be read in from the index buffer. Your MaxIndexValue is MinIndex + BaseVertexIndex + NumVertices. If MaxIndexValue is ever read in from the index buffer, the call will fail.

i.e. Index Buffer = 0 to 100, increments of 1.

DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, 0, 100, 0, 25);
should be okay, because you start at index 0 (StartIndex), then add 0 (BaseVertexIndex) and 0 (MinIndex) together, which satisfy a MinIndex test of 0. 0 (BaseVertexIndex) + 0 (MinIndex) + 100 (NumVertices) = 100. Since the last index read in from the index buffer is 100, you satisfy the max index in the range.

DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, 1, 100, 0, 25);
will fail however, because the first index read in is 0, and the MinIndex that can be read in is 1.

DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, 0, 99, 0, 25);
will fail because the last index read in is 100, and the MaxIndex that can be read in is 99.

DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 4, 0, 100, 0, 24);
will pass because the first index read in is 0 (but when BaseVertexIndex is added to it, it becomes 4) and it satisfies the MinIndex test of 4 (BaseVertexIndex + MinIndex), and the MaxIndex that can be read in is 104 (BaseVertexIndex(4) + MinIndex(0) + NumVertices(100)) , so the last index read in from the buffer is 100 (but then BaseVertexIndex is added, which makes it 104) and it satisfies the MaxIndex test.

In short, don''t concern yourself with the fact that I made the index buffer start at 0 and end at 100. You could switch that all around, throw in random numbers, or whatever you need to do. You''ll just always have to satisfy that the value read in from the index buffer is never less than (BaseVertexIndex + MinIndex) and that the value is never greater than (BaseVertexIndex + MinIndex + NumVertices).

If I made a mistake in here, let me know. I don''t have time to proofread, have to get home!

Chris