0
votes

I am trying to convert a 2D game to 3D and am having some trouble with textures. The whole game is made up of cubes so i'm using vertices to create a cube and calling it from the draw method. However I can only seem to colour all the cubes with one texture using the basic effect. I presume I need to use some other kind of effect so i can chose the texture for each cube but not really sure what to do. Any help is appreciated

Would be using a loop like the following to draw the cubes on screen. Somehow need the texture to be different for each cube

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

  for (int i=0;i<mapsize;i++)
  {
      vertices = createCubeTexture(-1.0f, 0.5f);
      device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 2, VertexPositionTexture.VertexDeclaration);
  }
}
1
We'll need code to assess the problem deeper. Right now, it sounds like you're just drawing as many cubes as you have stored in a list. This is obviously going to be a problem, if you don't load in a seperate texture for each cube you draw. But show some code. - Falgantil
Added the code above, hope that helps - user3208483

1 Answers

0
votes

See there's your problem:

for (int i=0;i<mapsize;i++)
  {
      vertices = createCubeTexture(-1.0f, 0.5f);
      device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 2, VertexPositionTexture.VertexDeclaration);
  }

You're looping through the entire map, creating a cube with a texture, every time you loop through the mapsize (presumably an int). This is great, but the problem lies in the fact that the method will create the same texture for all cubes. What you'll need to do is something like this:

public class Cube
{
    public Vector3 Position { get; set; }
    public Texture2D Texture { get; set; }

    public Cube(Vector3 position, Texture2D texture)
    {
        Position = position;
        Texture = texture;
    }

    public void Draw(GraphicsDevice device)
    {
        var vertices = createCubeTexture(position);
        device.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 2, VertexPositionTexture.VertexDeclaration);
    }
}

Note that I haven't worked with manual drawing of cubes in quite some time, so I don't recall if this is the EXACT way, but if not, it's very similar. Just loop through each cube, and call the Draw method.

And then when you instantiate the cubes, you pass a different texture, depending on what cube you're creating :)