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 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;

                }
            }
share|improve this question
add comment

1 Answer

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.

share|improve this answer
    
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? –  alecnash Oct 26 '12 at 13:08
    
comment looks ugly I'll edit the question –  alecnash Oct 26 '12 at 13:13
    
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. –  Roy T. Oct 26 '12 at 13:32
    
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? –  alecnash Oct 26 '12 at 13:37
    
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? –  Roy T. Oct 26 '12 at 15:27
show 3 more comments

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.