Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright 2011-2018 Daniel S. Buckstein
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- /*
- animal3D SDK: Minimal 3D Animation Framework
- By Daniel S. Buckstein
- drawCurveSegment_passColor_gs4x.glsl
- Draw a curve segment between waypoints. Pass solid color.
- */
- //This file was modified by Summer Softleigh with permission from the author.
- #version 410
- // 1) layout qualifiers
- // 2) declare input from vertex shader
- // 3) declare output data (same as fragment shader)
- // 4) declare uniform blocks for input data
- // 5) declare other uniforms (matrices, path info, etc.)
- // 6) implement interpolation algorithms
- // 7) implement path segment sampling and drawing algorithms
- // 8) test in main
- #define MAX_WAYPOINTS 32
- #define MAX_VERTICES 64
- //1
- layout (points) in;
- layout (line_strip, max_vertices = MAX_VERTICES) out;
- //2
- in int vPassInstanceID[];
- //3
- out vec4 vPassColor;
- //4
- uniform ubWaypoint
- {
- vec4 waypoint[MAX_WAYPOINTS];
- };
- uniform ubWaypointHandle
- {
- vec4 waypointHandle[MAX_WAYPOINTS];
- };
- //5
- uniform mat4 uMVP;
- uniform int uPathMode;
- uniform int uPathWaypointCount;
- uniform int uPathWaypoint0;
- //6
- // linear interpolation (same as GLSL function 'mix')
- vec4 lerp(in vec4 v0, in vec4 v1, const float t)
- {
- return (v0 + (v1 - v0) * t);
- }
- // Catmull-Rom
- vec4 sampleCatmullRom(in vec4 vp, in vec4 v0, in vec4 v1, in vec4 v2, const float t)
- {
- mat4 influences = mat4(vp, v0, v1, v2);
- mat4 kernel = mat4(
- 0,2,0,0, //column-major, is a column, not a row.
- -1,0,1,0,
- 2,-5,4,-1,
- -1,3,-3,1
- );
- vec4 param = vec4(1.0f, t, t*t, t*t*t);
- return influences * kernel * param * 0.5f;
- }
- // Bezier order 0 (constant, recursive base case)
- vec4 sampleBezier0(in vec4 v0, const float t)
- {
- return v0;
- }
- // Bezier order 1 (linear)
- vec4 sampleBezier1(in vec4 v0, in vec4 v1, const float t)
- {
- return lerp(v0, v1, t); //Same as lerp(sampleBezier0(v0), sampleBezier0(v1), t);
- }
- // Bezier order 2 (quadratic)
- vec4 sampleBezier2(in vec4 v0, in vec4 v1, in vec4 v2, const float t)
- {
- return lerp(sampleBezier1(v0, v1, t), sampleBezier1(v1, v2, t), t);
- }
- // Bezier order 3 (cubic)
- vec4 sampleBezier3(in vec4 v0, in vec4 v1, in vec4 v2, in vec4 v3, const float t)
- {
- return lerp(sampleBezier2(v0, v1, v2, t), sampleBezier2(v1, v2, v3, t), t);
- }
- // Bezier general
- vec4 sampleBezierN(in vec4 v[MAX_WAYPOINTS], const int order, const int base, const float t)
- {
- int i;
- int j;
- for(i = order; i > base; --i)
- {
- for(j = base; j < i; ++j)
- {
- v[j] = lerp(v[j+1], v[j], t);
- }
- }
- return v[0];
- }
- // cubic Hermite
- vec4 sampleCubicHermite(in vec4 v0, in vec4 v1, in vec4 m0, in vec4 m1, const float t)
- {
- float basis00 = 1 - 3*(t*t) + 2*(t*t*t);
- float basis10 = t - 2*(t*t) + (t*t*t);
- float basis01 = 3*(t*t) - 2*(t*t*t);
- float basis11 = (t*t*t) - (t*t);
- return basis00*v0 + basis10*m0 + basis01*v1 + basis11*m1;
- }
- // (7)
- void drawLineSamples(in vec4 v0, in vec4 v1, const int n, const float dt)
- {
- vec4 p;
- float t;
- int i;
- for(i = 0, t = 0.0; i < n; ++i, t += dt)
- {
- p = lerp(v0, v1, t);
- gl_Position = uMVP * p;
- EmitVertex();
- }
- EndPrimitive();
- }
- void drawCatmullRomSamples(in vec4 vp, in vec4 v0, in vec4 v1, in vec4 v2, const int n, const float dt)
- {
- vec4 p;
- float t;
- int i;
- for(i = 0, t = 0.0; i < n; ++i, t += dt)
- {
- p = sampleCatmullRom(vp, v0, v1, v2, t);
- gl_Position = uMVP * p;
- EmitVertex();
- }
- EndPrimitive();
- }
- void drawCubicBezierSamples(in vec4 v0, in vec4 v1, in vec4 v2, in vec4 v3, const int n, const float dt)
- {
- vec4 p;
- float t;
- int i;
- for(i = 0, t = 0.0; i < n; ++i, t += dt)
- {
- //General test
- /*vec4 v[MAX_WAYPOINTS] = vec4[MAX_WAYPOINTS](v0,v1,v2,v3,
- 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),
- 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.
- p = sampleBezierN(v, 3, 0, t);*/
- //Specialized function
- p = sampleBezier3(v0, v1, v2, v3, t);
- gl_Position = uMVP * p;
- EmitVertex();
- }
- EndPrimitive();
- }
- void drawCubicHermiteSamples(in vec4 v0, in vec4 v1, in vec4 m0, in vec4 m1, const int n, const float dt)
- {
- vec4 p;
- float t;
- int i;
- for(i = 0, t = 0.0; i < n; ++i, t += dt)
- {
- p = sampleCubicHermite(v0, v1, m0, m1, t);
- gl_Position = uMVP * p;
- EmitVertex();
- }
- EndPrimitive();
- }
- void main()
- {
- int w0 = vPassInstanceID[0];
- int w1 = w0 + 1 < uPathWaypointCount ? w0 + 1 : w0;
- int wp = w0 > 0 ? w0 - 1 : w0;
- int w2 = w1 + 1 < uPathWaypointCount ? w1 + 1 : w1;
- int n = MAX_VERTICES;
- float dt = 1.0f / float(n);
- if(uPathWaypoint0 == w0) vPassColor = vec4(0.0f, 0.5f, 1.0f, 1.0f); //TODO: Step function here instead
- else vPassColor = vec4(0.0f, 0.25f, 0.5f, 1.0f);
- // (8)
- switch (uPathMode)
- {
- case 0:
- drawLineSamples(waypoint[w0], waypoint[w1], n, dt);
- break;
- case 1:
- drawCatmullRomSamples(waypoint[wp], waypoint[w0], waypoint[w1], waypoint[w2], n, dt);
- break;
- case 2:
- drawCubicBezierSamples(waypoint[w0], waypointHandle[w0], waypointHandle[w1], waypoint[w1], n, dt);
- break;
- case 3:
- drawCubicHermiteSamples(waypoint[w0], waypoint[w1], waypointHandle[w0], waypointHandle[w1], n, dt);
- break;
- };
- }
Add Comment
Please, Sign In to add comment