Project

General

Profile

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

library / src / main / res / raw / main_vertex_shader.glsl @ 78ff6ea9

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 24804c15 Leszek Koltunski
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
in float a_Component;                 // The component a vertex belongs to.
27
                                      // to a vertex effect. An effect will only be active on a vertex iff (a_Association & vAssociation[effect]) != 0.
28
                                      // ( see VertexEffect.retSection() )
29
30 a2878a67 Leszek Koltunski
out vec3 v_Position;                  // for Transform Feedback only
31 24804c15 Leszek Koltunski
out vec3 v_endPosition;               // for Transform Feedback only
32
out vec3 v_Normal;                    //
33
out vec2 v_TexCoordinate;             //
34 5e331bc8 Leszek Koltunski
35 24804c15 Leszek Koltunski
uniform mat4 u_MVPMatrix;             // u_MVMatrixP * projection.
36
uniform mat4 u_MVMatrixP;             // the combined model/view matrix. (for points)
37
uniform mat4 u_MVMatrixV;             // the combined model/view matrix. (for vectors)
38
                                      // which need to work differently on points and vectors
39
uniform float u_Inflate;              // how much should we inflate (>0.0) or deflate (<0.0) the mesh.
40
uniform int u_TransformFeedback;      // are we doing the transform feedback now?
41 6a06a912 Leszek Koltunski
42 9f9924f8 Leszek Koltunski
layout (std140) uniform componentCenter
43 a2878a67 Leszek Koltunski
  {
44 45d530fc Leszek Koltunski
  vec4 vComCenter[MAX_COMPON];        // centers of mesh components. 4 floats: (x,y,z,unused)
45 a2878a67 Leszek Koltunski
  };
46
47 6a06a912 Leszek Koltunski
#if NUM_VERTEX>0
48 24804c15 Leszek Koltunski
uniform int vNumEffects;              // total number of vertex effects
49 78ff6ea9 Leszek Koltunski
50
layout (std140) uniform vUniformProperties
51
  {
52
  ivec4 vProperties[NUM_VERTEX];      // their properties, 4 ints:
53 24804c15 Leszek Koltunski
                                      // 1: name of the effect
54
                                      // 2: effect's AND association
55
                                      // 3: reserved int (probably another AND assoc in the future)
56
                                      // 4: effect's EQU association
57 78ff6ea9 Leszek Koltunski
  };
58 96e3b88a Leszek Koltunski
59 24804c15 Leszek Koltunski
uniform vec4 vUniforms[3*NUM_VERTEX]; // i-th effect is 3 consecutive vec4's: [3*i], [3*i+1], [3*i+2].
60
                                      // The first vec4 is the Interpolated values,
61
                                      // second vec4: first float - cache, next 3: Center, the third -  the Region.
62 0bd9f644 Leszek Koltunski
63 9f9924f8 Leszek Koltunski
layout (std140) uniform componentAssociation
64 0bd9f644 Leszek Koltunski
  {
65 24804c15 Leszek Koltunski
  ivec4 vComAssoc[MAX_COMPON];        // component Associations, 4 ints:
66
                                      // 1: component's AND association
67
                                      // 2: reserved. Probably another AND assoc in the future.
68
                                      // 3: component's EQU association
69
                                      // 4: reserved
70 0bd9f644 Leszek Koltunski
  };
71 341c803d Leszek Koltunski
72
//////////////////////////////////////////////////////////////////////////////////////////////
73
// HELPER FUNCTIONS
74
//////////////////////////////////////////////////////////////////////////////////////////////
75 353f7580 Leszek Koltunski
// Return degree of the point as defined by the Region. Currently only supports spherical regions.
76 9420f2fe Leszek Koltunski
//
77
// Let 'PS' be the vector from point P (the current vertex) to point S (the center of the effect).
78 353f7580 Leszek Koltunski
// Let region.xyz be the vector from point S to point O (the center point of the region sphere)
79
// Let region.w be the radius of the region sphere.
80
// (This all should work regardless if S is inside or outside of the sphere).
81 73af5285 Leszek Koltunski
//
82 353f7580 Leszek Koltunski
// Then, the degree of a point with respect to a given (spherical!) Region is defined by:
83 9420f2fe Leszek Koltunski
//
84 353f7580 Leszek Koltunski
// If P is outside the sphere, return 0.
85 50be8733 Leszek Koltunski
// Otherwise, let X be the point where the halfline SP meets the sphere - then return |PX|/|SX|,
86 9420f2fe Leszek Koltunski
// aka the 'degree' of point P.
87
//
88 ff8ad0a7 Leszek Koltunski
// We solve the triangle OPX.
89 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|)
90
// 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)
91
// where a = PS*PO/|PS| but we are really looking for d = |PX|/(|PX|+|PS|) = 1/(1+ (|PS|/|PX|) ) and
92
// |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.
93 341c803d Leszek Koltunski
94 0f10a0b6 Leszek Koltunski
float degree(in vec4 region, in vec3 PS)
95 341c803d Leszek Koltunski
  {
96 1e667536 Leszek Koltunski
  float ps_sq = dot(PS,PS);
97 9420f2fe Leszek Koltunski
98 1e667536 Leszek Koltunski
  if( ps_sq==0.0 ) return 1.0;
99 9420f2fe Leszek Koltunski
100 1e667536 Leszek Koltunski
  vec3 PO = PS + region.xyz;
101
  float d = region.w*region.w-dot(PO,PO);
102
103
  if( d<=0.0 ) return 0.0;
104
105
  float b = dot(PS,PO)/ps_sq;
106
107
  return 1.0 / (1.0 + 1.0/(sqrt(b*b + d/ps_sq)-b));
108 341c803d Leszek Koltunski
  }
109
110 81a0b906 leszek
#endif  // NUM_VERTEX>0
111
112 6a06a912 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
113 b2dc3c19 Leszek Koltunski
114
void main()
115
  {
116 a2878a67 Leszek Koltunski
  int component = int(a_Component);
117
  vec3 v = a_Position + u_Inflate*(a_Position - vComCenter[component].xyz);
118 a8537f43 Leszek Koltunski
  vec3 n = a_Normal;
119 6a06a912 Leszek Koltunski
120
#if NUM_VERTEX>0
121 7cd24173 leszek
  int effect=0;
122 b2b83871 Leszek Koltunski
123 6a06a912 Leszek Koltunski
  for(int i=0; i<vNumEffects; i++)
124
    {
125 24804c15 Leszek Koltunski
    if( ((vComAssoc[component].x & vProperties[i].y) != 0) || (vComAssoc[component].z == vProperties[i].w) )
126 36d65d88 Leszek Koltunski
      {
127
      // ENABLED EFFECTS WILL BE INSERTED HERE
128 b2b83871 Leszek Koltunski
129 36d65d88 Leszek Koltunski
      }
130 7cd24173 leszek
    effect+=3;
131 6a06a912 Leszek Koltunski
    }
132
#endif
133 667884b0 Leszek Koltunski
134
#ifdef PREAPPLY
135 62c869ad Leszek Koltunski
  v_Position   = v;
136
  v_endPosition= n;
137 667884b0 Leszek Koltunski
#else
138 62c869ad Leszek Koltunski
  if( u_TransformFeedback == 1 )
139
    {
140
    vec4 tmp1 =  u_MVMatrixP * vec4(v,1.0);
141
    vec4 tmp2 =  normalize(u_MVMatrixV * vec4(n,0.0));
142
143
    v_Position    = vec3(tmp1);
144
    v_endPosition = vec3(tmp1+100.0*tmp2);
145
    }
146 53873b84 Leszek Koltunski
  else
147
    {
148
    v_Position = v;
149
    }
150 667884b0 Leszek Koltunski
#endif
151
152 2dacdeb2 Leszek Koltunski
  v_TexCoordinate = a_TexCoordinate;
153 62c869ad Leszek Koltunski
  v_Normal        = normalize(vec3(u_MVMatrixV*vec4(n,0.0)));
154 3fc9327a Leszek Koltunski
  gl_Position     = u_MVPMatrix*vec4(v,1.0);
155 d333eb6b Leszek Koltunski
  }