Friday, November 15, 2013

OpenTK Tutorial 4 Addendum: The ColorCube class

Here's a little class that can show drawing multiple objects better. It's a cube with a solid color (code after the break).






    class ColorCube : Cube
    {
        Vector3 Color = new Vector3(1, 1, 1);

        public ColorCube(Vector3 color) : base() {
            Color = color;
        }

        public override Vector3[] GetColorData()
        {
            return new Vector3[] { 
                Color,
                Color, 
                Color,
                Color,
                Color, 
                Color, 
                Color, 
                Color
            };
        }
    }

The code is pretty simple, just set a color and return it for all the vertices' color (isn't OOP convenient?).

And if you want to draw a bunch of them in random positions, this will do it (keep in mind some of them might be placed out of view until we get a camera working):
            Random rand = new Random();
            
            for (int i = 0; i < 100; i++)
            {
                ColorCube c = new ColorCube(new Vector3((float)rand.NextDouble(),(float)rand.NextDouble(),(float)rand.NextDouble()));
                c.Position = new Vector3((float)rand.Next(-4, 4), (float)rand.Next(-4, 4), (float)rand.Next(-8, 8));
                c.Rotation = new Vector3((float)rand.Next(0, 6), (float)rand.Next(0, 6), (float)rand.Next(0, 6));
                c.Scale = Vector3.One * ((float)rand.NextDouble() + 0.2f);
                objects.Add(c);
            }

4 comments:

  1. Hi, I'm new to OpenGL. I saw these tutorials and they look extremely useful. I was wondering if there is a chance you can do a texture to quad tutorial. I for one, don't wan't to get bogged down with too much 3d math and so on, so it would be interesting to see how we can create a simple class to allow rendering to quads, so we can use opentk to do 2d renders, like in a platformer game. I think it would be very, very helpful to alot of vb .net users. Any chance for this?

    ReplyDelete
    Replies
    1. Thanks for the feedback! I have a plan for upcoming tutorials right now (the current plan is to make one for a basic camera next, as soon as work lets up a bit), but I'll definitely do the textures one as soon as I can after that. A textured quad class should be really simple to add as an addendum at that point.

      Delete
    2. Cool, cool, thanks - I'll keep checking back. I'm starting to get OpenTK up and running.

      Delete
  2. Hi there. I been following this tutorial series and it's been a huge help to me, so Thank you. Now, I found something pretty odd while adding the ColorCube do the program. If I draw only 2 cubes, 1 being the Cube, and the other being ColorCube, one of the cubes get colored wrongly. Like only half of it's color data is from Cube, and the other half is from ColorCube. IF the amount of cubes is 3, the problem disappears.

    I don't know if this has been found and solved already, but I can't find the reason why this is happening.

    ReplyDelete