Class BL_Shader -- Blender Game Engine 2.49b
|
setSampler
setSampler(name, index)
Sets the sampler. (This defines which texture will be used
and how.)
name:
Type:
string
sampler2D:
Used for textures.
samplerCube:
Used for an cube
map. (Also known as environment maps. )
index:
Type:
integer
Note:
index is the position of the texture in Blender's
Texture tab.
First position is 0. Second position is
1. Etc.
sampler2D
Sample
Code
# define the main part of the program
def main():
# get the current scene
scene = GameLogic.getCurrentScene()
# get a list of the objects in the scene
objList = scene.objects
# Get the object named Cube
obj = objList['OBCube']
# Only one mesh on this Cube
mesh = obj.meshes[0]
# only one material on this mesh
mat = mesh.materials[0]
# Using Blender GLSL Materials or Blender Multitexture
Materials?
if hasattr( mat, 'getMaterialIndex') == True:
#Get shader envelope
shader = mat.getShader()
# shader envelope created?
if shader != None:
# set the shader
shader.setSource(VertexProgram, FragmentProgram, True)
# "color" is a texture
# 0 is the 1st Blender texture channel
shader.setSampler("color", 0)
# Vertex shader
VertexProgram = """
void main()
{
// OpenGL glsl code
gl_TexCoord[0] =
gl_MultiTexCoord0;
gl_Position =
ftransform();
}
"""
# Fragment shader
FragmentProgram = """
// texture passed from main part of program
uniform sampler2D color;
void main()
{
// Get texture from
color map
vec3
texture =
(texture2D(color,
gl_TexCoord[0].st).xyz);
// Use it
gl_FragColor =
vec4(texture, 1.0);
}
"""
# run the program
main()
samplerCube
Note:
Uses a Blender environment map. Not an glsl
cube map.
Note:
Must be loaded as an environmental map. Not
as an image.
Sample
Code
# define the main part of the program
def main():
# get the current scene
scene = GameLogic.getCurrentScene()
# get a list of the objects in the scene
objList = scene.objects
# Get the object named Cube
obj = objList['OBCube']
# Only one mesh on this Cube
mesh = obj.meshes[0]
# only one material on this mesh
mat = mesh.materials[0]
# Using Blender GLSL Materials or Blender Multitexture
Materials?
if hasattr( mat, 'getMaterialIndex') == True:
#Get shader envelope
shader = mat.getShader()
# shader envelope created?
if shader != None:
# set the shader
shader.setSource(VertexProgram, FragmentProgram, True)
# "color" is a blender environmental map
# 0 is the 1st Blender texture channel
shader.setSampler("color", 0)
# Vertex shader
VertexProgram =
"""
void main()
{
// OpenGL glsl
gl_TexCoord[0] =
gl_MultiTexCoord0;
gl_Position =
ftransform();
}
"""
# Fragment shader
FragmentProgram =
"""
// texture passed from main part of program
uniform samplerCube color;
void main()
{
// Get texture from
color map
vec4
texture =
textureCube(color, vec3
(gl_TexCoord[0]));
// Use it
gl_FragColor = texture;
}
"""
# run the program
main()
|
Game developers
can use the game engine to create casual video games,
first and third person shooters, role playing games, racing simulations
and more. The game engine can be used to make both 2D games and 3D
games. Beginning game developers will only need basic knowledge
of
game design and programming to create rich and complex game models with
photo realistic textures to add detail to the graphics of the video
game. Today's games need more than good looking low polygon game
models. Use normal maps and photo realistic textures to create
high
quality game models.