instancingVS.glsl 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 vec3 lightDirIn;
  13. out Fragment
  14. {
  15. vec4 color;
  16. } fragment;
  17. out Vert
  18. {
  19. vec2 texcoord;
  20. } vert;
  21. vec4 quatMul ( in vec4 q1, in vec4 q2 )
  22. {
  23. vec3 im = q1.w * q2.xyz + q1.xyz * q2.w + cross ( q1.xyz, q2.xyz );
  24. vec4 dt = q1 * q2;
  25. float re = dot ( dt, vec4 ( -1.0, -1.0, -1.0, 1.0 ) );
  26. return vec4 ( im, re );
  27. }
  28. vec4 quatFromAxisAngle(vec4 axis, in float angle)
  29. {
  30. float cah = cos(angle*0.5);
  31. float sah = sin(angle*0.5);
  32. float d = inversesqrt(dot(axis,axis));
  33. vec4 q = vec4(axis.x*sah*d,axis.y*sah*d,axis.z*sah*d,cah);
  34. return q;
  35. }
  36. //
  37. // vector rotation via quaternion
  38. //
  39. vec4 quatRotate3 ( in vec3 p, in vec4 q )
  40. {
  41. vec4 temp = quatMul ( q, vec4 ( p, 0.0 ) );
  42. return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );
  43. }
  44. vec4 quatRotate ( in vec4 p, in vec4 q )
  45. {
  46. vec4 temp = quatMul ( q, p );
  47. return quatMul ( temp, vec4 ( -q.x, -q.y, -q.z, q.w ) );
  48. }
  49. out vec3 lightDir,normal,ambient;
  50. void main(void)
  51. {
  52. vec4 q = instance_quaternion;
  53. ambient = vec3(0.5,.5,0.5);
  54. vec4 worldNormal = (quatRotate3( vertexnormal,q));
  55. normal = normalize(worldNormal).xyz;
  56. lightDir = lightDirIn;
  57. vec4 localcoord = quatRotate3( position.xyz*instance_scale,q);
  58. vec4 vertexPos = ProjectionMatrix * ModelViewMatrix *(instance_position+localcoord);
  59. gl_Position = vertexPos;
  60. fragment.color = instance_color;
  61. vert.texcoord = uvcoords;
  62. }