0
\$\begingroup\$

I have just recently started working with Monogame (XNA). Now I had code that was drawing a grid using DrawUserIndexedPrimitives. However I read that it reloads the vertices in the GPU each draw, and I don't think that is going to cut it for me. Therefore I wanted to use DrawIndexedPrimitives instead.

However, when I made the switch, my pretty grid suddenly dissapeared. The question is thus: What did I do wrong using DrawIndexedPrimitives? The code within the draw method is given below:

game.GraphicsDevice.Indices = terrainIndexBuffer;
game.GraphicsDevice.SetVertexBuffer(terrainVertexBuffer);

game.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList,
                                          0,
                                          0,
                                          terrainVertices.Length,
                                          0,
                                          triangleIndicesList.Length / 3);
// This worked VV
// game.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList,
//                                               terrainVertices,
//                                               0,
//                                               terrainVertices.Length,
//                                               triangleIndicesList,
//                                               0,
//                                               triangleIndicesList.Length / 3);

Here terrainVertices is the list of vertices for the triangles, triangleIndicesList is a list representing the triangles (each three entries here refer to the vertices of one triangle). Outside of the Draw method, I initialized the grid and index list as follows:

terrainVertexBuffer = new VertexBuffer(game.GraphicsDevice,
                                       typeof(VertexPosition),
                                       terrainVertices.Length,
                                       BufferUsage.None);

terrainIndexBuffer = new IndexBuffer(game.GraphicsDevice,
                                     IndexElementSize.SixteenBits,
                                     sizeof(short) * triangleIndicesList.Length,
                                     BufferUsage.None);

terrainVertexBuffer.SetData<VertexPosition>(terrainVertices);
\$\endgroup\$
0
\$\begingroup\$

It turns out that I forgot to initialize the terrainIndexBuffer with the array of indices. So I added the line terrainIndexBuffer.SetData(triangleIndicesList);

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.