VertShader.vsh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /******************************************************************************
  2. * Vertex Shader
  3. ******************************************************************************/
  4. /*
  5. The vertex and fragment shaders implement two techniques for reflections.
  6. Which version is used for drawing the object is dependent on the value of
  7. bHighDetail. If bHighDetail is true it uses the method described in
  8. OGLES2PerturbedUVs and for false it uses OGLES2Reflections.
  9. Reason for using 2 methods is that when the object is far away you aren't
  10. going to notice all the detail that the PerturbedUVs method adds to
  11. the mesh so you may as well use a simpler method for creating reflections.
  12. This way you aren't using valuable resources on something you aren't
  13. going to notice.
  14. Also, when the training course is in 'low detail' mode it uses a different mesh.
  15. The mesh that is being drawn contains only 7% of the original meshes vertices.
  16. */
  17. attribute highp vec3 inVertex;
  18. attribute mediump vec3 inNormal;
  19. attribute mediump vec2 inTexCoord;
  20. attribute mediump vec3 inTangent;
  21. uniform highp mat4 MVPMatrix;
  22. uniform mediump mat3 ModelWorld;
  23. uniform mediump vec3 EyePosModel;
  24. uniform bool bHighDetail;
  25. varying mediump vec3 EyeDirection;
  26. varying mediump vec2 TexCoord;
  27. void main()
  28. {
  29. // Transform position
  30. gl_Position = MVPMatrix * vec4(inVertex,1.0);
  31. // Calculate direction from eye position in model space
  32. mediump vec3 eyeDirModel = normalize(EyePosModel - inVertex);
  33. if (bHighDetail)
  34. {
  35. // transform light direction from model space to tangent space
  36. mediump vec3 binormal = cross(inNormal, inTangent);
  37. mediump mat3 tangentSpaceXform = mat3(inTangent, binormal, inNormal);
  38. EyeDirection = eyeDirModel * tangentSpaceXform;
  39. TexCoord = inTexCoord;
  40. }
  41. else
  42. {
  43. // reflect eye direction over normal and transform to world space
  44. mediump vec3 reflectDir = ModelWorld * reflect(eyeDirModel, inNormal);
  45. TexCoord = normalize(reflectDir).xy * 0.5 + 0.5;
  46. }
  47. }