GLSL Programming/Blender/Minimal Shader

< GLSL Programming < Blender
Screen shot of the Blender 2.5 user interface. The large window is a 3D View with an opened Tool Shelf. Above it is a small Info window. In the bottom, right corner is a Properties window. The type of each window can be changed with the icon in its bottom left or top left corner.

This tutorial covers the basic steps to create a minimal GLSL shader in Blender 2.6.

Creating a GLSL Shader

The steps to specify and run a GLSL vertex and fragment shader are, for example:

And here is the Python script:

import bge

cont = bge.logic.getCurrentController()

VertexShader = """
   void main() // all vertex shaders define a main() function
   {
      gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
         // this line transforms the predefined attribute gl_Vertex 
         // of type vec4 with the predefined uniform 
         // gl_ModelViewProjectionMatrix of type mat4 and stores 
         // the result in the predefined output variable gl_Position 
         // of type vec4. (gl_ModelViewProjectionMatrix combines 
         // the viewing transformation, modeling transformation and 
         // projection transformation in one matrix.)
   }
"""

FragmentShader = """
   void main()
   {   
      gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
         // this fragment shader just sets the output color to opaque
         // red (red = 1.0, green = 0.0, blue = 0.0, alpha = 1.0)
   }
"""

mesh = cont.owner.meshes[0]
for mat in mesh.materials:
    shader = mat.getShader()
    if shader != None:
        if not shader.isValid():
            shader.setSource(VertexShader, FragmentShader, 1)

(If you are aware of easier ways to set the shaders for individual objects, please feel free to edit this page.)

Editing Shaders

This would be a good time to play with the shader; in particular, you can easily change the computed fragment color. Try neon green by opening the Python script in the Text Editor and replacing the fragment shader with this code:

   // this fragment shader should be assigned 
   // to "FragmentShader" in the script above

   void main()
   {   
      gl_FragColor = vec4(0.6, 1.0, 0.0, 1.0); 
         // red, green, blue, alpha
   }

You have to start the game engine again to apply the new shader; see above. The color should now be green. You could also try to modify the red, green, and blue components to find the warmest orange or the darkest blue. (Actually, there is a movie about finding the warmest orange and another about dark blue that is almost black.)

You could also play with the vertex shader, e.g. try this vertex shader:

   // this vertex shader should be assigned 
   // to "VertexShader" in the script above

   void main() 
   {
      gl_Position = gl_ModelViewProjectionMatrix * 
         (vec4(1.0, 0.1, 1.0, 1.0) * gl_Vertex);
   }

This flattens any input geometry by multiplying the coordinate with . (This is a component-wise vector product; for more information on vectors and matrices in GLSL see the discussion in “Vector and Matrix Operations”.)

In case the shader does not compile, Blender displays an error message in a console as described above:

Blender Game Engine Started
---- Vertex Shader Error ----
0:4(83): error: syntax error, unexpected ')', expecting ',' or ';'

---- Vertex shader failed to compile ----
Blender Game Engine Finished


Saving Your Work in a Scene

There is one more thing: you should save you work in a Blender file (the extension is .blend). Choose File > Save As ... from the menu of an Info window. (Remember that you can change the type of any Blender window with the icon in the top left or bottom left corner of the window).

One More Note about Terminology

It might be good to clarify the terminology. In GLSL, a “shader” is either a vertex shader or a fragment shader. The combination of both is called a “program”.

Unfortunately, Blender refers to this kind of program as a “shader” while a vertex shader is called a “vertex program” and a fragment shader is called a “fragment program”.

To make the confusion perfect, I'm going to use the word “shader” for a GLSL program, i.e. the combination of a vertex and a fragment shader. However, I will use the GLSL terms “vertex shader” and “fragment shader” instead of “vertex program” and “fragment program”.

Summary

Congratulations, you have reached the end of this tutorial. A few of the things you have seen are:

Actually, this was quite a lot of stuff.

Further Reading

If you still want to know more


< GLSL Programming/Blender

Unless stated otherwise, all example source code on this page is granted to the public domain.
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.