Project

General

Profile

Download (6.35 KB) Statistics
| Branch: | Revision:

library / src / main / res / raw / main_vertex_shader.glsl @ e54bfada

1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                          //
3
//                                                                                          //
4
// This file is part of DistortedLibrary.                                                          //
5
//                                                                                          //
6
// DistortedLibrary is free software: you can redistribute it and/or modify                        //
7
// it under the terms of the GNU General Public License as published by                     //
8
// the Free Software Foundation, either version 2 of the License, or                        //
9
// (at your option) any later version.                                                      //
10
//                                                                                          //
11
// DistortedLibrary is distributed in the hope that it will be useful,                             //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                           //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                            //
14
// GNU General Public License for more details.                                             //
15
//                                                                                          //
16
// You should have received a copy of the GNU General Public License                        // 
17
// along with DistortedLibrary.  If not, see <http://www.gnu.org/licenses/>.                       //
18
//////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
precision highp float;
21
precision highp int;
22

    
23
in vec3 a_Position;                  // Per-vertex position.
24
in vec3 a_Normal;                    // Per-vertex normal vector.
25
in vec3 a_Inflate;                   // This vector describes the direction this vertex needs to go when we 'inflate' the whole mesh.
26
                                     // If the mesh is locally smooth, this is equal to the normal vector. Otherwise (on sharp edges) - no.
27
in vec2 a_TexCoordinate;             // Per-vertex texture coordinate.
28

    
29
out vec3 v_Position;                 //
30
out vec3 v_endPosition;              // for Transform Feedback only
31

    
32
#ifdef PREAPPLY
33
out vec3 v_Inflate;                  // Transform Feedback for preapply effects
34
#endif
35

    
36
out vec3 v_Normal;                   //
37
out vec2 v_TexCoordinate;            //
38

    
39
uniform mat4 u_MVPMatrix;            // the combined model/view/projection matrix.
40
uniform mat4 u_MVMatrix;             // the combined model/view matrix.
41
uniform float u_Inflate;             // how much should we inflate (>0.0) or deflate (<0.0) the mesh.
42

    
43
#if NUM_VERTEX>0
44
uniform int vNumEffects;             // total number of vertex effects
45
uniform int vName[NUM_VERTEX];       // their names.
46
uniform vec4 vUniforms[3*NUM_VERTEX];// i-th effect is 3 consecutive vec4's: [3*i], [3*i+1], [3*i+2].
47
                                     // The first vec4 is the Interpolated values,
48
                                     // second vec4: first float - cache, next 3: Center, the third -  the Region.
49

    
50
//////////////////////////////////////////////////////////////////////////////////////////////
51
// HELPER FUNCTIONS
52
//////////////////////////////////////////////////////////////////////////////////////////////
53
// Return degree of the point as defined by the Region. Currently only supports spherical regions.
54
//
55
// Let 'PS' be the vector from point P (the current vertex) to point S (the center of the effect).
56
// Let region.xyz be the vector from point S to point O (the center point of the region sphere)
57
// Let region.w be the radius of the region sphere.
58
// (This all should work regardless if S is inside or outside of the sphere).
59
//
60
// Then, the degree of a point with respect to a given (spherical!) Region is defined by:
61
//
62
// If P is outside the sphere, return 0.
63
// Otherwise, let X be the point where the halfline SP meets the sphere - then return |PX|/|SX|,
64
// aka the 'degree' of point P.
65
//
66
// We solve the triangle OPX.
67
// We know the lengths |PO|, |OX| and the angle OPX, because cos(OPX) = cos(180-OPS) = -cos(OPS) = -PS*PO/(|PS|*|PO|)
68
// then from the law of cosines PX^2 + PO^2 - 2*PX*PO*cos(OPX) = OX^2 so PX = -a + sqrt(a^2 + OX^2 - PO^2)
69
// where a = PS*PO/|PS| but we are really looking for d = |PX|/(|PX|+|PS|) = 1/(1+ (|PS|/|PX|) ) and
70
// |PX|/|PS| = -b + sqrt(b^2 + (OX^2-PO^2)/PS^2) where b=PS*PO/|PS|^2 which can be computed with only one sqrt.
71

    
72
float degree(in vec4 region, in vec3 PS)
73
  {
74
  vec3 PO  = PS + region.xyz;
75
  float D = region.w*region.w-dot(PO,PO);      // D = |OX|^2 - |PO|^2
76

    
77
  if( D<=0.0 ) return 0.0;
78

    
79
  float ps_sq = dot(PS,PS);
80
  float one_over_ps_sq = 1.0/(ps_sq-(sign(ps_sq)-1.0));  // return 1.0 if ps_sq = 0.0
81
                                                         // Important: if we want to write
82
                                                         // b = 1/a if a!=0, b=1 otherwise
83
                                                         // we need to write that as
84
                                                         // b = 1 / ( a-(sign(a)-1) )
85
                                                         // [ and NOT 1 / ( a + 1 - sign(a) ) ]
86
                                                         // because the latter, if 0<a<2^-24,
87
                                                         // will suffer from round-off error and in this case
88
                                                         // a + 1.0 = 1.0 !! so 1 / (a+1-sign(a)) = 1/0 !
89
  float DOT  = dot(PS,PO)*one_over_ps_sq;
90

    
91
  return 1.0 / (1.0 + 1.0/(sqrt(DOT*DOT+D*one_over_ps_sq)-DOT));
92
  }
93

    
94
#endif  // NUM_VERTEX>0
95

    
96
//////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
void main()
99
  {
100
  vec3 v = a_Position + u_Inflate*a_Inflate;
101
  vec3 n = a_Normal;
102

    
103
#ifdef PREAPPLY
104
  vec3 inf = a_Inflate;
105
#endif
106

    
107
#if NUM_VERTEX>0
108
  int effect=0;
109

    
110
  for(int i=0; i<vNumEffects; i++)
111
    {
112
    // ENABLED EFFECTS WILL BE INSERTED HERE
113

    
114
    effect+=3;
115
    }
116
#endif
117
   
118
  v_Position      = v;
119

    
120
#ifdef PREAPPLY
121
  v_endPosition   = n;
122
  v_Inflate       = inf;
123
#else
124
  v_endPosition   = v + 0.5*n;
125
#endif
126

    
127
  v_TexCoordinate = a_TexCoordinate;
128
  v_Normal        = normalize(vec3(u_MVMatrix*vec4(n,0.0)));
129
  gl_Position     = u_MVPMatrix*vec4(v,1.0);
130
  }                               
(6-6/14)