Home Articles Tutorials Resources About

Tutorial 7 - Loading Meshes


By Pieter Germishuys, January 4 2006

Loading models is what we are going to do today. We are going to be using the .x file format for simplicity and use the set of helper classes and functions in direct3d.
What is a model?

a Model is a series of vertices that define a mesh and that being a series of polygons grouped together to form a surface.


What do we need to know?
We will need to remember the following steps
1 . a Mesh object
2 . a Texture object
3 . a Material associated with the texture

What this means is the following, When we create a mesh we want to make it look nice and realistic so we assign a texture to it. We also want light to react differently to certain objects/entities in our game, sometimes we want smoothness and sometimes we want our objects to give off a glow, this is what our material does.

The code
Declaring your objects.

private ExtendedMaterial[] eMaterials = null;
private Material[] materials;
private Texture[] textures;
private Mesh mesh;
private Matrix matWorld;


The constructor that will initialize our objects and load our mesh.
public Core()
{
????window = new Window();
????//Set 1 Light
????window.D3DDevice.Lights[0].Type = LightType.Directional;
????window.D3DDevice.Lights[0].Direction = new Vector3(0.0f, 0.0f, 1.0f);
????window.D3DDevice.Lights[0].Enabled = true;

????//Enable lighting in our engine
????window.D3DDevice.RenderState.Lighting = true;

????//Open the mesh. This mesh can be found in your samples directory of the DirectX SDK
????//ExtendedMaterials:
????//Returns material information that is saved in DirectX (.x) files.

????mesh = Mesh.FromFile("tiny.x", MeshFlags.Managed, window.D3DDevice, out eMaterials);
????//Create an array of textures (Our model/mesh may have different textures assigned to it)
????textures = new Texture[eMaterials.Length];
????//Create an array of materials (Our model/mesh may have different materials assigned to it)
????materials = new Material[eMaterials.Length];
????for(int i = 0; i < materials.Length; i++)
????{
????????//Retrieves or sets the Material structure that describes the material properties.
????????materials[i] = eMaterials[i].Material3D;
????????//Retrieves or sets a string that specifies the file name of the texture.
????????//Load the texture from the filename retrieved.

????????textures[i] = TextureLoader.FromFile(window.D3DDevice, eMaterials[i].TextureFilename);
????}
}

Render your scene.
private void Render()
{
????window.BeginRender(Color.Black);
????//for the amount of materials we have, set each texture/material and render the mesh
????for(int i = 0;i < materials.Length; i++)
????{
????????window.D3DDevice.Material = materials[i];
????????window.D3DDevice.SetTexture(0, textures[i]);
????????/* Draws a subset of a mesh:
????????* An attribute table is used to identify areas of
????????the mesh that need to be drawn with different textures,
????????render states, and materials. The application can use
????????the attribute table to hide portions of a mesh by
????????leaving out a given attribute identifier (attributeID)
????????when drawing the frame. Attribute tables
????????can also be used for application defined purposes.*/

????????mesh.DrawSubset(i);
????}
????window.FinishRender();
}



Files for this tutorial

Filename Size
? tut7.rar 655.0 KB
?
MDX info is an initiative by vector4. All content is copyright ? 2005-2006 by its respective authors | About MDX info | Terms of Use |
Coming soon!