Loading a texture onto a model.

Dec 5, 2013 at 4:04 PM
Edited Dec 5, 2013 at 4:06 PM
I understand the way to apply basic colours, but what about when we have a model with a texture. How can we load the texture at runtime please ?

Eventually I will be moving vertices around, so I need to know how to load the textures dynamically.


I can understand this code taken from the SimpleDemo :
            // Create some materials
            var greenMaterial = MaterialHelper.CreateMaterial(Colors.Green);
            var redMaterial = MaterialHelper.CreateMaterial(Colors.Red);
            var blueMaterial = MaterialHelper.CreateMaterial(Colors.Blue);
            var insideMaterial = MaterialHelper.CreateMaterial(Colors.Yellow);

            // Add 3 models to the group (using the same mesh, that's why we had to freeze it)
            modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Material = greenMaterial, BackMaterial = insideMaterial });
            modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(-2, 0, 0), Material = redMaterial, BackMaterial = insideMaterial });
            modelGroup.Children.Add(new GeometryModel3D { Geometry = mesh, Transform = new TranslateTransform3D(2, 0, 0), Material = blueMaterial, BackMaterial = insideMaterial });
How do I do this same thing but with an image, for example I am trying to load a .PNG image....

Many thanks !
Dec 5, 2013 at 5:20 PM
Edited Dec 5, 2013 at 5:21 PM
Here's where I am at now with this.

I load a .OBJ model that has 7 children, the first child 0 is the mesh of the hair.

I'm loading my 'start' model like this :
ObjReader CurrentHelixObjReader = new ObjReader();
start = CurrentHelixObjReader.Read("C:/Users/Roger/Desktop/head/base_med_.obj");
So I load the OBJ fine but when I try to grab the material of the original model child 0 (ie the hair) like this :
Material matty = (MaterialGroup)((GeometryModel3D)start.Children[0]).Material;
It gives me an error :
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: 'The invocation of the constructor on type 'HelixTrial.MainWindow' that matches the specified binding constraints threw an exception.' Line number '4' and line position '9'.
I can remove the line and get the model to load, but I need to get hold of the start up material for the hair so that I can apply it later when I alter the mesh via morph targets and rebuild it.

So I would be trying to apply 'matty' like so (partial code) when I rebuild the model :
final.Children.Add(new GeometryModel3D { Geometry = mes, Material = matty });
That last line is part of a loop that rebuilds the Model3DGroup for display in the viewport.

I hope I made sense ! Anyone care to tell me what I am doing wrong ? Here's a picture if it helps visualise... it's just a starting point, I haven;t looked at transparency for the hair at all yet.

Image
Dec 5, 2013 at 8:04 PM
I must stop asking questions so quickly, I managed to piece it together ! But for anyone else this is what you can do :

To load a material image you can simple do this :
Material myMaterial = MaterialHelper.CreateImageMaterial("path/to/image/image.jpg", 1);
The '1' is opacity.

And in my particular problem of getting an existing model's texture you can do this :

Instead of my using :
Material matty = (MaterialGroup)((GeometryModel3D)start.Children[0]).Material;
I did this :
Material anotherMaterial = ((GeometryModel3D)start.Children[0]).Material;
Now I have grabbed the hair texture from the original model and can use it later to reapply it when needed.

I do still have an icky problem with the hair and it's transparency. The original model used TGA images. I am not sure exactly how these compare to PNG, but I used an online converter and converted it to PNG. The texture loads but it's not rendering quite right.

Image

As can be seen it doesn't appear to be rendering the transparent areas as see-through.

Any ideas on fixing that please ?