Question

I recently took over a project that was left stagnate a team member quit a few months ago. While trying to get myself up to speed I came across this vertex shader and I'm having a hard time understanding what its doing:

uniform int axes;
varying vec4 passcolor;

void main()
{
  // transform the vertex
  vec4 point;
  if (axes == 0) {
     point = gl_Vertex;
  } else if (axes == 1) {
     point = gl_Vertex.xzyw;

  } else if (axes == 2) {
     point = gl_Vertex.xwzy;

  } else if (axes == 3) {
      point = gl_Vertex.yzxw;

  } else if (axes == 4) {
     point = gl_Vertex.ywxz;

  } else if (axes == 5) {
     point = gl_Vertex.zwxy;
  }

  point.z = 0.0;
  point.w = 1.0;

  // eliminate w point
  gl_Position = gl_ModelViewProjectionMatrix * point;

 passcolor = gl_Color;
}

The lines I'd like to better understand are the lines like this one:

point = gl_Vertex.xwzy;

I can't seem to find documentation that explains this.

Can someone give a quick explanation of what this shader is doing?

Was it helpful?

Solution

I can't seem to find documentation that explains this.

The GLSL specification is pretty clear about how swizzle selection works.

What the shader is basically doing is an obtuse way of picking two axes from a vec4. Notice that the Z and W of the point are overwritten after the selection. By all rights, it could be rewritten as:

vec2 point;
if (axes == 0) {
   point = gl_Vertex.xy;
} else if (axes == 1) {
   point = gl_Vertex.xz;
} else if (axes == 2) {
   point = gl_Vertex.xw;
} else if (axes == 3) {
   point = gl_Vertex.yz;
} else if (axes == 4) {
   point = gl_Vertex.yw;
} else if (axes == 5) {
   point = gl_Vertex.zw;
}

gl_Position = gl_ModelViewProjectionMatrix * vec4(point.xy, 0.0, 1.0);

So it is just selecting two coordinates from the input vertex. Why it needs to do this, I can't say.

OTHER TIPS

The order of the x, y, z, and w after the . determines a mapping from the x, y, z and w of gl_Vertex to the x, y, z, and w of point.

This is called swizzling. point = gl_Vertex is equivalent to point = gl_Vertex.xyzw. point = gl_Vertex.yxzw results in a point equivalent to gl_Vertex with x and y values swapped.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top