Project

General

Profile

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

library / src / main / res / raw / main_vertex_shader.glsl @ 53873b84

1 d333eb6b Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                          //
3
//                                                                                          //
4 535a45bc Leszek Koltunski
// This file is part of Distorted.                                                          //
5 d333eb6b Leszek Koltunski
//                                                                                          //
6 535a45bc Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                        //
7 d333eb6b Leszek Koltunski
// 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 535a45bc Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                             //
12 d333eb6b Leszek Koltunski
// 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 535a45bc Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                       //
18 d333eb6b Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
19
20 341151fc Leszek Koltunski
precision highp float;
21 c1a38ba3 Leszek Koltunski
precision highp int;
22 2e7ad49f Leszek Koltunski
23 94f6d472 Leszek Koltunski
in vec3 a_Position;                  // Per-vertex position.
24
in vec3 a_Normal;                    // Per-vertex normal vector.
25 6f2d931d Leszek Koltunski
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 94f6d472 Leszek Koltunski
in vec2 a_TexCoordinate;             // Per-vertex texture coordinate.
28 36d65d88 Leszek Koltunski
in float a_Component;                // The component a vertex belongs to.
29 1e672c1d Leszek Koltunski
                                     // to a vertex effect. An effect will only be active on a vertex iff (a_Association & vAssociation[effect]) != 0.
30
                                     // ( see VertexEffect.retSection() )
31 6f2d931d Leszek Koltunski
32 94f6d472 Leszek Koltunski
out vec3 v_Position;                 //
33 3fc9327a Leszek Koltunski
out vec3 v_endPosition;              // for Transform Feedback only
34 e54bfada Leszek Koltunski
35
#ifdef PREAPPLY
36
out vec3 v_Inflate;                  // Transform Feedback for preapply effects
37
#endif
38
39 94f6d472 Leszek Koltunski
out vec3 v_Normal;                   //
40
out vec2 v_TexCoordinate;            //
41 5e331bc8 Leszek Koltunski
42 0bd9f644 Leszek Koltunski
uniform mat4 u_MVPMatrix;            // u_MVMatrixP * projection.
43 62c869ad Leszek Koltunski
uniform mat4 u_MVMatrixP;            // the combined model/view matrix. (for points)
44
uniform mat4 u_MVMatrixV;            // the combined model/view matrix. (for vectors)
45
                                     // which need to work differently on points and vectors
46 7a5e538a Leszek Koltunski
uniform float u_Inflate;             // how much should we inflate (>0.0) or deflate (<0.0) the mesh.
47 62c869ad Leszek Koltunski
uniform int u_TransformFeedback;     // are we doing the transform feedback now?
48 6a06a912 Leszek Koltunski
49
#if NUM_VERTEX>0
50 ad33f883 Leszek Koltunski
uniform int vNumEffects;             // total number of vertex effects
51 15aa7d94 Leszek Koltunski
uniform int vName[NUM_VERTEX];       // their names.
52 96e3b88a Leszek Koltunski
uniform int vEffAndAssoc[NUM_VERTEX];// Associations of the vertex effects. Those are used to connect an effect to a Mesh component.
53
uniform int vEffEquAssoc[NUM_VERTEX];// Components the vertex effects work on. Likewise used to connect an effect to a Mesh component.
54
55 f6cac1f6 Leszek Koltunski
uniform vec4 vUniforms[3*NUM_VERTEX];// i-th effect is 3 consecutive vec4's: [3*i], [3*i+1], [3*i+2].
56
                                     // The first vec4 is the Interpolated values,
57 4aa38649 Leszek Koltunski
                                     // second vec4: first float - cache, next 3: Center, the third -  the Region.
58 0bd9f644 Leszek Koltunski
59 96e3b88a Leszek Koltunski
layout (shared, binding=1) uniform meshAssociation
60 0bd9f644 Leszek Koltunski
  {
61
  int vComAndAssoc[MAX_COMPON]; // 'logical AND' association of the component.
62
  int vComEquAssoc[MAX_COMPON]; // 'equal' association of the component.
63
  };
64 341c803d Leszek Koltunski
65
//////////////////////////////////////////////////////////////////////////////////////////////
66
// HELPER FUNCTIONS
67
//////////////////////////////////////////////////////////////////////////////////////////////
68 353f7580 Leszek Koltunski
// Return degree of the point as defined by the Region. Currently only supports spherical regions.
69 9420f2fe Leszek Koltunski
//
70
// Let 'PS' be the vector from point P (the current vertex) to point S (the center of the effect).
71 353f7580 Leszek Koltunski
// Let region.xyz be the vector from point S to point O (the center point of the region sphere)
72
// Let region.w be the radius of the region sphere.
73
// (This all should work regardless if S is inside or outside of the sphere).
74 73af5285 Leszek Koltunski
//
75 353f7580 Leszek Koltunski
// Then, the degree of a point with respect to a given (spherical!) Region is defined by:
76 9420f2fe Leszek Koltunski
//
77 353f7580 Leszek Koltunski
// If P is outside the sphere, return 0.
78 50be8733 Leszek Koltunski
// Otherwise, let X be the point where the halfline SP meets the sphere - then return |PX|/|SX|,
79 9420f2fe Leszek Koltunski
// aka the 'degree' of point P.
80
//
81 ff8ad0a7 Leszek Koltunski
// We solve the triangle OPX.
82 9420f2fe Leszek Koltunski
// We know the lengths |PO|, |OX| and the angle OPX, because cos(OPX) = cos(180-OPS) = -cos(OPS) = -PS*PO/(|PS|*|PO|)
83
// 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)
84
// where a = PS*PO/|PS| but we are really looking for d = |PX|/(|PX|+|PS|) = 1/(1+ (|PS|/|PX|) ) and
85
// |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.
86 341c803d Leszek Koltunski
87 0f10a0b6 Leszek Koltunski
float degree(in vec4 region, in vec3 PS)
88 341c803d Leszek Koltunski
  {
89 1e667536 Leszek Koltunski
  float ps_sq = dot(PS,PS);
90 9420f2fe Leszek Koltunski
91 1e667536 Leszek Koltunski
  if( ps_sq==0.0 ) return 1.0;
92 9420f2fe Leszek Koltunski
93 1e667536 Leszek Koltunski
  vec3 PO = PS + region.xyz;
94
  float d = region.w*region.w-dot(PO,PO);
95
96
  if( d<=0.0 ) return 0.0;
97
98
  float b = dot(PS,PO)/ps_sq;
99
100
  return 1.0 / (1.0 + 1.0/(sqrt(b*b + d/ps_sq)-b));
101 341c803d Leszek Koltunski
  }
102
103 81a0b906 leszek
#endif  // NUM_VERTEX>0
104
105 6a06a912 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
106 b2dc3c19 Leszek Koltunski
107
void main()
108
  {
109 277eddbb Leszek Koltunski
  vec3 v = a_Position + u_Inflate*a_Inflate;
110 a8537f43 Leszek Koltunski
  vec3 n = a_Normal;
111 6a06a912 Leszek Koltunski
112 e54bfada Leszek Koltunski
#ifdef PREAPPLY
113
  vec3 inf = a_Inflate;
114
#endif
115
116 6a06a912 Leszek Koltunski
#if NUM_VERTEX>0
117 7cd24173 leszek
  int effect=0;
118 36d65d88 Leszek Koltunski
  int component = int(a_Component);
119 b2b83871 Leszek Koltunski
120 6a06a912 Leszek Koltunski
  for(int i=0; i<vNumEffects; i++)
121
    {
122 2aeb75aa Leszek Koltunski
    if( ((vComAndAssoc[component] & vEffAndAssoc[i]) != 0) || (vComEquAssoc[component] == vEffEquAssoc[i]) )
123 36d65d88 Leszek Koltunski
      {
124
      // ENABLED EFFECTS WILL BE INSERTED HERE
125 b2b83871 Leszek Koltunski
126 36d65d88 Leszek Koltunski
      }
127 7cd24173 leszek
    effect+=3;
128 6a06a912 Leszek Koltunski
    }
129
#endif
130 667884b0 Leszek Koltunski
131
#ifdef PREAPPLY
132 62c869ad Leszek Koltunski
  v_Position   = v;
133
  v_endPosition= n;
134
  v_Inflate    = inf;
135 667884b0 Leszek Koltunski
#else
136 62c869ad Leszek Koltunski
  if( u_TransformFeedback == 1 )
137
    {
138
    vec4 tmp1 =  u_MVMatrixP * vec4(v,1.0);
139
    vec4 tmp2 =  normalize(u_MVMatrixV * vec4(n,0.0));
140
141
    v_Position    = vec3(tmp1);
142
    v_endPosition = vec3(tmp1+100.0*tmp2);
143
    }
144 53873b84 Leszek Koltunski
  else
145
    {
146
    v_Position = v;
147
    }
148 667884b0 Leszek Koltunski
#endif
149
150 2dacdeb2 Leszek Koltunski
  v_TexCoordinate = a_TexCoordinate;
151 62c869ad Leszek Koltunski
  v_Normal        = normalize(vec3(u_MVMatrixV*vec4(n,0.0)));
152 3fc9327a Leszek Koltunski
  gl_Position     = u_MVPMatrix*vec4(v,1.0);
153 d333eb6b Leszek Koltunski
  }