Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindEffectsManager.java @ e50f49b6

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
package org.distorted.examples.wind;
21

    
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23

    
24
import org.distorted.library.effect.MatrixEffectScale;
25
import org.distorted.library.effect.MatrixEffectShear;
26
import org.distorted.library.effect.VertexEffectDeform;
27
import org.distorted.library.effect.VertexEffectWave;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.type.Dynamic;
30
import org.distorted.library.type.Dynamic5D;
31
import org.distorted.library.type.Static3D;
32
import org.distorted.library.type.Static4D;
33
import org.distorted.library.type.Static5D;
34

    
35
class WindEffectsManager
36
  {
37
  private static final long TIME_TO_GET_UP    = 400; // time it takes for the max wind to
38
                                                     // make the flag horizontal (milliseconds)
39
  private static final long TIME_TO_FALL_DOWN =2000; // time it takes for the flag to
40
                                                     // fall back down if no wind (milliseconds)
41
  private long lastTime;
42
  private float mWind;
43
  private int mHeight, mWidth;
44

    
45
  private Static3D  shearFactor;
46
  private Static3D  scaleFactor;
47
  private Static3D  deformForce;
48
  private Static5D  windFactor11, windFactor12;
49
  private Dynamic5D windDynamic1;
50
  private Static5D  windFactor21, windFactor22;
51
  private Dynamic5D windDynamic2;
52
  private Static5D  windFactor31, windFactor32;
53
  private Dynamic5D windDynamic3;
54
  private Static5D  windFactor41, windFactor42;
55
  private Dynamic5D windDynamic4;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  WindEffectsManager(int width, int height)
60
    {
61
    lastTime = 0;
62

    
63
    mWidth = width;
64
    mHeight= height;
65

    
66
    shearFactor = new Static3D(0,0,0);
67
    scaleFactor = new Static3D(1,1,1);
68
    deformForce = new Static3D(mWidth/3,0,0);
69

    
70
    windFactor11 = new Static5D(mHeight/10,mHeight/5, 180, 0, 90);
71
    windFactor12 = new Static5D(mHeight/10,mHeight/5,-180, 0, 90);
72
    windDynamic1 = new Dynamic5D(1000,0.0f);
73
    windDynamic1.add(windFactor11);
74
    windDynamic1.add(windFactor12);
75
    windDynamic1.setMode(Dynamic.MODE_JUMP);
76
    windDynamic1.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
77

    
78
    windFactor21 = new Static5D(mHeight/10,mHeight/5,-180, 90, 10);
79
    windFactor22 = new Static5D(mHeight/10,mHeight/5,+180, 90, 10);
80
    windDynamic2 = new Dynamic5D(1000,0.0f);
81
    windDynamic2.add(windFactor21);
82
    windDynamic2.add(windFactor22);
83
    windDynamic2.setMode(Dynamic.MODE_JUMP);
84
    windDynamic2.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
85

    
86
    windFactor31 = new Static5D(mHeight/10,mHeight/10,-180, 90, 90);
87
    windFactor32 = new Static5D(mHeight/10,mHeight/10,+180, 90, 90);
88
    windDynamic3 = new Dynamic5D(1000,0.0f);
89
    windDynamic3.add(windFactor31);
90
    windDynamic3.add(windFactor32);
91
    windDynamic3.setMode(Dynamic.MODE_JUMP);
92
    windDynamic3.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
93

    
94
    windFactor41 = new Static5D(mHeight/10,mHeight/5,-180,  0,  0);
95
    windFactor42 = new Static5D(mHeight/10,mHeight/5,+180,  0,  0);
96
    windDynamic4 = new Dynamic5D(1000,0.0f);
97
    windDynamic4.add(windFactor41);
98
    windDynamic4.add(windFactor42);
99
    windDynamic4.setMode(Dynamic.MODE_JUMP);
100
    windDynamic4.setAccessType(Dynamic.ACCESS_TYPE_SEQUENTIAL);
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  void apply(DistortedEffects effects)
106
    {
107
    Static3D midLeft   = new Static3D(-mWidth/2,0,0);
108
    Static3D midRight  = new Static3D( mWidth/2,0,0);
109
    Static3D tadRight  = new Static3D( mWidth/4,0,0);
110
    Static4D windRegion= new Static4D(0,0,0,mHeight);
111

    
112
    setWind(0);
113

    
114
    effects.apply( new MatrixEffectScale(scaleFactor) );
115
    effects.apply( new MatrixEffectShear(shearFactor,midLeft) );
116

    
117
    effects.apply( new VertexEffectDeform(deformForce,midRight) );
118
    effects.apply( new VertexEffectWave(windDynamic1, midRight, windRegion) );
119
    effects.apply( new VertexEffectWave(windDynamic2, midRight, windRegion) );
120
    effects.apply( new VertexEffectWave(windDynamic3, midRight, windRegion) );
121
    effects.apply( new VertexEffectWave(windDynamic4, tadRight, windRegion) );
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  private void setWind(int wind)
127
    {
128
    mWind = wind;
129

    
130
    float tanAngle = (wind-50)/50.0f;
131
    shearFactor.set1(tanAngle);
132
    scaleFactor.set0(1/(float)Math.sqrt(1+tanAngle*tanAngle));
133

    
134
    windDynamic1.setDuration( wind > 0 ? 900 + 10000/wind : Long.MAX_VALUE);
135
    windDynamic2.setDuration( wind > 0 ? 720 +  8000/wind : Long.MAX_VALUE);
136
    windDynamic3.setDuration( wind > 0 ? 900 + 10000/wind : Long.MAX_VALUE);
137

    
138
    float wave2 = mHeight*( 0.05f + 0.002f*wind);
139
    windFactor21.set0(wave2);
140
    windFactor22.set0(wave2);
141

    
142
    float wave3 = (mHeight/(wind+5.0f));
143
    windFactor31.set0(wave3);
144
    windFactor32.set0(wave3);
145

    
146
    float wave4 = (mHeight*(wind*(100-wind)/50000.0f));
147
    windFactor41.set0(wave4);
148
    windFactor42.set0(wave4);
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  synchronized void increaseWind(long currTime)
154
    {
155
    if( lastTime>0 )
156
      {
157
      long diff = currTime-lastTime;
158
      float diffWind = (100.0f*diff/TIME_TO_GET_UP);
159

    
160
      mWind += diffWind;
161
      if( mWind>100.0f ) mWind=100.0f;
162

    
163
      setWind( (int)mWind );
164
      }
165

    
166
    lastTime = currTime;
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  synchronized void decreaseWind(long currTime)
172
    {
173
    if( lastTime>0 )
174
      {
175
      long diff = currTime-lastTime;
176
      float diffWind = (100.0f*diff/TIME_TO_FALL_DOWN);
177

    
178
      mWind -= diffWind;
179
      if( mWind<0.0f ) mWind=0.0f;
180

    
181
      setWind( (int)mWind );
182
      }
183

    
184
    lastTime = currTime;
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
  }
(2-2/5)