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 am creating a quad that starts as a single quad, then divides itself multiple times (from 1 -> 4 -> 16 -> 64 -> 256 etc) but I have encounted an issue. When I get to 64 sub divisions only 16 quads are shown.

enter image description here

Here is the quad class:

class Quad {
        GraphicsDevice device;

        private int rows = 1;
        private int columns = 1;

        private int width = 64;
        private int height = 64;

        private Quad[,] quads;
        private Quad parent;

        private List<VertexPositionColor> vertices;
        private List<int> indices;

        private Vector2 index;

        public List<VertexPositionColor> Vertices {
            get { return vertices; }
            set { }
        }

        public List<int> Indices {
            get { return indices; }
            set { }
        }

        public Vector2 Index {
            get { return index; }
            set { index = value; }
        }

        public int Width {
            get { return width; }
            set { }

        }

        public int Height {
            get { return height; }
            set { }
        }

        public void SetSize(int width, int height) {
            this.width = width;
            this.height = height;
        }

        public void SetParent(Quad parent) {
            this.parent = parent;
        }

        public List<Quad> GetChildren() {
            List<Quad> quadList = new List<Quad>();

            if (quads != null) {
                for (int x = 0; x < this.quads.GetLength(0); x++) {
                    for (int y = 0; y < this.quads.GetLength(1); y++) {
                        quadList.AddRange(quads[x, y].GetChildren());

                        OutputBuffer.STRING_BUFFER.Clear();
                    }
                }

                OutputBuffer.Add("Children Count: " + quadList.Count);

                return quadList;
            }

            quadList.Add(this);
            return quadList;
        }

        public Quad(ref GraphicsDevice device) {
            this.device = device;
        }

        public void Construct() {
            ConstructVertices();
            ConstructIndices();
        }

        private void ConstructVertices() {
            vertices = new List<VertexPositionColor>();

            Vector3 parentPosition = parent != null ? new Vector3(parent.Index.X * parent.Width, 0, parent.Index.Y * parent.Height) : Vector3.Zero;

            Vector3 backLeft = new Vector3(index.X * width, 0, index.Y * height + height) + parentPosition;
            Vector3 backRight = new Vector3(index.X * width + width, 0, index.Y * height + height) + parentPosition;
            Vector3 frontLeft = new Vector3(index.X * width, 0, index.Y * height) + parentPosition;
            Vector3 frontRight = new Vector3(index.X * width + width, 0, index.Y * height) + parentPosition;

            vertices.Add(new VertexPositionColor(frontLeft, Color.White));
            vertices.Add(new VertexPositionColor(frontRight, Color.White));
            vertices.Add(new VertexPositionColor(backLeft, Color.White));
            vertices.Add(new VertexPositionColor(backRight, Color.White));
        }

        private void ConstructIndices() {
            indices = new List<int>();

            indices.AddRange(new int[] { 0, 2, 3, 3, 1, 0 }.ToList());
        }

        public void ConstructChildren() {
            if (quads == null) {
                quads = new Quad[2, 2];

                for (int x = 0; x < quads.GetLength(0); x++) {
                    for (int y = 0; y < quads.GetLength(1); y++) {
                        quads[x, y] = new Quad(ref device);
                        quads[x, y].Index = new Vector2(x, y);
                        quads[x, y].SetSize(width / 2, height / 2);
                        quads[x, y].SetParent(this);
                        quads[x, y].Construct();
                    }
                }

                this.vertices = null;
                this.indices = null;
            } else {
                for (int x = 0; x < quads.GetLength(0); x++) {
                    for (int y = 0; y < quads.GetLength(1); y++) {
                        quads[x, y].ConstructChildren();
                    }
                }
            }
        }
    }

I think the problem is when the vertices are being constructed. I think those four sets of 16 quads are being drawn on top of each other. And I defiantly think it's doing the subdivision.

Any help on the matter would be much appreciated.

Edit:

And here's the QuadRenderer Class, note that I just hacked to together to test the above, so don't expect quality code :D:

class QuadRenderer {
    GraphicsDevice device;

    Quad quad;

    Matrix view;
    Matrix projection;
    Matrix world;

    Effect effect;

    Vector2 position = new Vector2(16, 16);

    int columns = 2;
    int rows = 2;

    KeyboardState previousKbState;

    public QuadRenderer(GraphicsDevice device) {
        this.device = device;

        this.quad = new Quad(ref device);
        this.quad.Construct();
    }

    public void LoadContent(ContentManager content) {
        this.effect = content.Load<Effect>("Effects\\Effects");
    }

    public void Update() {
        ProcessInput();

        view = Matrix.CreateLookAt(new Vector3(position.X, 80, position.Y), new Vector3(position.X, 0, 16 + position.Y), new Vector3(0, 1, 0));
        projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);
    }

    public void ProcessInput() {
        KeyboardState kbState = Keyboard.GetState();

        if (kbState.IsKeyDown(Keys.W)) {
            position.Y += 2;
        }
        if (kbState.IsKeyDown(Keys.S)) {
            position.Y -= 2;
        }
        if (kbState.IsKeyDown(Keys.A)) {
            position.X += 2;
        }
        if (kbState.IsKeyDown(Keys.D)) {
            position.X -= 2;
        }

        if (kbState.IsKeyDown(Keys.Space) &&
            !previousKbState.IsKeyDown(Keys.Space)) {
                this.quad.ConstructChildren();
        }

        this.previousKbState = kbState;
    }

    public void Draw() {
        device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Color.Black, 1.0f, 0);

        RasterizerState rs = new RasterizerState();
        rs.CullMode = CullMode.None;
        rs.FillMode = FillMode.WireFrame;
        device.RasterizerState = rs;

        Matrix world = Matrix.Identity * Matrix.CreateTranslation(new Vector3(64 / 2, 0, 64 / 2));
        effect.CurrentTechnique = effect.Techniques["Coloured"];
        effect.Parameters["View"].SetValue(view);
        effect.Parameters["Projection"].SetValue(projection);
        effect.Parameters["World"].SetValue(world);

        foreach (EffectPass pass in effect.CurrentTechnique.Passes) {
            pass.Apply();

            foreach (Quad quad in this.quad.GetChildren()) {
                //world *= Matrix.CreateTranslation(new Vector3(quad.Index.X * quad.Width, 0, quad.Index.Y * quad.Height));

                device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, quad.Vertices.ToArray(), 0, quad.Vertices.Count, quad.Indices.ToArray(), 0, quad.Indices.Count / 3);
            }
        }
    }
share|improve this question
1  
And what do you think is causing it? Awfully close to here's my code find my problem. –  Byte56 Apr 29 '12 at 6:52
    
are you sure that it is even doing the subdivision to 64, or could it just be stagnating at 16? –  gardian06 Apr 29 '12 at 7:05
    
I think the problem is when the vertices are being constructed. I think those four sets of 16 quads are being drawn on top of each other. And I defiantly think it's doing the subdivision. –  Darestium Apr 29 '12 at 7:10
    
@gardian06 Updated –  Darestium May 1 '12 at 9:29

2 Answers 2

You are not showing all the code but I suggest making width and height floating point values. You are dividing them by 2 at each subdivision, meaning they'll eventually reach zero.

share|improve this answer
    
OK, I just added more of the code into the original post. And thanks for the tip, silly of me to make them integers :D –  Darestium Apr 30 '12 at 7:29
    
OK, I changed the types to floating point values, but it still has not fixed my problem :( –  Darestium Apr 30 '12 at 8:08
up vote 0 down vote accepted

I found the problem, I was not taking the parent index and adding it to the child's index, I changed the following line:

quads[x, y].Index = new Vector2(x + index.X * 2, y + index.Y * 2);

and removed the following (and all references to it):

Vector3 parentPosition = parent != null ? new Vector3(parent.Index.X * parent.Width, 0, parent.Index.Y * parent.Height) : Vector3.Zero;
share|improve this answer

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.