-1
\$\begingroup\$

I created different types of cubes for my cube class and based on the height of the cube, the cube type is different. This means changing the texture for the cube.

               if (y >= 1)
                    {
                        SetUpIndicesAndVertices(x, map[x, z] - y, z, vertices, indices);
                        cubes.Add(new Cube.Stone(device, new Vector3(x, map[x, z] - y, z), stone));
                    }

                    else
                    {
                        SetUpIndicesAndVertices(x, map[x, z] - y, z, vertices, indices);
                        cubes.Add(new Cube.Grass(device, new Vector3(x, map[x, z] - y, z), grass));
                    }

So I thought this would work, but my texture for all cubes is still grass. I noticed in my draw method these few lines:

    foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            no++;
            pass.Apply();
            effect.VertexColorEnabled = false;
            effect.TextureEnabled = true;
            effect.Texture = grass;

Which sets every cube to grass. Is there a way that I can set the texture based on each cube type in the list "cubes"?

\$\endgroup\$
3
  • \$\begingroup\$ you can sort them corresponding to tiles, and then when you are drawing them you only change texture once when you need. so that you know that example 0->11 is grass, and 11->15 is stone and 15->100 is dirt. \$\endgroup\$
    – Tordin
    Jul 16, 2013 at 9:50
  • \$\begingroup\$ (Just note that the "Correct" way to do this isn't with individual textures but with a texture atlas - so you can use a single texture for cubes of many different kinds - so you need only one buffer and draw call.) \$\endgroup\$ Jul 16, 2013 at 12:57
  • \$\begingroup\$ Ah yikes. Well the sides of the cubes are all the same texture so that shouldn't be too complicated \$\endgroup\$ Jul 16, 2013 at 13:49

1 Answer 1

0
\$\begingroup\$

A more OO approach would consist in looping through your objects like this:

foreach (RenderableObject rObject in Items)
{
    [...]
    effect.Texture = rObject.Texture;
}

of course this would result in you possibly having the context switch texture very frequently and unnecessarily. If you have the objects sorted by texture, you can move that assignment outside and before the loop. Then you just have one such render-loop for each different category of items that share the same properties.

I suggest you implement some functionality representing meta-information about the items you are going to render. This way you can sort them according to different properties (i.e.: texture, rendering order [opaque, blended], geometry. Then you pass this information to your draw call so that it sets the correct state.

\$\endgroup\$
1
  • \$\begingroup\$ I actually did have something that like in some previous code I had, but I forgot about it. I'll have to try it out. I basically went through my list of cube: foreach(Cube block in cubes). Maybe that will work \$\endgroup\$ Jul 16, 2013 at 13:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .