[GEM-dev] shader library

Wesley Smith wesley.hoke at gmail.com
Tue Aug 14 23:48:25 CEST 2007


My first attempt to send this was rejected.  Hopefully it will only appear once.

If you want a good place to look for ideas, download Jitter from
cycling 74 and look at the shader folder.  There are a ton of shaders
in there that can be borrowed from.  Some have copyright notices, so
you just need to leave those in the files to distribute them.

I've been working on an audiovisual composition system using Lua with
some other people here in Santa Barbara and we have a shader file
format that is actually Lua code so you can drop functions in it and
they will expand to generate shader code for you.  The format is
basically a translation for Jitter's .jxs XML shader format into Lua
tables.  It works quite well and allows the shader C++ class to
construct an interface to the shader dynamically and set default
values for uniform parameters.  Here's a phong shader written in this
format:


Shader
{
       name = "mat.phong.shl",
       description = [[ Phong Shader (per-pixel lighting) ]],

       La = Parameter {
               type = "vec4", default = {0., 0., 0., 1.0},
               description = "Ambient Light"
       },

       Li = Parameter {
               type = "vec4", default = {1., 1., 1., 1.0},
               description = "Incident Light"
       },

       eyePosition = Parameter {
               type = "vec3", default = {0., 0., 4.},
               description = "Eye Position in world coordinates"
       },

       Ke = Parameter {
               type = "vec4", default = {0., 0., 0., 0.},
               description = "Emissive Color"
       },

       Ka = Parameter {
               type = "vec4", default = {0., 0., 0., 1.},
               description = "Ambient Color"
       },

       Kd = Parameter {
               type = "vec4", default = {0.2, 0.2, 0.2, 1.},
               description = "Diffuse Color"
       },

       Ks = Parameter {
               type = "vec4", default = {1., 1., 1., 1.},
               description = "Specular Color"
       },

       shininess = Parameter {
               type = "float", default = 10.,
               description = "Material Shininess"
       },

       vertexParameters =
       {
               --none
       },

       fragmentParameters =
       {
               "La",
               "Li",
               "eyePosition",
               "Ke",
               "Kd",
               "Ks",
               "shininess",
       },

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--      Vertex Program
--------------------------------------------------------------------------
vertexProgram = [[

varying vec3 N;
varying vec3 P;

void main (void)

{
       //get the vertices into eye space
       P = (gl_ModelViewMatrix*gl_Vertex).xyz;

       //get the normals into eye space
       N = normalize(gl_Normal);
       N = mat3(       gl_ModelViewMatrixInverseTranspose[0].xyz,
                               gl_ModelViewMatrixInverseTranspose[1].xyz,
                               gl_ModelViewMatrixInverseTranspose[2].xyz)*N;

       gl_Position = ftransform();
}

]],
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--      Fragment Program
--------------------------------------------------------------------------
fragmentProgram = [[

uniform vec4 La;
uniform vec4 Li;
uniform vec3 eyePosition;
uniform vec4 Ke;
uniform vec4 Ka;
uniform vec4 Kd;
uniform vec4 Ks;
uniform float shininess;

varying vec3 N;
varying vec3 P;

void main (void)
{
       vec3 Nn = normalize(N);

       //ambient contribution
       vec4 ambient = La*Ka;

       vec3 lightPosition = vec3(gl_LightSource[0].position);

       //diffuse contribution
       vec3 L = normalize(lightPosition - P);
       vec4 diffuse = Kd*Li*max(dot(Nn, L), 0.);

       //calculate specular contribution
       vec3 V = normalize(eyePosition - P);
       vec3 H = normalize(L + V); //average of lighting and view vector)
not true reflection vector
       vec4 specular = Ks*Li * pow(max(dot(Nn, H), 0.), shininess);

       gl_FragColor = Ke + ambient + diffuse + specular;
}

]],
--------------------------------------------------------------------------
}




More information about the GEM-dev mailing list