Easy use of GLSL shaders in game engine

One dream in Architecture design on computer is to walk through the future project in realtime and photorealistic rendering. Blender is on the way slowy but surely. Due to the Game Engine, you could ever make a walkthrough. With the integration of GLSL language you can now have the same visual effects as commercial games.

The aim of this tutorial is to explain how to import GLSL materials into your walkthrough game scene. OpenGL Shader Langage is a bit complex for us so we will work with pre-made GLSL materials.

How GLSL materials work?
To put it in a nutshell, that's what each GLSL material needs to work:


 * A blender material which contains textures, colors, etc...
 * A script which contains the GLSL code that will emphasize the material with tremendous effects
 * A empty which will load the script on Game Blender start

And don't forget to have "Use blender materials" activated in the Game top menu.

Example
We start with a simple walkthrough.



[[Media:GLSL-1.blend|Download the .blend source]]

There is 2 solid objects: one wall on the left and the whole piece. Our purpose is to get bump stones on the left wall.

Thus, get what we need:

The material
Download the Stone Wall (GLSL) material blend file from this website and import the "Stone Wall-GLSL" material to you scene. Use the File->Append menu to do so.



Select the left wall object and apply this material on it.

The textures in this material need UV to be mapped (see the "Map to" tab in the Shading panel (F5)), so select the left wall object, press F to go to UV Face select mode. This create an UVmap for the object. Press F one time more to exit and go back to Edit mode. (Fine UVmapping is a large topic not discussed here. However, this is good enough for this tutorial)

The script
From the same Stone Wall GLSL material blend file, import the Stone-GLSL.py script (in the Text category while appending) Open it in the text editor and look for the line "ShaderObjects = [ objlist['OBCube'] ]". This is the list of object the GLSL effects are applying for. So you need to fill it right. In our example, replace this line by "ShaderObjects = [ objlist['OBLeftWall'] ]"

The empty
Add an empty somewhere in your scene. Go to the Logic panel (F4) and add a sensor and a controller. Choose a Python type for the controller and fill the text box with the GLSL script name. Link the sensor to the controller.



Make sure you activated "Use blender materials" in the Game top menu.



Start the game and open your eyes !



[[Media:GLSL-2.blend|Download the .blend source]]

Links
To go further :
 * http://download.blender.org/documentation/242GE_Docs/index.html#index