useShadowMapInstancingVS.glsl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #version 330
  2. precision highp float;
  3. layout (location = 0) in vec4 position;
  4. layout (location = 1) in vec4 instance_position;
  5. layout (location = 2) in vec4 instance_quaternion;
  6. layout (location = 3) in vec2 uvcoords;
  7. layout (location = 4) in vec3 vertexnormal;
  8. layout (location = 5) in vec4 instance_color;
  9. layout (location = 6) in vec3 instance_scale;
  10. uniform mat4 ModelViewMatrix;
  11. uniform mat4 ProjectionMatrix;
  12. uniform mat4 DepthBiasModelViewProjectionMatrix;
  13. uniform mat4 MVP;
  14. uniform vec3 lightDirIn;
  15. out vec4 ShadowCoord;
  16. out Fragment
  17. {
  18. vec4 color;
  19. } fragment;
  20. out Vert
  21. {
  22. vec2 texcoord;
  23. } vert;
  24. vec4 quatMul ( in vec4 q1, in vec4 q2 )
  25. {
  26. vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz );
  27. vec4 dt = q1 * q2;
  28. float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) );
  29. return vec4 ( im, re );
  30. }
  31. vec4 quatFromAxisAngle(vec4 axis, in float angle)
  32. {
  33. float cah = cos(angle*0.5);
  34. float sah = sin(angle*0.5);
  35. float d = inversesqrt(dot(axis,axis));
  36. vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah);
  37. return q;
  38. }
  39. //
  40. // vector rotation via quaternion
  41. //
  42. vec4 quatRotate3 ( in vec3 p, in vec4 q )
  43. {
  44. vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) );
  45. return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );
  46. }
  47. vec4 quatRotate ( in vec4 p, in vec4 q )
  48. {
  49. vec4 temp = quatMul ( q, p );
  50. return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );
  51. }
  52. out vec3 lightDir,normal,ambient;
  53. void main(void)
  54. {
  55. vec4 q = instance_quaternion;
  56. ambient = vec3(0.5,.5,0.5);
  57. vec4 worldNormal = (quatRotate3( vertexnormal,q));
  58. normal = normalize(worldNormal).xyz;
  59. lightDir = lightDirIn;
  60. vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);
  61. vec4 vertexPos = MVP* vec4((instance_position+localcoord).xyz,1);
  62. gl_Position = vertexPos;
  63. ShadowCoord = DepthBiasModelViewProjectionMatrix * vec4((instance_position+localcoord).xyz,1);
  64. fragment.color = instance_color;
  65. vert.texcoord = uvcoords;
  66. }