1
\$\begingroup\$

I created a cube in blender which has multiple images applied to its faces. I am trying to import the model into xna and get the same results as shown when rendering the model in blender. I go through every mesh (for the cube its only one) and through every part but only the first image used in blender is displayed in every face. The code I am using to fetch the texture looks like that:

foreach (ModelMesh m in model.Meshes)
{                   
   foreach (Effect e in m.Effects)
   {
      foreach (var part in m.MeshParts)                   
      {
         e.CurrentTechnique = e.Techniques["Lambert"];                 
         e.Parameters["view"].SetValue(camera.viewMatrix);
         e.Parameters["projection"].SetValue(camera.projectionMatrix);
         e.Parameters["colorMap"].SetValue(modelTextures[part.GetHashCode()]);

      }
   }
   m.Draw();
}

Am I missing something?


EDIT:

Here is the code for getting the texture from the blender and use it in the XNA:

foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    BasicEffect basicEffect = part.Effect as BasicEffect;

                    if (basicEffect != null)
                        modelTextures[part.GetHashCode()] = basicEffect.Texture;

                    part.Effect = material.effect;

                }
            }
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

This is an exact duplicate of a question on StackOverflow.com, but since the question has to be on gamedev.stackexchange.com to flag it as a duplicate I'll give you the link here with some added info as an answer.

The answer: Drawing a textured cube with multiple sides in XNA 4.0

The difficulty here is that you can't easily tell XNA in one draw call to use multiple diffuse textures for the model. Lets give you a bit more background. Each model consists of vertices (points in 3D space with some additional information). Multiple vertices are used to construct triangles and from these triangles your cube is made. Often each vertex contains a tuple (u,v) which tells the GPU what pixel of the texture should be mapped to that vertex. For each triangle the 3 UVs from the vertices is then linearly interpreted over the surface of the triangle so that we can paint the triangle in the correct colours.

Now as you can see there is no information here on which texture to use. This entire scheme was designed to use one texture. Now vertices can contain arbitrary information so we could think of scheme were we create our own vertex type, say VertexPositionNormalUVTextureID instead of the common/default VertexPositionNormalTexture we'd then have to write a shader to which we stream 6 textures and the cube, having replaced its vertices by our own type. This would work perfectly but its quite a bit of work. So the other approach is to just see the cube as 6 planes and render each plane normally, setting a different texture each time. This saves us from a lot of hassle, like converting vertices and writing our own (extremely one-case-use) shader.

\$\endgroup\$
8
  • \$\begingroup\$ I just thought that if I create a model like that in blender and use the UV map for every face of the cube I will get 6 parts so by going into each one of them I would get the result I want. I also have a method to get the texture from Blender:/n foreach (ModelMesh mesh in model.Meshes){ foreach (ModelMeshPart part in mesh.MeshParts){ BasicEffect basicEffect = part.Effect as BasicEffect; if (basicEffect != null) modelTextures[part.GetHashCode()] = basicEffect.Texture; part.Effect = material.effect;}} /n Am I completely wrong by thinking that way? \$\endgroup\$
    – alecnash
    Commented Oct 26, 2012 at 13:08
  • \$\begingroup\$ comment looks ugly I'll edit the question \$\endgroup\$
    – alecnash
    Commented Oct 26, 2012 at 13:13
  • \$\begingroup\$ Does modelTextures[part.GetHashCode()] work properly? Because part.GetHashCode() operates over the entire domain of int, so I'm not sure if that will always be within the bounds of the modelTextures array. Do you have multiple meshes or meshparts right now in the model? Because if you change the texture per meshpart it would work. \$\endgroup\$
    – Roy T.
    Commented Oct 26, 2012 at 13:32
  • \$\begingroup\$ the GetHashCode() works fine. I debug it and I get 6 parts so I suppose the textures should be there. In blender I also have 6 different materials. I am doing something wrong there? \$\endgroup\$
    – alecnash
    Commented Oct 26, 2012 at 13:37
  • \$\begingroup\$ I'd still keep an eye on GetHashCode() since it wasn't designed to be a linear index starting at 0. But have you been able to draw the 6 textures you got from the using a sprite batch? Or drawing the part using 2 different textures that you've loaded manually? \$\endgroup\$
    – Roy T.
    Commented Oct 26, 2012 at 15:27

You must log in to answer this question.

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