|
Using Shaders: 2 Textures

Sampler variables are used to give shader programs access to textures. The following fragment shader uses
a 2D texture called AImage.
uniform sampler2D AImage;
varying float LightIntensity;
void main()
{
vec3 lightColor = vec3(texture2D(AImage, gl_TexCoord[0].st));
gl_FragColor = vec4(lightColor * LightIntensity, 1.0);
}
The texture is sent to the shader program by declaring an SFString variable in the X3D with the same name.
In addition, the texture must be created by adding an ImageTexture to the Appearance node.
appearance Appearance {
texture ImageTexture {
url "image3.png"
}
shaders
ProgramShader {
language "GLSL"
programs [
ShaderProgram {
field SFVec3f LightPosition 0 0 4
url "simpletexturev.txt"
}
ShaderProgram {
field SFString AImage "image3.png"
type "FRAGMENT"
url "simpletexturef.txt"
}
]
}
}
The image on the box is just the plain texture, without using the shader program.

Multiple sampler variables can be added by using a MultiTexture node in the Appearance.
appearance Appearance {
texture MultiTexture {
texture [
ImageTexture {
url "image3.png"
}
ImageTexture {
url "brick.jpg"
}
]
}
shaders
ProgramShader {
language "GLSL"
programs [
ShaderProgram {
field SFVec3f LightPosition 0 0 4
field SFString AImage1 "image3.png"
field SFString AImage2 "brick.jgp"
url "multitexturev.txt"
}
ShaderProgram {
type "FRAGMENT"
url "multitexturef.txt"
}
]
}
}
As mentionned in the previous tutorial, a ComposedShader and ShaderParts could also be used
to produce the same result.
Download tutorial files (same as previous tutorial)
|