Applying image to Geometry

Mar 10, 2011 at 6:00 AM

I'm trying to apply a image to a simple geometry.   It's just a flat square for now.  But whenever I apply the image, all I get is a fuzzy halo around it.  I've tried to figure out from the SampleBrowser how to apply it, but can't quite figure out the last mile.

        public void LoadTexture(string PuzzleName, int PuzzleSize)
        {
                var uri = string.Format("{0}/{1}/{2}/{3}.png"Constants.PuzzleMeshPath, PuzzleName, PuzzleSize, Id);
                var image = Runtime.Instance.ContentManager.Load<Image>(uri);
                var imageMap = new ImageMap(image);
                var material = new Material()
                {
                    DiffuseMap = imageMap,
                    Shade = MaterialShade.Gouraud,
                    DoubleSided = false
                };

                Material = material;
        }

I've checked and the image seems to be loaded fine.  It has picked up the right X/Y size for it.  I've also just tried something like this and it make it blue.  So the problem is with the DiffuseMap:

			var material = new Material
                {
                Diffuse = Colors.Blue,
                Shade = MaterialShade.Gouraud,
                DoubleSided = true
                };

Any thoughts?
Coordinator
Mar 15, 2011 at 6:00 PM

Sorry for getting back to you so late.

Could you drop in a screenshot of the problem and also include the texture as a reference as well, easier to understand.

Mar 16, 2011 at 2:01 AM

I've attached a screeshot of 3 meshes:

http://www.flickr.com/photos/9381372@N08/5530300061

The item on the left is a box with the image:

    <Geometries:Box x:Name="myBox" Dimension="30,30,5" Position="-100,100,0">
        <Geometries:Box.Material>
            <Materials:Material Ambient="Black" Diffuse="Green" Specular="White" Shade="Gouraud" DiffuseMap="/Asgard.RIA;component/Assets/PuzzleMesh/Sphere/8/1.png"/>
        </Geometries:Box.Material>
    </Geometries:Box>

The one in the middle is a completely custom mesh generated using this code:

/// <summary>
        /// Generates the vertices/faces for the puzzle piece
        /// </summary>
        /// <param name="viewport"></param>
        public override void Prepare(Viewport viewport)
        {
            HalfPuzzleSize = _puzzle.HalfPuzzlePieceSize;

            GenerateVertices();
            GenerateFaces();
            LoadTexture();
            GeometryHelper.CalculateNormals(FullDetailLevel);

            base.Prepare(viewport);
        }
/// <summary>
        /// Generates the vertices.
        /// There are for lines in the shape of square
        /// </summary>
        protected override void GenerateVertices()
        {
            var topLeft = new Vertex(-HalfPuzzleSize, HalfPuzzleSize, HalfPuzzleSize);
            var topRight = new Vertex(HalfPuzzleSize, HalfPuzzleSize, HalfPuzzleSize);
            var botRight = new Vertex(HalfPuzzleSize, -HalfPuzzleSize, HalfPuzzleSize);
            var botLeft = new Vertex(-HalfPuzzleSize, -HalfPuzzleSize, HalfPuzzleSize);

            FullDetailLevel.AllocateVertices(4);
            FullDetailLevel.SetVertex(0, topLeft);
            FullDetailLevel.SetVertex(1, topRight);
            FullDetailLevel.SetVertex(2, botRight);
            FullDetailLevel.SetVertex(3, botLeft);
        }


        /// <summary>
        /// Generates the faces.
        /// There are two triangles, which covers the square
        /// </summary>
        protected override void GenerateFaces()
        {
            FullDetailLevel.AllocateFaces(2);
            FullDetailLevel.SetFace(0, new Face(3, 1, 0));
            FullDetailLevel.SetFace(1, new Face(3, 2, 1));
        }

/// <summary>
        /// Loads the texture for the puzzle piece
        /// </summary>
        public void LoadTexture()
        {
            try
            {
                var uri = string.Format("{0}/{1}/{2}/{3}.png"Constants.PuzzleMeshPath, _puzzle.PuzzleName, _puzzle.PuzzleSize, Id);
                var image = Runtime.Instance.ContentManager.Load<Image>(uri);
                var imageMap = new ImageMap(image);
                var material = new Material()
                {
                    DiffuseMap = imageMap,
                    Shade = MaterialShade.Gouraud,
                    Ambient = Colors.Black,
                    DoubleSided = true
                };
                material.Ambient = Colors.Black;
                material.Diffuse = Colors.Green;
                material.Specular = Colors.White;

                Material = material;
                // Is this needed?
                //InvalidatePrepare();
            }
            catch (Exception ex)
            {
                LogWriter.Error("Error loading texture for " + _puzzle.PuzzleName + " of size " + _puzzle.PuzzleSize + " Id:" + Id, ex);
            }
        }

The last one is a hybrid where part of it in XAML and part of it is in code.  I was trying to see if applying the material in XAML would make any difference.

            <Piece:SpherePuzzlePiece x:Name="newPuzzle" Id="1" Position="100,100,10">
                <Piece:SpherePuzzlePiece.Material>
                    <Materials:Material Ambient="Black" Diffuse="Green" Specular="White" Shade="Gouraud" DiffuseMap="/Asgard.RIA;component/Assets/PuzzleMesh/Sphere/8/1.png"/>
                </Piece:SpherePuzzlePiece.Material>
            </Piece:SpherePuzzlePiece> 

You can see this weird halo around the box, but no actual map.  I'm stumped as to why.