18. ShadersLesson 18
Shaders
Level:
experienced Version of the Xtreme3D: 3.0.x
Author: Gecko
One of the most interesting features Xtreme3D is a shader. The notion of "shader" here is wider than the other engines. Usually this term is used to refer the program to the GPU that are performed for each top model, or for each pixel of the model on the screen. Such shaders in the Xtreme3D also has a (see next chapter), but in a general sense, shader special called, or modifies computer software replaces a material to which it is attached. Some of these special effects work, and on the old graphics cards that do not support ��������� program, and some are based on the built-in engine programs.
Using shaders can be superimposed on the object several materials, render the contours of the object, make the object embossed or give it the "effect of comic books." Consider all possibilities of built-in shaders Xtreme3D within a single lesson is impossible, so we stop at the one-������� relief (Bump Shader).
The effect of relief greatly increases the realism of models - it is used in the games for more than 10 years of age and over the years has become the de-facto standard. ����������� is usually achieved by using the method of normal mapping (Normal projection). This method is based and Bump Shader in the Xtreme3D. The essence of the normal mapping in that normal is set for each point of the surface (in contrast to the normal vertex lighting, where the normal set for each of the peaks, and then simply interpolated over the surface of the polygon. This is done using normal maps normal map) - A special texture, in which the color of pixels compared to the normal vectors (RGB = XYZ). Normal Map can be generated from the map heights or from ������������������� geometry by tracing - This function is virtually all professional packages of 3D-modeling.
![](data/normalmap.jpg)
The normal map was relatively invariant speed and transfer model (i.e., remained unchanged in these transformations), its set in a special space, called the space of the tangent (tangent space). In this space, the coordinate axis Z is perpendicular to the surface, and the X and Y axes, respectively, are mutually perpendicular to the tangents to the surface. The lighting also calculated in the space of the tangent - direction of light is transformed in this space using a special matrix, which is called TBN on the first letters of its components - the tangent and BINORMAL, Normal �������, ���������, normal). Normal here - the usual normal tops, and the ������� and ��������� - vectors, perpendicular to the normal and perpendicular to each other. These vectors calculates the Xtreme3D. Currently, they are supported only for objects of type Freeform.
Despite the quite complicated for beginners the theoretical base, use the terrain effect in the Xtreme3D is very easy - all the complexity of the realization of the hidden under the convenient API.
First, create materials with the necessary textures:
MaterialCreate('mBumpDiffuse', 'diffuse.png');
MaterialCreate('mBumpNormal', 'normal.png');</A
Now create the terrain shader and convey to him the textures:
Bump BumpShaderCreate =();
BumpShaderSetDiffuseTexture(bump, 'mBumpDiffuse');
BumpShaderSetNormalTexture(bump, 'mBumpNormal');
BumpShaderSetMaxLights(bump, 3);
BumpShaderSetMaxLights function defines the number of light sources, which should take into account the shader. Recall that the Xtreme3D supports up to 8 light sources - the same applies to the terrain shader.
Now you can create material and attach to him our shader:
MaterialCreate('mBump', '');
MaterialSetAmbientColor('mBump', c_black, 1);
MaterialSetDiffuseColor('mBump', c_white, 1);
MaterialSetSpecularColor('mBump', c_ltgray, 1);
MaterialSetShininess('mBump', 32);
MaterialSetShader('mBump, bump);