Writing a simple custom rendering shader

A minimal RED::RenderShader is composed of:

The following code sample shows the construction of such a minimal shader:

// Use RED::ShaderString for ARB shader programming.
RED::RenderShader shader;
RED::ShaderString vsh, psh;
RED::ShaderProgramID vshid, pshid;
RED::RenderCode rcode;

// Binding of the vertex channel:
rcode.BindChannel( RED_VSH_VERTEX, RED::MCL_VERTEX );

// Vertex shader program:
vsh.VertexTransform( "result.position", "state.matrix.program[2]", "vertex.attrib[0]" );

// Pixel shader program:
psh.Add( "MOV result.color, { 1.0, 0.0, 0.0, 1.0 };\n" );

// Load the shaders from strings:
RC_TEST( iresmgr->LoadShaderFromString( vshid, vsh ) );
RC_TEST( iresmgr->LoadShaderFromString( pshid, psh ) );

// Set the render code and the programs:
RC_TEST( shader.SetRenderCode( rcode, RED_L0 ) );
RC_TEST( shader.SetVertexProgramId( vshid, RED_L0, resmgr ) );
RC_TEST( shader.SetPixelProgramId( pshid, RED_L0, resmgr ) );

First the render code is used to bind the mesh vertex channel to the shader input channel.

Then the vertex and pixel shader programs are written in ARB using a RED::ShaderString helper object. The vertex program simply transforms the vertex position from the world space to the screen space. The pixel program just paints the pixels in red.

Finally all the data are registered in the RED::RenderShader.

In the previous sample we wrote the programs in ARB. The next sample shows the same programs written in GLSL:

// Use simple strings for GLSL programs:
RED::String vsh_glsl;
RED::String psh_glsl;

// Vertex shader program:
vsh_glsl  = "void main()\n";
vsh_glsl += "{\n";
vsh_glsl += "gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n";
vsh_glsl += "}\n";

// Pixel shader program:
psh_glsl  = "void main()\n";
psh_glsl += "{\n";
psh_glsl += "gl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );\n";
psh_glsl += "}\n";

When a more complicated program needs to be written, one could load an external ARB or GLSL text file and fill the render shader with its content.