-=[Adding textures]=-


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

See Also

How do I add textures to a shader?


First add a material to your mesh and load a texture in the first unit.

Open the template and add the following code to the vertex shader.

void main() 
{
        gl_TexCoord[0] = gl_MultiTexCoord0;
        gl_Position = ftransform();
}

We are using the builtin varying variable gl_TexCoord[#] for convenience. You could also write the shader to look like this.

varying vec4 texcoord;
void main() 
{
        texcoord = gl_MultiTexCoord0;
        gl_Position = ftransform();
}

Add the flowing code to the fragment shader.

uniform sampler2D texture;

void main() {
        gl_FragColor = texture2D(texture, gl_TexCoord[0].st);
}

texture2D accepts a sampler2D and a vec2 argument. We use the swizzle operator to make vec4 gl_TexCoord[0] a vec2 gl_TexCoord[0].st.
you could get the same result by writing

texture2D(texture, vec2(gl_TexCoord[0]));

A uniform was added so it needs to be set in the material loop.

Add:

shader = mat.getShader()
if shader != None:
        if not shader.isValid():
                shader.setSource(VertexShader, FragmentShader,1)
        
        first_texture_index = 0
        shader.setSampler('texture', first_texture_index)

Link an always sensor (pulse mode off) calling the final script.
If all is well you now have a texture shader. press (p) and see the shader in action.

The final script:

##-----------------------------------------------------------------------------
## Shader Texture (howto)
##-----------------------------------------------------------------------------
import GameLogic
ObjectList = GameLogic.getCurrentScene().getObjectList()

# -------------------------------------
ShaderObjects = [ObjectList['OBCube']]
MaterialIndexList = [0]
# -------------------------------------


# -------------------------------------
VertexShader = """
void main(){
        gl_TexCoord[0] = gl_MultiTexCoord0;
        gl_Position = ftransform();
}
"""

FragmentShader = """
uniform sampler2D texture;

void main() {
        gl_FragColor = texture2D(texture, gl_TexCoord[0].st);
}
"""

def MainLoop ():
        # for each object supplied
        for obj in ShaderObjects:

                mesh_index = 0
                mesh = obj.getMesh(mesh_index)

                while mesh != None:
                        
                        # for each material in the mesh
                        for mat in mesh.materials:
                                
                                # sanity check.. 
                                # is the use blender materials option checked?
                                if not hasattr(mat, "getMaterialIndex"):
                                        return
                                
                                # 0->15 avaiable
                                mat_index = mat.getMaterialIndex()

                                # find an index from the list                           
                                found = 0
                                for i in range(len(MaterialIndexList)):
                                        if mat_index == MaterialIndexList[i]:
                                                found=1
                                                break
                                if not found: continue

                                shader = mat.getShader()
                                if shader != None:
                                    if not shader.isValid():
                                        shader.setSource(VertexShader, FragmentShader,1)

                                    first_texture_index = 0
                                    shader.setSampler('texture', first_texture_index)

                        mesh_index += 1
                        mesh = obj.getMesh(mesh_index)


## call it
MainLoop()



--[- snailrose -]--