1
\$\begingroup\$

I'm having some problem to draw textures with class "DrawUserIndexPrimitive" on Xna.

I can draw my cubes / models without problem. But I want to draw different textures on my cubes. I loop on each "ID" of a different texture applied to my different cube. But it does not work. I apply every time buffer and vertexPosition with IndexBuffer different from my class "GraphicData."

the final texture to be applied is the same on all my cubes

thank you

Here is my code for function "Draw"

public void drawWorld(GameTime gameTime) 
    { 
        if (needToDraw) 
        { 
            int index = 0; 
            foreach (GraphicData data in graphicData) 
            { 

                effet.LightingEnabled = false; 
                effet.VertexColorEnabled = false; 
                effet.TextureEnabled = true; 
                effet.FogEnabled = false; 
                effet.FogStart = arcadia.camera.NearPlane; 
                effet.FogEnd = arcadia.camera.FarPlane; 
                effet.FogColor = new Vector3(1, 1, 1); 
                effet.View = arcadia.camera.View; 
                effet.Projection = arcadia.camera.Projection; 

                if (index == 0) 
                { 
                    effet.Texture = Game.Content.Load<Texture2D>("Models\\Ground\\Land\\ground_land_text1"); 
                } 
                else 
                { 
                    effet.Texture = Game.Content.Load<Texture2D>("Models\\Ground\\Land\\ground_land_text2"); 
                } 
                foreach (EffectPass pass in effet.CurrentTechnique.Passes) 
                { 
                    pass.Apply(); 
                    //Initialize et envoie à la carte graphique le buffer et les vertices 
                    if (data.vertexBuffer != null && data.indexBuffer != null) 
                    { 
                        Game.GraphicsDevice.SetVertexBuffer(data.vertexBuffer); 
                        Game.GraphicsDevice.Indices = data.indexBuffer; 
                        Game.GraphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, data.vertexPosition, 0, data.vertexPosition.Length, data.indices, 0, data.indices.Length / 3); 
                    } 

                } 
                index++; 
            } 
        } 


    } 
\$\endgroup\$
3
  • \$\begingroup\$ Im not 100% sure, but maybe try using a single effect for every object. If the whole thing is batched the actual draw will happen AFTER you changed the effect back and forth and therefore only apply the last texture set. \$\endgroup\$
    – floAr
    Mar 22, 2013 at 9:45
  • \$\begingroup\$ That seems like a good idea! I'll try it for lunch and back to keep you informed of the results! \$\endgroup\$ Mar 22, 2013 at 9:58
  • \$\begingroup\$ Yes I have tried now but unfortunately it does not change anything ...To believe that I really have no chance. Only the last texture is applied everywhere \$\endgroup\$ Mar 22, 2013 at 12:02

1 Answer 1

1
\$\begingroup\$

I used this in a similar situation (drawing a cube, each side with a different texture) and the texture changes properly. It's probably a little sloppy but it works:

        effect.Texture = someTexture; 
        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {                                             
            pass.Apply();
            mainRoom.DrawFace(GraphicsDevice, frontFaceVertices);
        }

        effect.Texture = someDifferentTexture;
        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {                
            pass.Apply();
            mainRoom.DrawFace(GraphicsDevice, backFaceVertices);
        }

//where .DrawFace draws the vertices
    public void DrawFace(GraphicsDevice device, VertexPositionNormalTexture[] vertices)
    {
        // Create the shape buffer and dispose of it to prevent out of memory
        using (VertexBuffer buffer = new VertexBuffer(device, VertexPositionNormalTexture.VertexDeclaration, NUM_VERTICES, BufferUsage.WriteOnly))
        {
            buffer.SetData(vertices);
            device.SetVertexBuffer(buffer);
            device.SamplerStates[0] = SamplerState.LinearClamp;
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, NUM_TRIANGLES_PER_FACE);
        }
    }

Instead of using the index to check which texture to use, provide your data class with the index of a texture to draw, and use an if statement to determine which texture should be drawn.

Also, loading textures is very slow, it is much better to load them once into a variable.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you very much for this example well explained. Exactly what I expected as an example. This is perfect \$\endgroup\$ Apr 3, 2013 at 6:44

You must log in to answer this question.

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