# MDX. Weird bug with index buffer

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

## Recommended Posts

I got an odd bug when trying to render a simple textured quad on screen. I copied my existing code for rendering bitmapped fonts (textured quads) which works fine and just modified it for this specific purpose of rendering a sprite on screen, which always consists of only 1 textured quad. I render my quad by using a vertex and an index buffer just as i would with the fonts. But when i set the index buffer size to 6 (since there are 2 triangles defined for the quad) it doesnt display anything at all on screen, only the vertices are visible if i render points. However, as soon as the index buffer is set to 18 (or above) it displays fine. Does anyone know why this happens? Since the buffer is never changes, 6 indices is what i want to use and this is how i do it throughout the rest of my system. Code:

// Creates a vertex buffer to store our vertices
vertexBuffer = new VertexBuffer(device.renderManager.getDevice() as Device,
4 * CustomVertex.TransformedColoredTextured.StrideSize,
Usage.Dynamic | Usage.WriteOnly,
CustomVertex.TransformedColoredTextured.Format,
Pool.Default);

// Create an index buffer to use with our indexed vertex buffer...
indexBuffer = new IndexBuffer(	typeof(int),
18,
device.renderManager.getDevice() as Device,
Usage.Dynamic | Usage.WriteOnly,
Pool.Default);

// Initialize the index buffer here since it will always be in the same pattern
int[] indices = new int[6];
indices[0] = 0;
indices[1] = 2;
indices[2] = 1;
indices[3] = 0;
indices[4] = 3;
indices[5] = 2;

// Write the indices to the index buffer
GraphicsStream stream = indexBuffer.Lock(0, 0, LockFlags.Discard);
stream.Write(indices);
indexBuffer.Unlock();
stream.Dispose();



##### Share on other sites
The indexbuffer intialization looks fine to me, but you're using 32bit indices (since you're defining them as ints) which may be the cause of the problem. For a small object (with < 32K indices) you should use shorts as the index type anyway, so I'd change the code to that:

[source lang=C#]// Create an index buffer to use with our indexed vertex buffer...indexBuffer = new IndexBuffer(	typeof(short),								6,								device.renderManager.getDevice() as Device,								Usage.Dynamic | Usage.WriteOnly,								Pool.Default);// Initialize the index buffer here since it will always be in the same patternshort[] indices = new short[6];indices[0] = 0;indices[1] = 2;indices[2] = 1;indices[3] = 0;indices[4] = 3;indices[5] = 2;// shortcut for setting dataindexBuffer.SetData(indices, 0, LockFlags.Discard);

This doesn't address your problem directly, but using 32-bit indices can lead to some problems. For one, not all videocards support 32-bit indices which may be one cause of the problem. Secondly, I don't know if MDX is smart enough to know that an IndexBuffer initialized with typeof(int) indicates the use of 32-bit indices (other IndexBuffer contructors take a bool parameter to indicate whether the indices are 16 or 32 bit). By using 16-bit indices, you don't have to worry about these two potential problems.

Another thing I spotted is that you're using a lock size of 0 (the 2nd parameter of indexBuffer.Lock). I don't know if this makes MDX lock the entire buffer, but you might want to consider the SetData alternative in my snippet.

Hope this helps :)

##### Share on other sites
I will try to implement your suggestions later tonight and see how it works :)
The SetData call was really convenient and thats one thing i didnt know about.

##### Share on other sites
Apart from getting better code with the shorts and SetData calls, it still wont display the triangles with an index buffer size less than 18, so thats pretty odd.

Of course i could continue using the code i have with the size of 18,
but finding out whats wrong would be very interesting to know :)

There is a sample project using the sprite class (which this is for)
with the code for this particular part located in the SpriteDX9.cs file.
The sample project is of an animated megaman character for testing it out
along with the xml based animation setup files.

##### Share on other sites
You may have specified the wrong number of primitives to draw in your DrawIndexPrimitive call (which should be 2 for a quad), so D3D goes looking for indices that aren't there and the call fails. By using more indices the call may succeed, but you're effectively rendering rubbish with the redundant 12 indices.

Just checked the code and it does indeed specify 6 primitives to draw which would require the 18 indices. By changing the last parameter in this line (SpriteDX9.cs, Render method) to 2, your problems should be fixed.

// last parameter specifies numPrimitives, which is numIndices/3 for a TriangleListd.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, 4, 0, 2);

For future reference, you might want to try enabling the debug runtimes every now and then to make sure your application works fine on them. The debug runtimes complain about near to everything, so they're a big help in finding issues like these which are easily overlooked.

##### Share on other sites
Thanks Remigus!

I cant help but to feel a bit stupid since it was such a simple bug in the end.
When i open up the code i have for rendering meshes however, i can see that i do exactly what you mentioned there so its a "stupid" miss from my side.

"you might want to try enabling the debug runtimes every now and then to make sure your application works fine on them. The debug runtimes complain about near to everything, so they're a big help in finding issues like these which are easily overlooked."

That i havent tried yet. Anything that makes bugfinding easier is appreciated.
I wil check it out.

Thanks again,
and as my dutch friend and roommate would say.. Stroopwafel! :)

• 35
• 12
• 10
• 9
• 9
• ### Forum Statistics

• Total Topics
631357
• Total Posts
2999525
×