Advanced Model Loading

Embedded Resources

In addition to specifying a path to an Ascend (*.asc) model in the Loader.Load method, Ascend also supports specifying a Stream object. This capability allows the developer to load models that are compiled into the project as embedded resources. There are two ways to embed and retrieve these resources: via project properties and via file properties.

Project Properties

The first and simplest way to embed resources is via a project's properties page (accessible by right-clicking a project in the solution). By clicking on the Resources tab, the developer can add resources of any type to the project and give them names. For an Ascend model, the developer should add the appropriate model file and any relevant textures. The Loader.Load method has an optional parameter of type Dictionary<string, Bitmap> that is used by the Ascend loader to resolve textures defined in the original model file. The code below describes how to load an embedded Ascend model called bird that uses a texture called bird_texture that is also embedded:

namespace MyProject
{
    using (Stream birdStream = new MemoryStream(MyProject.Properties.Resources.bird))
    using (Bitmap birdTexBitmap = MyProject.Properties.Resources.bird_texture)
    {
        ModelSceneNode model = Loader.Load(birdStream,
            new Dictionary<string, Bitmap> { { "bird_texture.jpg", birdTexBitmap } });
    }
}

It is important that the keys in the texture dictionary match the file names of any required model textures and that they map correctly to their corresponding resources. If model textures are missing or mismatched, ensure that the dictionary and resources are set up properly.

File Properties

The next way to embed and retrieve an Ascend model is via the file properties page (accessible by right-clicking a file in the solution). For a file that already exists in the solution, the developer can change the Build Action property of the file to Embedded Resource. Though similar in functionality to the technique described above, a resource embedded in this manner must be retrieved via the .NET Framework Assembly.GetManifestResourceStream method (located in System.Reflection.dll) instead of the project's Properties.Resources object. The following code shows how to load an embedded Ascend model file called bird.asc that uses a texture file called bird_texture.jpg that is also embedded (note that in this case the texture dictionary is of type Dictionary<string, Stream> instead of Dictionary<string, Bitmap>):

namespace MyProject
{
    Assembly assembly = Assembly.GetExecutingAssembly();
    
    using (Stream birdStream = assembly.GetManifestResourceStream("MyProject.bird.asc")
    using (Stream birdTexStream =
        assembly.GetManifestResourceStream("MyProject.bird_texture.jpg")
    {
        ModelSceneNode model = Loader.Load(birdStream,
            new Dictionary<string, Stream> { { "bird_texture.jpg", birdTexStream } });
    }
}

< PREVIOUS: Appendix
 

Last edited Feb 26, 2014 at 5:47 PM by menehune23, version 14

Comments

No comments yet.