Main > Features > Materials

Getting to know materials API is totally optional. One can simply use
default materials without even knowing they're there.

However, if you'd like to customize rendering of your sprites or add
new post-processing filter you have powerful material API at your fingertips.

Basically Tiny2D materials are very similar to DirectX Effect (FX) files.

Every material is described in a simple XML file like this:

<?xml version="1.0"?>
<material>
  <technique name="my_technique" blending="additive">
    <shader type="vertex" path="my_shaders.fx" entry="my_vertex_shader_main"/>
    <shader type="fragment" path="my_shaders.fx" entry="my_fragment_shader_main"/>
  </technique>
  <technique name="another_technique" blending="alpha">
    <shader type="vertex" path="my_shaders.fx" entry="my_vertex_shader_main"/>
    <shader type="fragment" path="my_shaders.fx" entry="other_fragment_shader_main"/>
  </technique>
</material>

Each material can have multiple techniques selectable at runtime by name
(or better by cached index). Each technique links together one GLSL vertex
shader and one GLSL fragment shader and for each one specifies shader file
as well as, optionally, the name of the entry function (by default 'main').

Blending can be specified per technique as done above. Supported values are:
* additive (i.e. one, one)
* alpha (standard alpha blending i.e. source-alpha, inversed-source-alpha)
* none (blending disabled)

At runtime you can set variables defined in the shader using
Material::Set*Parameter() functions by name (or better by cached index).

Normally GLSL doesn't allow multiple shaders in a single file but with
Tiny2D one can simply separate them with special '###splitter###' string.

Individual shaders can't share any code directly but you can do so thanks to
Tiny2D support for #include directive in shader files.

Here is sample shader file that is taking advantage of both features.

Note: shaders are expected to be written using official OpenGL GLSL syntax,
regardless of whether they're to be run on OpenGL or OpenGLES internally.
Tiny2D automatically converts minor ES GLSL language differences into GLSL
compliant format - including:
* removal of #version directive
* adding precision qualifiers if not specified
* replacing 'in' and 'out' keywords with 'varying'
* declaring GLSL built-in attributes like 'gl_Vertex' or 'gl_FragColor' as vec4

But if really needed you can still do ES specific code thanks to 'GLES' macro.
For example:

#ifdef GLES
	vec4 color = vec4(1, 0, 0, 1); // red on mobile devices
#else
	vec4 color = vec4(0, 1, 0, 1); // green on desktop
#endif

Sample Code

// Declaration

Material material;

// Initialization (from my_material.material.xml)

material.Create("my_material");

// Drawing

material.SetTechnique("my_technique");
material.SetFloatParameter("my_parameter", 0.5f);
material.DrawFullScreenQuad();




Contact: contact at pixelelephant dot com
(C) 2013 Pixel Elephant - All Rights Reserved