Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffectWave.java @ 6bb59aad

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 String NAME = "WAVE";
37
  private static final float[] UNITIES = new float[] {0.0f};
38
  private static final int DIMENSION = 5;
39
  private static final boolean SUPPORTS_CENTER = true;
40
  private static final boolean SUPPORTS_REGION = true;
41

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

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

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

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

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

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

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

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

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

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

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

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

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

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

    
181
    return ret;
182
    }
183
  }
(23-23/23)