Project

General

Profile

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

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

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 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 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
out vec3 v_Position;                  // for Transform Feedback only
31
out vec3 v_endPosition;               // for Transform Feedback only
32
out vec3 v_Normal;                    //
33
out vec2 v_TexCoordinate;             //
34

    
35
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

    
42
#ifdef COMP_CENTERS
43
layout (std140) uniform componentCenter
44
  {
45
  vec4 vComCenter[MAX_COMPON];        // centers of mesh components. 4 floats: (x,y,z,unused)
46
  };
47
#endif
48

    
49
#if NUM_VERTEX>0
50
uniform int vNumEffects;              // total number of vertex effects
51

    
52
layout (std140) uniform vUniformProperties
53
  {
54
  ivec4 vProperties[NUM_VERTEX];      // their properties, 4 ints:
55
                                      // 1: name of the effect
56
                                      // 2: effect's AND association
57
                                      // 3: reserved int (probably another AND assoc in the future)
58
                                      // 4: effect's EQU association
59
  };
60

    
61
layout (std140) uniform vUniformFloats
62
  {
63
  vec4 vUniforms[3*NUM_VERTEX];       // i-th effect is 3 consecutive vec4's: [3*i], [3*i+1], [3*i+2].
64
                                      // The first vec4 is the Interpolated values,
65
                                      // second vec4: first float - cache, next 3: Center, the third -  the Region.
66
  };
67

    
68
#ifdef BUGGY_UBOS
69
layout (packed) uniform componentAssociation
70
  {
71
  ivec2 vComAssoc[MAX_COMPON];        // component 'AND' and 'EQU' Associations
72
  };
73
#else
74
layout (std140) uniform componentAssociation
75
  {
76
  ivec4 vComAssoc[MAX_COMPON];        // component 'AND' and 'EQU' Associations
77
  };
78
#endif
79

    
80
//////////////////////////////////////////////////////////////////////////////////////////////
81
// HELPER FUNCTIONS
82
//////////////////////////////////////////////////////////////////////////////////////////////
83
// Return degree of the point as defined by the Region. Currently only supports spherical regions.
84
//
85
// Let 'PS' be the vector from point P (the current vertex) to point S (the center of the effect).
86
// Let region.xyz be the vector from point S to point O (the center point of the region sphere)
87
// Let region.w be the radius of the region sphere.
88
// (This all should work regardless if S is inside or outside of the sphere).
89
//
90
// Then, the degree of a point with respect to a given (spherical!) Region is defined by:
91
//
92
// If P is outside the sphere, return 0.
93
// Otherwise, let X be the point where the halfline SP meets the sphere - 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(in vec4 region, in vec3 PS)
103
  {
104
  float ps_sq = dot(PS,PS);
105

    
106
  if( ps_sq==0.0 ) return 1.0;
107

    
108
  vec3 PO = PS + region.xyz;
109
  float d = region.w*region.w-dot(PO,PO);
110

    
111
  if( d<=0.0 ) return 0.0;
112

    
113
  float b = dot(PS,PO)/ps_sq;
114

    
115
  return 1.0 / (1.0 + 1.0/(sqrt(b*b + d/ps_sq)-b));
116
  }
117

    
118
#endif  // NUM_VERTEX>0
119

    
120
//////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
void main()
123
  {
124
  int component = int(a_Component);
125
  vec3 n = a_Normal;
126
#ifdef COMP_CENTERS
127
  vec3 v = a_Position + u_Inflate*(a_Position - vComCenter[component].xyz);
128
#else
129
  vec3 v = a_Position + u_Inflate*a_Position;
130
#endif
131

    
132
#if NUM_VERTEX>0
133
  int effect=0;
134

    
135
  for(int i=0; i<vNumEffects; i++)
136
    {
137
    if( ((vComAssoc[component].x & vProperties[i].y) != 0) || (vComAssoc[component].y == vProperties[i].w) )
138
      {
139
      // ENABLED EFFECTS WILL BE INSERTED HERE
140

    
141
      }
142
    effect+=3;
143
    }
144
#endif
145

    
146
#ifdef PREAPPLY
147
  v_Position   = v;
148
  v_endPosition= n;
149
#else
150
  if( u_TransformFeedback == 1 )
151
    {
152
    vec4 tmp1 =  u_MVMatrixP * vec4(v,1.0);
153
    vec4 tmp2 =  normalize(u_MVMatrixV * vec4(n,0.0));
154

    
155
    v_Position    = vec3(tmp1);
156
    v_endPosition = vec3(tmp1+100.0*tmp2);
157
    }
158
  else
159
    {
160
    v_Position = v;
161
    }
162
#endif
163

    
164
  v_TexCoordinate = a_TexCoordinate;
165
  v_Normal        = normalize(vec3(u_MVMatrixV*vec4(n,0.0)));
166
  gl_Position     = u_MVPMatrix*vec4(v,1.0);
167
  }                               
(6-6/14)