Project

General

Profile

« Previous | Next » 

Revision 1e667536

Added by Leszek Koltunski over 4 years ago

1) Cube: convert it to the latest library. Main difference: objects are rendered better, individual cubits have rounded corners.
2) Examples: some adjustments to MeshJoin & Predeform
3) Library: fix a bug in main_vertex_shader's 'degree' function, which didn't work proprely in case of a vertex which was exactly at the center (i.e. vector PS was zero)

View differences:

src/main/java/org/distorted/library/effect/VertexEffectDistort.java
131 131
      + "vec4 region= vUniforms[effect+2];                             \n"
132 132
      + "float d = degree(region,ps);                                  \n"
133 133

  
134
      + "if( d>0.0 )                                                   \n"
135
      + "  {                                                           \n"
136
      + "  v += d*force;                                               \n"
134
      + "v += d*force;                                                 \n"
137 135

  
138
      + "  float tp = 1.0+n.z;                                         \n"
139
      + "  float tr = 1.0 / (tp - (1.0 - sign(tp)));                   \n"
136
      + "float tp = 1.0+n.z;                                           \n"
137
      + "float tr = 1.0 / (tp - (sign(tp)-1.0));                       \n"   // proper way to compute 1/x is
138
                                                                             // 1/(x-(sign(x)-1)) and NOT 1/(x+1-sign(x))
140 139

  
141
      + "  float ap = ps.x*n.z - ps.z*n.x;                             \n"   // likewise rotate the ps vector
142
      + "  float bp = ps.y*n.z - ps.z*n.y;                             \n"   //
143
      + "  float cp =(ps.x*n.y - ps.y*n.x)*tr;                         \n"   //
144
      + "  vec3 psRot = vec3( ap+n.y*cp , bp-n.x*cp , dot(ps,n) );     \n"   //
140
      + "float ap = ps.x*n.z - ps.z*n.x;                               \n"   // rotate the ps vector
141
      + "float bp = ps.y*n.z - ps.z*n.y;                               \n"   //
142
      + "float cp =(ps.x*n.y - ps.y*n.x)*tr;                           \n"   //
143
      + "vec3 psRot = vec3( ap+n.y*cp , bp-n.x*cp , dot(ps,n) );       \n"   //
145 144

  
146
      + "  float len = length(psRot);                                  \n"
147
      + "  float corr= sign(len)-1.0;                                  \n"   // make N (0,0,1) if ps=(0,0,0)
148
      + "  vec3 N = vec3( -dot(force,n)*psRot.xy, region.w*len-corr ); \n"   // modified rotated normal
145
      + "float len = length(psRot);                                    \n"
146
      + "float corr= sign(len)-1.0;                                    \n"   // make N (0,0,1) if ps=(0,0,0)
147
      + "vec3 N = vec3( -dot(force,n)*psRot.xy, region.w*len-corr );   \n"   // modified rotated normal
149 148
                                                                             // dot(force,n) is rotated force.z
150
      + "  float an = N.x*n.z + N.z*n.x;                               \n"   // now create the normal vector
151
      + "  float bn = N.y*n.z + N.z*n.y;                               \n"   // back from our modified normal
152
      + "  float cn =(N.x*n.y - N.y*n.x)*tr;                           \n"   // rotated back
153
      + "  n = vec3( an+n.y*cn , bn-n.x*cn , -N.x*n.x-N.y*n.y+N.z*n.z);\n"   // notice 4 signs change!
149
      + "float an = N.x*n.z + N.z*n.x;                                 \n"   // now create the normal vector
150
      + "float bn = N.y*n.z + N.z*n.y;                                 \n"   // back from our modified normal
151
      + "float cn =(N.x*n.y - N.y*n.x)*tr;                             \n"   // rotated back
152
      + "n = vec3( an+n.y*cn , bn-n.x*cn , -N.x*n.x-N.y*n.y+N.z*n.z);  \n"   // notice 4 signs change!
153

  
154
      + "  n = normalize(n);                                           \n";
154 155

  
155
      + "  n = normalize(n);                                           \n"
156
      + "  }                                                           \n";
157 156
    }
158 157

  
159 158
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/res/raw/main_vertex_shader.glsl
75 75

  
76 76
float degree(in vec4 region, in vec3 PS)
77 77
  {
78
  vec3 PO  = PS + region.xyz;
79
  float D = region.w*region.w-dot(PO,PO);      // D = |OX|^2 - |PO|^2
78
  float ps_sq = dot(PS,PS);
80 79

  
81
  if( D<=0.0 ) return 0.0;
80
  if( ps_sq==0.0 ) return 1.0;
82 81

  
83
  float ps_sq = dot(PS,PS);
84
  float one_over_ps_sq = 1.0/(ps_sq-(sign(ps_sq)-1.0));  // return 1.0 if ps_sq = 0.0
85
                                                         // Important: if we want to write
86
                                                         // b = 1/a if a!=0, b=1 otherwise
87
                                                         // we need to write that as
88
                                                         // b = 1 / ( a-(sign(a)-1) )
89
                                                         // [ and NOT 1 / ( a + 1 - sign(a) ) ]
90
                                                         // because the latter, if 0<a<2^-24,
91
                                                         // will suffer from round-off error and in this case
92
                                                         // a + 1.0 = 1.0 !! so 1 / (a+1-sign(a)) = 1/0 !
93
  float DOT  = dot(PS,PO)*one_over_ps_sq;
94

  
95
  return 1.0 / (1.0 + 1.0/(sqrt(DOT*DOT+D*one_over_ps_sq)-DOT));
82
  vec3 PO = PS + region.xyz;
83
  float d = region.w*region.w-dot(PO,PO);
84

  
85
  if( d<=0.0 ) return 0.0;
86

  
87
  float b = dot(PS,PO)/ps_sq;
88

  
89
  return 1.0 / (1.0 + 1.0/(sqrt(b*b + d/ps_sq)-b));
96 90
  }
97 91

  
98 92
#endif  // NUM_VERTEX>0

Also available in: Unified diff