xKamuna

GLSL Example 2

May 6th, 2019
560
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Copyright 2011-2018 Daniel S. Buckstein
  3.     Licensed under the Apache License, Version 2.0 (the "License");
  4.     you may not use this file except in compliance with the License.
  5.     You may obtain a copy of the License at
  6.         http://www.apache.org/licenses/LICENSE-2.0
  7.     Unless required by applicable law or agreed to in writing, software
  8.     distributed under the License is distributed on an "AS IS" BASIS,
  9.     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10.     See the License for the specific language governing permissions and
  11.     limitations under the License.
  12. */
  13. /*
  14.     animal3D SDK: Minimal 3D Animation Framework
  15.     By Daniel S. Buckstein
  16.    
  17.     drawCurveSegment_passColor_gs4x.glsl
  18.     Draw a curve segment between waypoints. Pass solid color.
  19. */
  20. //This file was modified by Summer Softleigh with permission from the author.
  21. #version 410
  22. //  1) layout qualifiers
  23. //  2) declare input from vertex shader
  24. //  3) declare output data (same as fragment shader)
  25. //  4) declare uniform blocks for input data
  26. //  5) declare other uniforms (matrices, path info, etc.)
  27. //  6) implement interpolation algorithms
  28. //  7) implement path segment sampling and drawing algorithms
  29. //  8) test in main
  30. #define MAX_WAYPOINTS 32
  31. #define MAX_VERTICES 64
  32. //1
  33. layout (points) in;
  34. layout (line_strip, max_vertices = MAX_VERTICES) out;
  35. //2
  36. in int vPassInstanceID[];
  37. //3
  38. out vec4 vPassColor;
  39. //4
  40. uniform ubWaypoint
  41. {
  42.     vec4 waypoint[MAX_WAYPOINTS];
  43. };
  44. uniform ubWaypointHandle
  45. {
  46.     vec4 waypointHandle[MAX_WAYPOINTS];
  47. };
  48. //5
  49. uniform mat4 uMVP;
  50. uniform int uPathMode;
  51. uniform int uPathWaypointCount;
  52. uniform int uPathWaypoint0;
  53. //6
  54. // linear interpolation (same as GLSL function 'mix')
  55. vec4 lerp(in vec4 v0, in vec4 v1, const float t)
  56. {
  57.     return (v0 + (v1 - v0) * t);
  58. }
  59. // Catmull-Rom
  60. vec4 sampleCatmullRom(in vec4 vp, in vec4 v0, in vec4 v1, in vec4 v2, const float t)
  61. {
  62.     mat4 influences = mat4(vp, v0, v1, v2);
  63.     mat4 kernel = mat4(
  64.         0,2,0,0, //column-major, is a column, not a row.
  65.         -1,0,1,0,
  66.         2,-5,4,-1,
  67.         -1,3,-3,1
  68.     );
  69.     vec4 param = vec4(1.0f, t, t*t, t*t*t);
  70.     return influences * kernel * param * 0.5f;
  71. }
  72. // Bezier order 0 (constant, recursive base case)
  73. vec4 sampleBezier0(in vec4 v0, const float t)
  74. {
  75.     return v0;
  76. }
  77. // Bezier order 1 (linear)
  78. vec4 sampleBezier1(in vec4 v0, in vec4 v1, const float t)
  79. {
  80.     return lerp(v0, v1, t); //Same as lerp(sampleBezier0(v0), sampleBezier0(v1), t);
  81. }
  82. // Bezier order 2 (quadratic)
  83. vec4 sampleBezier2(in vec4 v0, in vec4 v1, in vec4 v2, const float t)
  84. {
  85.     return lerp(sampleBezier1(v0, v1, t), sampleBezier1(v1, v2, t), t);
  86. }
  87. // Bezier order 3 (cubic)
  88. vec4 sampleBezier3(in vec4 v0, in vec4 v1, in vec4 v2, in vec4 v3, const float t)
  89. {
  90.     return lerp(sampleBezier2(v0, v1, v2, t), sampleBezier2(v1, v2, v3, t), t);
  91. }
  92. // Bezier general
  93. vec4 sampleBezierN(in vec4 v[MAX_WAYPOINTS], const int order, const int base, const float t)
  94. {
  95.      int i;
  96.      int j;
  97.      for(i = order; i > base; --i)
  98.      {
  99.         for(j = base; j < i; ++j)
  100.         {
  101.             v[j] = lerp(v[j+1], v[j], t);
  102.         }
  103.      }
  104.      return v[0];
  105. }
  106. // cubic Hermite
  107. vec4 sampleCubicHermite(in vec4 v0, in vec4 v1, in vec4 m0, in vec4 m1, const float t)
  108. {
  109.     float basis00 = 1 - 3*(t*t) + 2*(t*t*t);
  110.     float basis10 = t - 2*(t*t) + (t*t*t);
  111.     float basis01 = 3*(t*t) - 2*(t*t*t);
  112.     float basis11 = (t*t*t) - (t*t);
  113.     return basis00*v0 + basis10*m0 + basis01*v1 + basis11*m1;
  114. }
  115. // (7)
  116. void drawLineSamples(in vec4 v0, in vec4 v1, const int n, const float dt)
  117. {
  118.     vec4 p;
  119.     float t;
  120.     int i;
  121.     for(i = 0, t = 0.0; i < n; ++i, t += dt)
  122.     {
  123.         p = lerp(v0, v1, t);
  124.         gl_Position = uMVP * p;
  125.         EmitVertex();
  126.     }
  127.     EndPrimitive();
  128. }
  129. void drawCatmullRomSamples(in vec4 vp, in vec4 v0, in vec4 v1, in vec4 v2, const int n, const float dt)
  130. {
  131.     vec4 p;
  132.     float t;
  133.     int i;
  134.     for(i = 0, t = 0.0; i < n; ++i, t += dt)
  135.     {
  136.         p = sampleCatmullRom(vp, v0, v1, v2, t);
  137.         gl_Position = uMVP * p;
  138.         EmitVertex();
  139.     }
  140.     EndPrimitive();
  141. }
  142. void drawCubicBezierSamples(in vec4 v0, in vec4 v1, in vec4 v2, in vec4 v3, const int n, const float dt)
  143. {
  144.     vec4 p;
  145.     float t;
  146.     int i;
  147.     for(i = 0, t = 0.0; i < n; ++i, t += dt)
  148.     {
  149.         //General test
  150.         /*vec4 v[MAX_WAYPOINTS] = vec4[MAX_WAYPOINTS](v0,v1,v2,v3,
  151.         vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0),
  152.         vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0), vec4(0.0)); //28 times.
  153.         p = sampleBezierN(v, 3, 0, t);*/
  154.         //Specialized function
  155.         p = sampleBezier3(v0, v1, v2, v3, t);
  156.         gl_Position = uMVP * p;
  157.         EmitVertex();
  158.     }
  159.     EndPrimitive();
  160. }
  161. void drawCubicHermiteSamples(in vec4 v0, in vec4 v1, in vec4 m0, in vec4 m1, const int n, const float dt)
  162. {
  163.     vec4 p;
  164.     float t;
  165.     int i;
  166.     for(i = 0, t = 0.0; i < n; ++i, t += dt)
  167.     {
  168.         p = sampleCubicHermite(v0, v1, m0, m1, t);
  169.         gl_Position = uMVP * p;
  170.         EmitVertex();
  171.     }
  172.     EndPrimitive();
  173. }
  174. void main()
  175. {
  176.     int w0 = vPassInstanceID[0];
  177.     int w1 = w0 + 1 < uPathWaypointCount ? w0 + 1 : w0;
  178.     int wp = w0 > 0 ? w0 - 1 : w0;
  179.     int w2 = w1 + 1 < uPathWaypointCount ? w1 + 1 : w1;
  180.     int n = MAX_VERTICES;
  181.     float dt = 1.0f / float(n);
  182.     if(uPathWaypoint0 == w0) vPassColor = vec4(0.0f, 0.5f, 1.0f, 1.0f); //TODO: Step function here instead
  183.     else vPassColor = vec4(0.0f, 0.25f, 0.5f, 1.0f);
  184.     // (8)
  185.     switch (uPathMode)
  186.     {
  187.         case 0:
  188.             drawLineSamples(waypoint[w0], waypoint[w1], n, dt);
  189.             break;
  190.         case 1:
  191.             drawCatmullRomSamples(waypoint[wp], waypoint[w0], waypoint[w1], waypoint[w2], n, dt);
  192.             break;
  193.         case 2:
  194.             drawCubicBezierSamples(waypoint[w0], waypointHandle[w0], waypointHandle[w1], waypoint[w1], n, dt);
  195.             break;
  196.         case 3:
  197.             drawCubicHermiteSamples(waypoint[w0], waypoint[w1], waypointHandle[w0], waypointHandle[w1], n, dt);
  198.             break;
  199.     };
  200. }
Add Comment
Please, Sign In to add comment