Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Problem was that I set the effect for this to .EnableDefaultLightning(); but I shouldn't.

and this works as the Render code:

device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineList, _vertices, 0, _faces, VertexPositionColor.VertexDeclaration);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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