-=[Shader reference]=-


--=[ Index | Materials | GLSL ]=--
-=[ Material | Shaders | Example | Changes | How-to | Scripts ]=-

Quick Jump


Functions
-setSource(vertex, fragment, apply)
-delSource()
-getVertexProg()
-getFragmentProg()
-setNumberOfPasses(pass_number)
-isValid()
-validate()

Uniforms
-setUniform4f(name, x,y,z,w)
-setUniform3f(name, x,y,z)
-shader.setUniform2f(name, s,t)
-setUniform1f(name, f)
-setUniform4i(name, x,y,z,w)
-setUniform3i(name, x,y,z)
-setUniform2i(name, s,t)
-setUniform1i(name, i)
-setUniformfv(name, list[2,3,4])
-setUniformiv(name, list[2,3,4])
-setUniformMatrix4(name, list[[4]], transpose=true)
-setUniformMatrix3(name, list[[3]], transpose=true)
-setUniformDef(name, enum)




Accessing a shader.

mesh_index = 0
mesh = obj.getMesh(mesh_index)
material_num_on_mesh =0

while mesh != None:
        for mat in mesh.materials:
                if not hasattr(mat, "getMaterialIndex"):
                        return
                shader = mat.getShader()
                if shader != None:
                        # do stuff
                
        mesh_index += 1
        mesh = obj.getMesh(mesh_index)

Functions


setSource(vertex, fragment, apply)

Sets the shader source for the current material.

vertex = """
void main()
{
        gl_Position = ftransform();
}
"""

fragment = """
void main()
{
        gl_FragColor = vec4(1.0, 0.5, 0.0, 1.0);
}
"""

# ... material loop
apply = 1
shader.setSource(vertex, fragment, apply)



delSource()

Deletes the installed program attached to the current material.
If deleted, the material will default back to original material on the object.
The shader will need to be re linked with shader.setSource() if it's going to be used again.



getVertexProg()

Returns the string holding the vertex program.



getFragmentProg()

Returns the string holding the Fragment program.



setNumberOfPasses(pass_number)

TODO more than one render pass.

Will always render one pass at the moment.



isValid()

Returns true if the shader has been successfully linked with shader.setSource(), false otherwise.



validate()

Prints info on the shader.

TODO return an indication on the state of the shader.



Uniforms

Setting values in your shader is accomplished by uniform variables.
Each uniform type in the shader has a matching python function.

Example int uniform:

uniform int a;
shader.setUniform1i('a', 1)

uniform vec2 b;
shader.setUniform2i('b', 1,2)

...

Example float uniform.

uniform float a;
shader.setUniform1f('a', 1.0)

uniform vec2 b;
shader.setUniform2f('b', 1.0,2.5)

...

The functions setUniformiv, setUniformfv accept lists of size 2,3 or 4, matching an int or float list.

uniform vec3 objectPosition;
shader.setUniformfv('objectPosition', gameobject.getPosition()) # [x,y,z]

uniform vec4 color;
rgba = [1.0, 0.5, 0.0, 1.0]
shader.setUniformfv('color', rgba)



setUniform4f(name, x,y,z,w)

Sets a float vec4 uniform variable.



setUniform3f(name, x,y,z)

Sets a float vec3 uniform variable.



shader.setUniform2f(name, s,t)

Sets a float vec2 uniform variable.



setUniform1f(name, f)

Sets a float uniform variable.



setUniform4i(name, x,y,z,w)

Sets a int vec4 uniform variable.



setUniform3i(name, x,y,z)

Sets a int vec3 uniform variable.



setUniform2i(name, s,t)

Sets a int vec2 uniform variable.



setUniform1i(name, i)

Sets a int uniform variable.



setUniformfv(name, list[2,3,4])

Sets a float vec(2,3,4) uniform variable.



setUniformiv(name, list[2,3,4])

Sets a int vec(2,3,4) uniform variable.



setUniformMatrix4(name, list[[4]], transpose=true)

Sets a unform mat4.

The game engine handles a matrix in row major format, and OpenGL handles a matrix in column major.
Therefore the matrix is transposed by default.

mat = [\
        [1,0,0,0],\
        [1,1,0,0],\
        [0,0,1,0],\
        [0,0,0,1]\
        ]



setUniformMatrix3(name, list[[3]], transpose=true)

Sets a unform mat3.

The game engine handles a matrix in row major format, and OpenGL handles a matrix in column major.
Therefore the matrix is transposed by default.

mat = [\
        [1,0,0],\
        [1,1,0],\
        [0,0,1]\
        ]



setUniformDef(name, enum)

This is a special unform function. There are a few pre defined internal functions that can be handled here.

Predefined functions

Example: Set a constant timer on the shader
shader.setUniformDef("timer", GameLogic.CONSTANT_TIMER)

See Changes setUniformDef(name, enum)



setAttrib(enum)

Removed: See Changes setAttrib(enum)



setSampler(name, index)

Sets a sampler type

Samplers are accessed by index. A mesh with no material assigned to it, but has a face texture instead, the index for the sampler will be 0.
Otherwise the sampler index will be the order of the texture in the material panel.

Valid sampler types

Invalid sampler types



--[- snailrose -]--