Thursday, 24 December 2009

How to render a Grid...

As I previously mentioned. I’ve been working on a grid for my game. So far the grid has been working, but was not visually present in the game. There is a good MSDN tutorial explaining how to render a line in your scene. MSDN Points, Lines and Primitives

It’s a good starting point if you’re trying to implement your own line renderer for your game. Luckily for some of us (Sunburn owners) this has been done for us by Synapse Gaming. It’s called the LineRenderHelper() and can be utilized for easy line rendering. Below is the result of a quick setup with the line renderer.

How it works…

You simply create a new LineRenderHelper() and pass it the maximum amount of points your grid will contain. Every line has a start and end point, so it’s 2 vertices for each line.

LineRenderHelper line = new LineRenderHelper(100); // This will create the helper with a maximum capacity of 100 vertices, or 50 lines.

Method for setting up the array of lines to be drawn.

public void SetGridLines(int grid, int lines, Color color)
         for (int i = 0; i < lines; i++)
               line.Submit(Vector3.Left * grid * i, (Vector3.Left * grid * i) + Vector3.Forward * (grid * (lines - 1)), color);
               line.Submit(Vector3.Forward * grid * i, (Vector3.Forward * grid * i) + Vector3.Left * (grid  * (lines - 1)), color);

This code will set up the array of lines. I pass a grid (empty space between the parallel lines) , lines (amount of lines that should be drawn) and the color.
To submit a line, all you need to do is call the Submit method.

          line.Submit(*start_vector3*, *end_vector3*, *color*);

This will add a single line to your renderer. If you try to execute your code, nothing will show on screen. This is because the helper is not automatically drawn. You need a BasicEffect to do this.

Draw Code:

// Setup your basicEffect
basicEffect.World = Matrix.Identity;
basicEffect.View = camera.View;
basicEffect.Projection = camera.Projection;
basicEffect.VertexColorEnabled = true;

//Start the effect
foreach (EffectPass pass in basicEffect.CurrentTechnique.Passes)

//render all lines stored inside the helper class


This is pretty basic, and should not need much explanation if you’ve ever worked with BasicEffect. If you’ve placed this correctly in your draw code, you should now see the lines on screen.

Of course, like mentioned before…You need Sunburn to use this helper class.
Sunburn Framework is currently in Beta. If you’re interested in using Sunburn, you should definitely sign-up and start using Sunburn. Sunburn Community Forums

Results of a quick stress-test:

Up to 200k vertices the scene would run fairly smoothly (on my laptop Asus M50VN) stressing it up to 1.000.000 vertices (making 500k lines) got my FPS down to 5. But it was still very possible to navigate around the scene. You only need a few dozen lines for your scene at any given time. So should not ever run into trouble with your FPS because of your Grid, which in the end, will most likely not even be rendered in your final game.

No comments:

Post a Comment