All object in Chilli Source are rendered with a Material. A material essentially describes the render state for an object. This includes properties such as the Texture, the Shader, blending and how the object should be culled. Objects will be batched by material when rendering reducing the number of render state changes for performance. Fewer materials means a more efficient app.
Typically when using a material the shader being used is not specified, instead this is handled for you by creating a material of a specific type. For example a StaticBlinn material can be created which will use the engines internal shaders to render a static mesh with blinn-phong shading. If different shaders are required a custom material should be created.
Materials can be created either from file or in code. When creating from file they are loaded from a csmaterial file – a basic xml file format. The following is an example of a csmaterial file for rendering a static mesh with blinn-phong shading.
This can then be loaded like any other Resource:
CSCore::ResourcePool* resourcePool = CSCore::Application::Get()->GetResourcePool();
CSRendering::MaterialCSPtr material = resourcePool->LoadResource(CSCore::StorageLocation::k_package, "Materials/Blinn.csmaterial");
Alternately materials can be created in code. They can be created through the Resource Pool like other Resources but it is usually easier to create them using the Material Factory. This allows them to be created with the engine’s internal shaders.
CSCore::ResourcePool* resourcePool = CSCore::Application::Get()->GetResourcePool();
CSRendering::TextureCSPtr texture = resourcePool->LoadResource(CSCore::StorageLocation::k_package, "Textures/Texture.png");
CSRendering::MaterialFactory* materialFactory = CSCore::Application::Get()->GetSystem();
CSRendering::MaterialSPtr material = materialFactory->CreateStaticBlinn("UniqueId", texture);