Project

General

Profile

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

library / src / main / res / raw / main_vertex_shader.glsl @ 2f35828c

1
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                          //
3
//                                                                                          //
4
// This file is part of Distorted.                                                          //
5
//                                                                                          //
6
// Distorted 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
// Distorted 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 Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18
//////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
precision mediump float;
21

    
22
#if __VERSION__ != 100
23
in vec3 a_Position;                  // Per-vertex position.
24
in vec3 a_Normal;                    // Per-vertex normal vector.
25
in vec2 a_TexCoordinate;             // Per-vertex texture coordinate.
26
out vec3 v_Position;                 //
27
out vec3 v_endPosition;              // for Transform Feedback only
28
out vec3 v_Normal;                   //
29
out vec2 v_TexCoordinate;            //
30
#else
31
attribute vec3 a_Position;           // Per-vertex position.
32
attribute vec3 a_Normal;             // Per-vertex normal vector.
33
attribute vec2 a_TexCoordinate;      // Per-vertex texture coordinate.
34
varying vec3 v_Position;             //
35
varying vec3 v_endPosition;          // for Transform Feedback only
36
varying vec3 v_Normal;               //
37
varying vec2 v_TexCoordinate;        //
38
#endif
39

    
40
uniform vec3 u_objD;                 // half of object width x half of object height X half the depth;
41
                                     // point (0,0,0) is the center of the object
42

    
43
uniform mat4 u_MVPMatrix;            // the combined model/view/projection matrix.
44
uniform mat4 u_MVMatrix;             // the combined model/view matrix.
45

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

    
53
//////////////////////////////////////////////////////////////////////////////////////////////
54
// HELPER FUNCTIONS
55
//////////////////////////////////////////////////////////////////////////////////////////////
56
// The trick below is the if-less version of the
57
//
58
// t = dx<0.0 ? (u_objD.x-v.x) / (u_objD.x-ux) : (u_objD.x+v.x) / (u_objD.x+ux);
59
// h = dy<0.0 ? (u_objD.y-v.y) / (u_objD.y-uy) : (u_objD.y+v.y) / (u_objD.y+uy);
60
// d = min(t,h);
61
//
62
// float d = min(-ps.x/(sign(ps.x)*u_objD.x+p.x),-ps.y/(sign(ps.y)*u_objD.y+p.y))+1.0;
63
//
64
// We still have to avoid division by 0 when p.x = +- u_objD.x or p.y = +- u_objD.y (i.e on the edge of the Object)
65
// We do that by first multiplying the above 'float d' with sign(denominator1*denominator2)^2.
66
//
67
//////////////////////////////////////////////////////////////////////////////////////////////
68
// return degree of the point as defined by the bitmap rectangle
69

    
70
float degree_bitmap(in vec2 S, in vec2 PS)
71
  {
72
  vec2 A = sign(PS)*u_objD.xy + S;
73

    
74
  vec2 signA = sign(A);                           //
75
  vec2 signA_SQ = signA*signA;                    // div = PS/A if A!=0, 0 otherwise.
76
  vec2 div = signA_SQ*PS/(A-(vec2(1,1)-signA_SQ));//
77

    
78
  return 1.0-max(div.x,div.y);
79
  }
80

    
81
//////////////////////////////////////////////////////////////////////////////////////////////
82
// Return degree of the point as defined by the Region. Currently only supports circular regions.
83
//
84
// Let us first introduce some notation.
85
// Let 'PS' be the vector from point P (the current vertex) to point S (the center of the effect).
86
// Let region.xy be the vector from point S to point O (the center point of the region circle)
87
// Let region.z be the radius of the region circle.
88
// (This all should work regardless if S is inside or outside of the circle).
89
//
90
// Then, the degree of a point with respect to a given (circular!) Region is defined by:
91
//
92
// If P is outside the circle, return 0.
93
// Otherwise, let X be the point where the halfline SP meets the region circle - then return |PX|/||SX|,
94
// aka the 'degree' of point P.
95
//
96
// We solve the triangle OPX.
97
// We know the lengths |PO|, |OX| and the angle OPX, because cos(OPX) = cos(180-OPS) = -cos(OPS) = -PS*PO/(|PS|*|PO|)
98
// 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)
99
// where a = PS*PO/|PS| but we are really looking for d = |PX|/(|PX|+|PS|) = 1/(1+ (|PS|/|PX|) ) and
100
// |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.
101

    
102
float degree_region(in vec4 region, in vec2 PS)
103
  {
104
  vec2 PO  = PS + region.xy;
105
  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
106

    
107
  if( D<=0.0 ) return 0.0;
108

    
109
  float ps_sq = dot(PS,PS);
110
  float one_over_ps_sq = 1.0/(ps_sq-(sign(ps_sq)-1.0));  // return 1.0 if ps_sq = 0.0
111
                                                         // Important: if we want to write
112
                                                         // b = 1/a if a!=0, b=1 otherwise
113
                                                         // we need to write that as
114
                                                         // b = 1 / ( a-(sign(a)-1) )
115
                                                         // [ and NOT 1 / ( a + 1 - sign(a) ) ]
116
                                                         // because the latter, if 0<a<2^-24,
117
                                                         // will suffer from round-off error and in this case
118
                                                         // a + 1.0 = 1.0 !! so 1 / (a+1-sign(a)) = 1/0 !
119
  float DOT  = dot(PS,PO)*one_over_ps_sq;
120

    
121
  return 1.0 / (1.0 + 1.0/(sqrt(DOT*DOT+D*one_over_ps_sq)-DOT));
122
  }
123

    
124
//////////////////////////////////////////////////////////////////////////////////////////////
125
// return min(degree_bitmap,degree_region). Just like degree_region, currently only supports circles.
126

    
127
float degree(in vec4 region, in vec2 S, in vec2 PS)
128
  {
129
  vec2 PO  = PS + region.xy;
130
  float D = region.z*region.z-dot(PO,PO);      // D = |OX|^2 - |PO|^2
131

    
132
  if( D<=0.0 ) return 0.0;
133

    
134
  vec2 A = sign(PS)*u_objD.xy + S;
135
  vec2 signA = sign(A);
136
  vec2 signA_SQ = signA*signA;
137
  vec2 div = signA_SQ*PS/(A-(vec2(1,1)-signA_SQ));
138
  float E = 1.0-max(div.x,div.y);
139

    
140
  float ps_sq = dot(PS,PS);
141
  float one_over_ps_sq = 1.0/(ps_sq-(sign(ps_sq)-1.0));  // return 1.0 if ps_sq = 0.0
142
  float DOT  = dot(PS,PO)*one_over_ps_sq;
143

    
144
  return min(1.0/(1.0 + 1.0/(sqrt(DOT*DOT+D*one_over_ps_sq)-DOT)),E);
145
  }
146

    
147
#endif  // NUM_VERTEX>0
148

    
149
//////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
void main()
152
  {
153
  vec3 v = 2.0*u_objD*a_Position;
154
  vec3 n = a_Normal;
155

    
156
#if NUM_VERTEX>0
157
  int effect=0;
158

    
159
  for(int i=0; i<vNumEffects; i++)
160
    {
161
    // ENABLED EFFECTS WILL BE INSERTED HERE
162

    
163
    effect+=3;
164
    }
165
#endif
166
   
167
  v_Position      = v;
168
  v_endPosition   = v + (0.3*u_objD.x)*n;
169
  v_TexCoordinate = a_TexCoordinate;
170
  v_Normal        = normalize(vec3(u_MVMatrix*vec4(n,0.0)));
171
  gl_Position     = u_MVPMatrix*vec4(v,1.0);
172
  }                               
(7-7/9)