Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectWave.java @ da9b3f07

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 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
package org.distorted.library.effect;
21

    
22
import org.distorted.library.type.Data3D;
23
import org.distorted.library.type.Data4D;
24
import org.distorted.library.type.Data5D;
25
import org.distorted.library.type.Dynamic3D;
26
import org.distorted.library.type.Dynamic4D;
27
import org.distorted.library.type.Dynamic5D;
28
import org.distorted.library.type.Static3D;
29
import org.distorted.library.type.Static4D;
30
import org.distorted.library.type.Static5D;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
public class VertexEffectWave extends VertexEffect
35
  {
36
  private static final float[] UNITIES         = new float[] {0.0f};
37
  private static final int     DIMENSION       = 5;
38
  private static final boolean SUPPORTS_CENTER = true;
39
  private static final boolean SUPPORTS_REGION = true;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42
/**
43
 * Directional, sinusoidal wave effect.
44
 *
45
 * @param wave   A 5-dimensional data structure describing the wave: first member is the amplitude,
46
 *               second is the wave length, third is the phase (i.e. when phase = PI/2, the sine
47
 *               wave at the center does not start from sin(0), but from sin(PI/2) ) and the next two
48
 *               describe the 'direction' of the wave.
49
 *               <p>
50
 *               Wave direction is defined to be a 3D vector of length 1. To define such vectors, we
51
 *               need 2 floats: thus the third member is the angle Alpha (in degrees) which the vector
52
 *               forms with the XY-plane, and the fourth is the angle Beta (again in degrees) which
53
 *               the projection of the vector to the XY-plane forms with the Y-axis (counterclockwise).
54
 *               <p>
55
 *               <p>
56
 *               Example1: if Alpha = 90, Beta = 90, (then V=(0,0,1) ) and the wave acts 'vertically'
57
 *               in the X-direction, i.e. cross-sections of the resulting surface with the XZ-plane
58
 *               will be sine shapes.
59
 *               <p>
60
 *               Example2: if Alpha = 90, Beta = 0, the again V=(0,0,1) and the wave is 'vertical',
61
 *               but this time it waves in the Y-direction, i.e. cross sections of the surface and the
62
 *               YZ-plane with be sine shapes.
63
 *               <p>
64
 *               Example3: if Alpha = 0 and Beta = 45, then V=(sqrt(2)/2, -sqrt(2)/2, 0) and the wave
65
 *               is entirely 'horizontal' and moves point (x,y,0) in direction V by whatever is the
66
 *               value if sin at this point.
67
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
68
 */
69
  public VertexEffectWave(Data5D wave, Data3D center)
70
    {
71
    super(EffectName.WAVE,DIMENSION,SUPPORTS_CENTER,SUPPORTS_REGION,UNITIES);
72

    
73
    if( wave instanceof Dynamic5D)
74
      {
75
      mDynamic0 = (Dynamic5D)wave;
76
      }
77
    else if ( wave instanceof Static5D)
78
      {
79
      mStatic0  = (Static5D)wave;
80
      }
81

    
82
    if( center instanceof Static3D)
83
      {
84
      mStaticCenter = (Static3D)center;
85
      }
86
    else if( center instanceof Dynamic3D )
87
      {
88
      mDynamicCenter = (Dynamic3D)center;
89
      }
90

    
91
    mStaticRegion = new Static4D(0,0,Float.MAX_VALUE, Float.MAX_VALUE);
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
/**
96
 * Directional, sinusoidal wave effect.
97
 *
98
 * @param wave   see {@link VertexEffectWave(Data5D,Data3D)}
99
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
100
 * @param region Region that masks the Effect.
101
 */
102
  public VertexEffectWave(Data5D wave, Data3D center, Data4D region)
103
    {
104
    super(EffectName.WAVE,DIMENSION,SUPPORTS_CENTER,SUPPORTS_REGION,UNITIES);
105

    
106
    if( wave instanceof Dynamic5D)
107
      {
108
      mDynamic0 = (Dynamic5D)wave;
109
      }
110
    else if ( wave instanceof Static5D)
111
      {
112
      mStatic0  = (Static5D)wave;
113
      }
114

    
115
    if( center instanceof Static3D)
116
      {
117
      mStaticCenter = (Static3D)center;
118
      }
119
    else if( center instanceof Dynamic3D)
120
      {
121
      mDynamicCenter = (Dynamic3D)center;
122
      }
123

    
124
    if( region instanceof Static4D)
125
      {
126
      mStaticRegion = (Static4D)region;
127
      }
128
    else if( region instanceof Dynamic4D)
129
      {
130
      mDynamicRegion = (Dynamic4D)region;
131
      }
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
137
    {
138
    boolean ret = false;
139

    
140
    if( mDynamicCenter!=null )
141
      {
142
      mDynamicCenter.interpolateMain(uniforms,index+5,currentDuration,step);
143
      }
144
    else
145
      {
146
      uniforms[index+5] = mStaticCenter.getX();
147
      uniforms[index+6] = mStaticCenter.getY();
148
      uniforms[index+7] = mStaticCenter.getZ();
149
      }
150

    
151
    if( mDynamicRegion!=null )
152
      {
153
      mDynamicRegion.interpolateMain(uniforms,index+8,currentDuration,step);
154
      }
155
    else
156
      {
157
      uniforms[index+ 8] = mStaticRegion.getX();
158
      uniforms[index+ 9] = mStaticRegion.getY();
159
      uniforms[index+10] = mStaticRegion.getZ();
160
      uniforms[index+11] = mStaticRegion.getW();
161
      }
162

    
163
    if( mDynamic0!=null )
164
      {
165
      ret = mDynamic0.interpolateMain(uniforms,index,currentDuration,step);
166
      }
167
    else
168
      {
169
      uniforms[index  ] = ((Static5D)mStatic0).getX();
170
      uniforms[index+1] = ((Static5D)mStatic0).getY();
171
      uniforms[index+2] = ((Static5D)mStatic0).getZ();
172
      uniforms[index+3] = ((Static5D)mStatic0).getW();
173
      uniforms[index+4] = ((Static5D)mStatic0).getV();
174
      }
175

    
176
    uniforms[index+2] = (float)(Math.PI*uniforms[index+2]/180);
177
    uniforms[index+3] = (float)(Math.PI*uniforms[index+3]/180);
178
    uniforms[index+4] = (float)(Math.PI*uniforms[index+4]/180);
179

    
180
    return ret;
181
    }
182
  }
(25-25/25)