Facebook
From MT, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 258
  1. // Blending shader
  2.  
  3. // PI constant
  4. #define M_PI 3.1415926535897932384626433832795
  5.  
  6. // Used by lighting fragment shader
  7. varying vec3 N;
  8. varying vec3 v;
  9.  
  10. // Bending angle (-30 to 30 degrees)
  11. uniform float bendAngle;
  12.  
  13. // Creates matrix representing rotation around X axis
  14. mat4 makeXRotMat(float myAngle)
  15. {
  16.         // STUDENTS CODE //
  17.         //                   X    Y    Z    W
  18.         mat4 retMat = mat4( 1.0, 0.0, 0.0, 0.0,
  19.                                                 0.0, cos(myAngle), -sin(myAngle), 0.0,
  20.                                                 0.0, sin(myAngle), cos(myAngle), 0.0,
  21.                                                 0.0, 0.0, 0.0, 1.0 );
  22.  
  23.         // TODO: create matrix representing rotation around X axis
  24.         // Hint: use circle equation in parametric form
  25.         // X column should remain constant - YZ unit vectors represent desired change of base vectors
  26.  
  27.         // STUDENTS CODE END //
  28.         return retMat;
  29. }
  30.  
  31. // Vertex shader entry point
  32. void main()
  33. {
  34.         // Copy vertex
  35.         vec4 tmpV = gl_Vertex;
  36.        
  37.         // STUDENTS CODE //
  38.         // TODO: Create blended cone animation using vertices with Z > 1.0
  39.  
  40.         // Step one: create rotation matrices: one should represent rotation and the other should be identity matrix
  41.         // We will blend vertex rotation between them
  42.         mat4 matOne = makeXRotMat(bendAngle);
  43.        
  44.         // Step two: calculate blending coefficient - we start bending if given vertex tmpV has Z coordinate greater than 1.0
  45.         // Cone has height of 4.0 units (Z axis), so coefficient should be calculated for Z in range from 1.0 to 4.0
  46.         // and result should be between 0.0 and 1.0
  47.         // Hint: use function clamp(val_to_clamp, min, max);
  48.         // float coeff = ...
  49.         float coeff = clamp((tmpV.z - 1)/3, 0, 1);
  50.  
  51.         // Step three: alter only vertices with Z coordinate greater than 1.0
  52.         // Hint: You can access tmpV like a structure - tmpV.x, tmpV.y, tmpV.z
  53.         // Hint: tmpV must be translated to the origin in Z axis before transformation, since center of rotation will be in the origin
  54.         // Hint: matrices can be blended using regular multiplication operations - blendedMat = coeffN1 * matN1 + coeffN2 * matN2 + ...
  55.         // Hint: sum of coeffN1 + coeffN2 + ... must be equal to 1.0
  56.         // Hint: after altering tmpV with blendMat (tmpV = tmpV * blendMat; or tmpV *= blendMat), remember to translate tmpV back in Z axis!
  57.        
  58.         if(tmpV.z > 1.0) {
  59.                 tmpV.z -= 1.0;         
  60.                 tmpV *= (coeff*matOne + (1f-coeff)*mat4(1f));
  61.                 tmpV.z += 1.0;
  62.         }
  63.  
  64.         // STUDENTS CODE END //
  65.  
  66.         // tmpV now contains final position in model space..
  67.  
  68.         // Transforming The Vertex
  69.         v = vec3(gl_ModelViewMatrix * tmpV);      
  70.         N = normalize(gl_NormalMatrix * gl_Normal);
  71.         gl_Position = gl_ModelViewProjectionMatrix * tmpV;
  72. }