Deforming objects with a vertex shader

A vertex shader processes each vertex of drawing objects and can change their built-in attributes such as position, color, normal, and can also change any custom attributes. Here, we consider the example of the vertex shader that just moves vertices according to a rule with the help of parameters that are controlled by the mouse position.

Note

This is example 08-Shaders/06-VertexDeformation.

This example is based on the example given in the The triangles cloud example section of Chapter 2, Drawing in 3D. The original example draws a rotated sphere-shaped cloud of random triangles.

Vertex shader

In the bin/data folder, create a new text file shaderVert.c containing the following code:

#version 120
#extension GL_ARB_texture_rectangle : enable
#extension GL_EXT_gpu_shader4 : enable

uniform float phase = 0.0;  //Phase for "sin" function
uniform float distortAmount = 0.25; //Amount of distortion

void main() {
  //Get original position of the vertex
  vec3 v = gl_Vertex.xyz;

  //Compute value of distortion for current vertex
  float distort = distortAmount * sin( phase + 0.015 * v.y );

  //Move the position
  v.x /= 1.0 + distort;
  v.y /= 1.0 + distort;
  v.z /= 1.0 + distort;

  //Set output vertex position
  vec4 posHomog = vec4( v, 1.0 );
  gl_Position = gl_ModelViewProjectionMatrix * posHomog;

  //Set output texture coordinate and color in a standard way
  gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_FrontColor = gl_Color;
}

This shader has two parameters, phase and distortAmount, which affect the phase of a sine wave and the amount of distortion respectively. The main() function transforms gl_Vertex using the phase and distortAmount parameters, and finally writes the result to gl_Position (see details on these variables in the Structure of a shader's code section).

The most notable thing here is the relation between variables gl_Vertex and gl_Position having type vec4, and variable v having type vec3. The variables of type vec4 are 3D vectors in homogeneous coordinates, where the last coordinate just sets the scaling (which is most often equal to 1.0). In the example, we wish to perform computations in ordinary 3D space, so we can truncate the last coordinate of gl_Vertex and obtain v. We then perform all computations with v, and finally just append to v the fourth coordinate (equal to 1.0) to obtain vec4 posHomog, which is used for the final computation of gl_Position.

The last two lines of the main() function are as follows:

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_FrontColor = gl_Color;

The lines the set texture coordinate (not needed in the example, but can be effective for future use of the shader) and front color to the a standard values. See the A simple fragment shader example section for details on these.

Fragment shader

In the bin/data folder, create a new text file shaderFrag.c, which contains the following code:

#version 120
#extension GL_ARB_texture_rectangle : enable
#extension GL_EXT_gpu_shader4 : enable

void main(){
  gl_FragColor = gl_Color;
}

This is a dummy fragment shader, which just writes the output color equal to the gl_Color value (which is equal to the interpolated gl_FrontColor value from the vertex shader).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset