Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changes between Initial Version and Version 1 of content/Shader


Ignore:
Timestamp:
Nov 12, 2008, 2:46:15 PM (16 years ago)
Author:
janise
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • content/Shader

    v1 v1  
     1
     2= Shader =
     3
     4
     5With this page I want to give some insights about shaders in Ogre in order to help anybody willing to implement some into orxonox. The Page does not really deal with writing shader programs but mainly with using them.
     6
     7
     8
     9
     10== What are shaders? ==
     11
     12Shaders are external code fragments that can be used to create graphical rendering effects. Famous examples are bump and normal mapping, water effects or HDR.
     13
     14
     15There are three major languages that are used to write shader programs:
     16- HLSL (High Level Shader Language, can only be used with DirectX)[[BR]]
     17- GLSL (OpenGL Shader Language, can only be used with OpenGL)[[BR]]
     18- CG (by Nvidia, similar to GLSL, can be used for OpenGl and DirectX with an appropriate compiler: [http://developer.nvidia.com/object/cg_toolkit.html]
     19
     20
     21
     22Two types of shader programs exist:
     23
     24- Vertex shader: Is run once for each vertex given to the graphics processor. The purpose is to transform each vertex's 3D position in virtual space to the 2D coordinate at which it appears on the screen. Vertex shaders can manipulate properties such as position, color, and texture coordinate.
     25
     26- Pixel shader (or fragment shader): Takes the output from the vertex shader and colorizes every individual pixel.
     27
     28
     29
     30----
     31
     32
     33== Implementing Shaders ==
     34
     35For every shader we have to write a material file in Ogre where we need to define the shader programs we are going to need and their parameters. Then we can use those shader programs in the passes of the material file.
     36
     37'''Basic Example:''' (taken from the Ogre wiki)[[BR]]
     38This Material simply applies a texture to an Object using shaders
     39
     40
     41''PlainTexture.material''
     42{{{
     43// CG Vertex shader definition
     44vertex_program PlainTexture_VS cg                       
     45{
     46        // Look in this source file for shader code
     47        source GameObjStandard.cg
     48        // Use this function for the vertex shader                     
     49        entry_point main_plain_texture_vp       
     50        // Compile the shader to vs_1_1 format 
     51        profiles vs_1_1                                 
     52
     53        // This block saves us from manually setting parameters in code
     54        default_params                                 
     55        {
     56                // Ogre will put the worldviewproj into our 'worldViewProj' parameter for us.
     57                param_named_auto worldViewProj worldviewproj_matrix             
     58                // Note that 'worldViewProj' is a parameter in the cg code.
     59        }
     60}
     61
     62// CG Pixel shader definition
     63fragment_program PlainTexture_PS cg                     
     64{
     65        // Look in this source file for shader code
     66        source GameObjStandard.cg               
     67        // Use this function for the pixel shader       
     68        entry_point main_plain_texture_fp       
     69        // Compile to ps_1_1 format     
     70        profiles ps_1_1                                 
     71}
     72
     73material PlainTexture
     74{
     75        // Material has one technique
     76        technique                                       
     77        {
     78                // This technique has one pass
     79                pass                                   
     80                {
     81                        // Make this pass use the vertex shader defined above
     82                        vertex_program_ref PlainTexture_VS     
     83                        {
     84                        }
     85                        // Make this pass use the pixel shader defined above
     86                        fragment_program_ref PlainTexture_PS   
     87                        {
     88                        }
     89                        texture_unit
     90                        {
     91                                // This pass will use this 2D texture as its input
     92                                texture Ten.png 2d             
     93                        }
     94                }
     95        }
     96}
     97}}}
     98
     99''GameObjStandard.cg''
     100{{{
     101void main_plain_texture_vp(
     102                // Vertex Inputs
     103                float4 position         : POSITION,     // Vertex position in model space
     104                float2 texCoord0        : TEXCOORD0,    // Texture UV set 0
     105
     106                // Outputs
     107                out float4 oPosition    : POSITION,     // Transformed vertex position
     108                out float2 uv0          : TEXCOORD0,    // UV0
     109
     110                // Model Level Inputs
     111                uniform float4x4 worldViewProj)
     112{
     113        // Calculate output position
     114        oPosition = mul(worldViewProj, position);
     115
     116        // Simply copy the input vertex UV to the output
     117        uv0 = texCoord0;
     118}
     119
     120void main_plain_texture_fp(
     121                // Pixel Inputs
     122                float2 uv0              : TEXCOORD0,    // UV interpolated for current pixel
     123                         
     124                // Outputs
     125                out float4 color        : COLOR,        // Output color we want to write
     126                         
     127                // Model Level Inputs
     128                uniform sampler2D texture)              // Texture we're going to use
     129{
     130        // Just sample texture using supplied UV
     131        color = tex2D(texture, uv0);
     132}
     133}}}
     134
     135The keyword ''uniform'' identifies the parameters you will have to supply in the material file.
     136
     137    * Some parameters can be supplied by the Ogre Engine (''param_named_auto''), like a light position.
     138    * Some parameters can be supplied by your material file (''param_named'') like a texture map.
     139    * Some parameters can be supplied by your application (''custom'').
     140
     141We can now apply this material file to an entity somewhere in our code:
     142
     143{{{
     144    MyEntity->setMaterialName( "PlainTexture" );
     145}}}
     146
     147Instead of just passing on the texture color to the output in the last line of the pixel shader you can imagine that we could do all kinds of funny things with it first. For instance changing the brightness of each pixel depending on a second normal map texture and the position of the viewer to create an illusion of a surface structure.