| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Articles MaterialLibrary

Page history last edited by PBworks 18 years, 7 months ago

Material Library

The material library contains a list of materials.It is these materials that will be assigned to scene objects.Materials contained in the material library can be created and modified during runtime and design time.Why use the material library

1) Reuse of materials. Create a material once and use it on different objects. This saves memory and code. The disadvantage of this is that any changes you make to the material in the library, will be reflected by all the objects that use that material. This is only a disadvantage when you want to change the material of one object, but want to keep the material settings on the other objects that use the same material. This could also be a advantage. When you want to have the materials of objects to change in sync, changing it in the material library once and have the effect take place with all objects saves a lot of hassle.

2) The materials defined in the library has some properties not available on normal scene object's material property.

     a) TextureScale: Define the X, Y and Z scale of the texture.

     b) TextureOffset: Define the X, Y and Z offset of the texture.

     c) Texture2Name: This property is used for multi texturing. Supply the name of the second texture to be applied. This second texture must be in the material library. Using these additional properties will allow you to create fantastic textural special effects.

3) When loading a 3D mesh object. Some mesh objects might have texturing defined in the objects data structure. To apply these textures to the mesh object, these textures have to extracted so it can be used as part of the rendering procedure of the mesh object. These textures has to be extracted to a material library for rendering. For more details see the article about loading 3D mesh objects.

Predefined material library

Even though you can setup material for the library during runtime and design time, there is still a third way of preparing materials for your application. The third way is what I call "Predefined material libraries". This is when you have create a standard material library that can be used in a number of applications. As you might not want to re-define the libraries materials over and over for each project, you can set it up once and load it from resourceas required. This resource can be a materials file, a resource DLL or any other resource structure you choose.

How do I do this? Simple, the material library has SaveToFile, LoadFromFile, SaveToStream and LoadFromStream methods to save the library and load it when required. Recommended extension when saving a material library to file is ".GLL"

Setting up material library during design time

Place material library on form or datamodule. Double clicking on the material library component will open the materials list editor.From here, you can add and remove materials to the library.

Add a material to the list. Selecting a material in the materials list editor, you will notice that material options become available in the object inspector.Give the material a appropriate name.One of the published properties of the created material is "Material". Click on the ellipses button of the material to open a material design time property editor.

From here you can:

1) Set material properties.

2) Apply textures to material.

3) Set texturing properties.

4) View effect of material being setup on scene objects.

When done, click on the OK button to save changes to material and close the editor.

Setting up material library during run time

Most of the time materials are created for the library during design time. In these cases, the only runtime interaction with the materials modification is to assign a texture to the texture property of the material. Examples of this can be seen in almost all GLScene demos that use material libraries and textures.Because of this, we will not spend any more time on this scenario, but rather spend our time on dynamic creation and manipulation of materials.

Creating material object for library

To create a material to be used in the library:

1) Create a local variable of type TGLLibMaterial.

2) Create the TGLLibMaterial. Use the normal constructor method to do this. You will notice that the parameter for the create method is of type TCollection. Pass the material library's materials property as the parameter.

         e.g. lMaterial := TGLLibMaterial.Create(GLMaterialLibrary1.Materials);

3) Name the material.

4) Set the materials properties as required. In this case we want to make the front property's diffuse color brown.

        lMaterial.Material.FrontProperties.Diffuse.Color := clrBrown;

5) Assign the material to a scene object. (If required).

     Full example:

     var

       lMaterial : TGLLibMaterial;

     begin

       {Create material}

       lMaterial := TGLLibMaterial.Create(GLMaterialLibrary1.Materials);

       lMaterial.Material.FrontProperties.Diffuse.Color := clrBrown;

       lMaterial.Name := 'matbrown';

       {Set material to scene object}

       GLCube1.Material.MaterialLibrary := GLMaterialLibrary1;

       GLCube1.Material.LibMaterialName := 'matbrown';

       GLCube1.NotifyChange(GLCube1);

     end;

Applying materials in library to scene objects

After you have setup the materials to be used in your scene, you might want to assign them to the relevant scene objects.To do this, you have to assign the material library to the scene object and give the name of the material to be used.Each scene object that directly or indirectly inherit from TGLSceneObject, will have a material property.It is in this property where the material library and material name has to be assigned. See full example above.

CRS

Comments (0)

You don't have permission to comment on this page.