Project

General

Profile

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

library / src / main / res / raw / main_vertex_shader.glsl @ 3f12341d

1 7f1735dc Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20 d333eb6b Leszek Koltunski
21 341151fc Leszek Koltunski
precision highp float;
22 c1a38ba3 Leszek Koltunski
precision highp int;
23 2e7ad49f Leszek Koltunski
24 24804c15 Leszek Koltunski
in vec3 a_Position;                   // Per-vertex position.
25
in vec3 a_Normal;                     // Per-vertex normal vector.
26
in vec2 a_TexCoordinate;              // Per-vertex texture coordinate.
27
in float a_Component;                 // The component a vertex belongs to.
28
                                      // to a vertex effect. An effect will only be active on a vertex iff (a_Association & vAssociation[effect]) != 0.
29
                                      // ( see VertexEffect.retSection() )
30
31 a2878a67 Leszek Koltunski
out vec3 v_Position;                  // for Transform Feedback only
32 24804c15 Leszek Koltunski
out vec3 v_endPosition;               // for Transform Feedback only
33
out vec3 v_Normal;                    //
34
out vec2 v_TexCoordinate;             //
35 5e331bc8 Leszek Koltunski
36 24804c15 Leszek Koltunski
uniform mat4 u_MVPMatrix;             // u_MVMatrixP * projection.
37
uniform mat4 u_MVMatrixP;             // the combined model/view matrix. (for points)
38
uniform mat4 u_MVMatrixV;             // the combined model/view matrix. (for vectors)
39
                                      // which need to work differently on points and vectors
40
uniform float u_Inflate;              // how much should we inflate (>0.0) or deflate (<0.0) the mesh.
41
uniform int u_TransformFeedback;      // are we doing the transform feedback now?
42 6a06a912 Leszek Koltunski
43 46d463a4 Leszek Koltunski
#ifdef COMP_CENTERS
44 9f9924f8 Leszek Koltunski
layout (std140) uniform componentCenter
45 a2878a67 Leszek Koltunski
  {
46 45d530fc Leszek Koltunski
  vec4 vComCenter[MAX_COMPON];        // centers of mesh components. 4 floats: (x,y,z,unused)
47 a2878a67 Leszek Koltunski
  };
48 46d463a4 Leszek Koltunski
#endif
49 a2878a67 Leszek Koltunski
50 6a06a912 Leszek Koltunski
#if NUM_VERTEX>0
51 24804c15 Leszek Koltunski
uniform int vNumEffects;              // total number of vertex effects
52 78ff6ea9 Leszek Koltunski
53
layout (std140) uniform vUniformProperties
54
  {
55
  ivec4 vProperties[NUM_VERTEX];      // their properties, 4 ints:
56 24804c15 Leszek Koltunski
                                      // 1: name of the effect
57
                                      // 2: effect's AND association
58
                                      // 3: reserved int (probably another AND assoc in the future)
59
                                      // 4: effect's EQU association
60 78ff6ea9 Leszek Koltunski
  };
61 96e3b88a Leszek Koltunski
62 de77a6c5 Leszek Koltunski
layout (std140) uniform vUniformFloats
63
  {
64
  vec4 vUniforms[3*NUM_VERTEX];       // i-th effect is 3 consecutive vec4's: [3*i], [3*i+1], [3*i+2].
65 24804c15 Leszek Koltunski
                                      // The first vec4 is the Interpolated values,
66
                                      // second vec4: first float - cache, next 3: Center, the third -  the Region.
67 de77a6c5 Leszek Koltunski
  };
68 0bd9f644 Leszek Koltunski
69 eff6e3d3 Leszek Koltunski
#ifdef BUGGY_UBOS
70
layout (packed) uniform componentAssociation
71
  {
72
  ivec2 vComAssoc[MAX_COMPON];        // component 'AND' and 'EQU' Associations
73
  };
74
#else
75 073e5a7a Leszek Koltunski
layout (std140) uniform componentAssociation
76 0bd9f644 Leszek Koltunski
  {
77 073e5a7a Leszek Koltunski
  ivec4 vComAssoc[MAX_COMPON];        // component 'AND' and 'EQU' Associations
78 0bd9f644 Leszek Koltunski
  };
79 eff6e3d3 Leszek Koltunski
#endif
80 341c803d Leszek Koltunski
81
//////////////////////////////////////////////////////////////////////////////////////////////
82
// HELPER FUNCTIONS
83
//////////////////////////////////////////////////////////////////////////////////////////////
84 353f7580 Leszek Koltunski
// Return degree of the point as defined by the Region. Currently only supports spherical regions.
85 9420f2fe Leszek Koltunski
//
86
// Let 'PS' be the vector from point P (the current vertex) to point S (the center of the effect).
87 353f7580 Leszek Koltunski
// Let region.xyz be the vector from point S to point O (the center point of the region sphere)
88
// Let region.w be the radius of the region sphere.
89
// (This all should work regardless if S is inside or outside of the sphere).
90 73af5285 Leszek Koltunski
//
91 353f7580 Leszek Koltunski
// Then, the degree of a point with respect to a given (spherical!) Region is defined by:
92 9420f2fe Leszek Koltunski
//
93 353f7580 Leszek Koltunski
// If P is outside the sphere, return 0.
94 50be8733 Leszek Koltunski
// Otherwise, let X be the point where the halfline SP meets the sphere - then return |PX|/|SX|,
95 9420f2fe Leszek Koltunski
// aka the 'degree' of point P.
96
//
97 ff8ad0a7 Leszek Koltunski
// We solve the triangle OPX.
98 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|)
99
// 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)
100
// where a = PS*PO/|PS| but we are really looking for d = |PX|/(|PX|+|PS|) = 1/(1+ (|PS|/|PX|) ) and
101
// |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.
102 341c803d Leszek Koltunski
103 0f10a0b6 Leszek Koltunski
float degree(in vec4 region, in vec3 PS)
104 341c803d Leszek Koltunski
  {
105 1e667536 Leszek Koltunski
  float ps_sq = dot(PS,PS);
106 9420f2fe Leszek Koltunski
107 1e667536 Leszek Koltunski
  if( ps_sq==0.0 ) return 1.0;
108 9420f2fe Leszek Koltunski
109 1e667536 Leszek Koltunski
  vec3 PO = PS + region.xyz;
110
  float d = region.w*region.w-dot(PO,PO);
111
112
  if( d<=0.0 ) return 0.0;
113
114
  float b = dot(PS,PO)/ps_sq;
115
116
  return 1.0 / (1.0 + 1.0/(sqrt(b*b + d/ps_sq)-b));
117 341c803d Leszek Koltunski
  }
118
119 81a0b906 leszek
#endif  // NUM_VERTEX>0
120
121 6a06a912 Leszek Koltunski
//////////////////////////////////////////////////////////////////////////////////////////////
122 b2dc3c19 Leszek Koltunski
123
void main()
124
  {
125 a2878a67 Leszek Koltunski
  int component = int(a_Component);
126 a8537f43 Leszek Koltunski
  vec3 n = a_Normal;
127 46d463a4 Leszek Koltunski
#ifdef COMP_CENTERS
128
  vec3 v = a_Position + u_Inflate*(a_Position - vComCenter[component].xyz);
129
#else
130
  vec3 v = a_Position + u_Inflate*a_Position;
131
#endif
132 6a06a912 Leszek Koltunski
133
#if NUM_VERTEX>0
134 7cd24173 leszek
  int effect=0;
135 b2b83871 Leszek Koltunski
136 6a06a912 Leszek Koltunski
  for(int i=0; i<vNumEffects; i++)
137
    {
138 80961fc1 Leszek Koltunski
    if( ((vComAssoc[component].x & vProperties[i].y) != 0) || (vComAssoc[component].y == vProperties[i].w) )
139 36d65d88 Leszek Koltunski
      {
140
      // ENABLED EFFECTS WILL BE INSERTED HERE
141 b2b83871 Leszek Koltunski
142 36d65d88 Leszek Koltunski
      }
143 7cd24173 leszek
    effect+=3;
144 6a06a912 Leszek Koltunski
    }
145
#endif
146 667884b0 Leszek Koltunski
147
#ifdef PREAPPLY
148 62c869ad Leszek Koltunski
  v_Position   = v;
149
  v_endPosition= n;
150 667884b0 Leszek Koltunski
#else
151 62c869ad Leszek Koltunski
  if( u_TransformFeedback == 1 )
152
    {
153
    vec4 tmp1 =  u_MVMatrixP * vec4(v,1.0);
154
    vec4 tmp2 =  normalize(u_MVMatrixV * vec4(n,0.0));
155
156
    v_Position    = vec3(tmp1);
157
    v_endPosition = vec3(tmp1+100.0*tmp2);
158
    }
159 53873b84 Leszek Koltunski
  else
160
    {
161
    v_Position = v;
162
    }
163 667884b0 Leszek Koltunski
#endif
164
165 2dacdeb2 Leszek Koltunski
  v_TexCoordinate = a_TexCoordinate;
166 62c869ad Leszek Koltunski
  v_Normal        = normalize(vec3(u_MVMatrixV*vec4(n,0.0)));
167 3fc9327a Leszek Koltunski
  gl_Position     = u_MVPMatrix*vec4(v,1.0);
168 d333eb6b Leszek Koltunski
  }