View Full Version : Silly ques. about GLSL texture function

11-02-2011, 09:14 AM

Please , could someone tell me what means adding ".xyz" to the vector returned by texture function?
I'm trying to understand some shader and why and when to do this. We assume that myTexture is a normal map passed as a texture to the pixel shader. Each pixel is a normal x,y,z stored as r,g,b.

vec3 normal;

normal = texture(myTexture, fragmentTexCoord).xyz;

Thanks in advance

11-02-2011, 10:12 AM
The syntax your describing refers to something called "swizzling".

Picking up your example:

vec3 normal = texture(myTexture, fragmentTexCoord).xyz;

This function generally returns a vec4. Using the swizzle mask ".xyz" tells the compiler to actually convert and return a vec3 with, basically copy constructed with the XYZ values of the original vec4. Much like this:

vec4 vecFour = vec4(1.0, 2.0, 3.0, 1.0);

// the following vec3 are equal
vec3 swizzeled = vecFour.xyz;
vec3 manual = vec3(vecFour.x, vecFour.y, vecFour.z);

There is also other neat functionality you get with swizzling. Check out http://www.opengl.org/wiki/GLSL_Types#Swizzling. (http://www.opengl.org/wiki/GLSL_Types#Swizzling)