Skip to main content
GameDev.net gamedev.net
🔒 Locked

XNA 4.0 - Looking for quad hardware instancing tutorial

Started by Oblivion165 Feb 3, 2011 at 11:57 AM 3 replies 4.4k views
Original Post
Oblivion165
Oblivion165
Hi, I am a long time programmer just starting to get into 3D from 2D games. I have made some progress in DX9 but am having trouble finding a functional XNA 4.0 Tutorial that isn't overly complicated or uses meshes instead of vertex based geometry. For instance the one from MSDN is way too complicated for someone just starting out.

All I want to do is take 1 quad with a texture and render it multiple times at other locations. Like so:
quadd.png
If it would work with standard lighting that would be fantastic or at lease let me manually determine the darkness like in DX9's material color.

Thanks for your help if anyone has any

EDIT: Oh and I'm working in C#
Oblivion165
Oblivion165

This XNA tutorial is for windows phone, but it should run on a PC somewhat effortlessly. It basically draws a textured cube and builds the vertex information in the code. It'll at least point you in the right direction. http://blogs.msdn.co...ws-phone-7.aspx


That is a very nice example but sadly is doesn't use hardware instances to draw that cube, just uses standard primitives. Thanks though!

EDIT: Wait, I could be wrong here. I thought that it requires you to use the DrawIndexedPrimitive but that one is setting a vertex buffer. I'll look more into it and get back to ya'

EDIT 2: Fantastic, as long as this is actually doing it this is perfect. Has 4 of my major questions in it right off the bat.

public void RenderToDevice(GraphicsDevice device)
{
// Build the cube, setting up the _vertices array
if (_isConstructed == false)
ConstructCube();

// Create the shape buffer and dispose of it to prevent out of memory
using (VertexBuffer buffer = new VertexBuffer(
device,
VertexPositionNormalTexture.VertexDeclaration,
NUM_VERTICES,
BufferUsage.WriteOnly))
{
// Load the buffer
buffer.SetData(_vertices);

// Send the vertex buffer to the device
device.SetVertexBuffer(buffer);
}

// Draw the primitives from the vertex buffer to the device as triangles
device.DrawPrimitives(PrimitiveType.TriangleList, 0, NUM_TRIANGLES);
}


So is this a legit way of doing hardware instaces? I take it that you just build all the objects in the same vertex declare and then load them into the buffer.
Starnick
Starnick
Couple of comments:

1. You're not actually doing instancing there. DrawPrimitives() renders non-indexed primitives once. When you're doing instancing, remember you need to define the world matrices of each instance somewhere; in your code snippet you just have a vertex buffer with the cube's vertices. Take a look at the graphics device methods here. You're going to want DrawInstancedPrimitives()

2. You probably do not want to be recreating the vertex buffer and disposing of it every time you render your cube(s). First you're going to be generating garbage (vertex buffer is still a managed object) and at some point the garbage collector will kick in - generating garbage every frame is a bad idea, since you don't know when the collector will run, and when it does your game can hang. Secondly, creating GPU resources every frame isn't a good idea either, since resource creation isn't exactly fast. Ideally, create your vertex buffer once (e.g. at load time) then keep re-using it.

3. You mentioned you look at a XNA tutorial and it was too complex - did you take a look at the official AppHub one? It uses a custom model class, but it still will show you how to setup instancing just using vertex buffers, take a look at the method DrawModelHardwareInstancing() in InstancedModelSampleGame.cs

The basic idea (of what the sample uses) is you have your vertex buffer with the mesh's content (vertices, colors, normals, etc), and you have a second, dynamic vertex buffer which you update all of the world matrices for each instance to. Hope that helps.
Oblivion165
Oblivion165
Ah thank you for clearing that up looks like I had my terminology wrong. I was able to use that phone example to create exactly what I needed and I am getting fantastic performance. 60 frames rendering 10,000 quads with a 128x128 texture! Thanks for your help guys.

EDIT: And that link is the one I was talking about that is too complex for me.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.