Drawing 2D Grid in 3D View - Need help with method
- by Deukalion
I'm trying to draw a simple 2D grid for an editor, to able to navigate more clearly around the 3D space, but I can't render it:
Grid2D class, creates a grid of a certain size at a location and should just draw lines.
public class Grid2D : IShape
{
private VertexPositionColor[] _vertices;
private Vector2 _size;
private Vector3 _location;
private int _faces;
public Grid2D(Vector2 size, Vector3 location, Color color)
{
float x = 0, y = 0;
if (size.X < 1f)
{
size.X = 1f;
}
if (size.Y < 1f)
{
size.Y = 1f;
}
_size = size;
_location = location;
List<VertexPositionColor> vertices = new List<VertexPositionColor>();
_faces = 0;
for (y = -size.Y; y <= size.Y; y++)
{
vertices.Add(new VertexPositionColor(location + new Vector3(-size.X, y, 0), color));
vertices.Add(new VertexPositionColor(location + new Vector3(size.X, y, 0), color));
_faces++;
}
for (x = -size.X; x <= size.X; x++)
{
vertices.Add(new VertexPositionColor(location + new Vector3(x, -size.Y, 0), color));
vertices.Add(new VertexPositionColor(location + new Vector3(x, size.Y, 0), color));
_faces++;
}
_vertices = vertices.ToArray();
}
public void Render(GraphicsDevice device)
{
device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, _vertices, 0, _faces);
}
}
Like this:
+----+----+----+----+
| | | | |
+----+----+----+----+
| | | | |
+----+----+----+----+
| | | | |
+----+----+----+----+
| | | | |
+----+----+----+----+
Anyone knows what I'm doing wrong? If I add a Shape without texture, it's set automatically to VertexColorEnabled and TextureEnabled = false.
This is how I render it:
foreach (RenderObject render in _renderObjects)
{
render.Effect.Projection = projection;
render.Effect.View = view;
render.Effect.World = world;
foreach (EffectPass pass in render.Effect.CurrentTechnique.Passes)
{
pass.Apply();
try
{
// Could be a Grid2D
render.Shape.Render(_device);
}
catch
{
throw;
}
}
}
Exception is thrown:
The current vertex shader declaration does not include all the elements required by the current Vertex Shader. Normal0 is missing.
Simply put, I can't figure out how to draw a few lines. I want to draw them one at a time and I guess that's the problem I haven't figured out, and even when I tried rendering vertices[i], vertices[i+1] and primitiveCount = 1, vertices = 2, and so on it didn't work either.
Any suggestions?