Project

General

Profile

« Previous | Next » 

Revision 318dd77b

Added by Leszek Koltunski over 5 years ago

Some improvements to the Wind app - still does not look very realistic though :(

View differences:

src/main/java/org/distorted/examples/wind/WindActivity.java
49 49
      windText = findViewById(R.id.windText);
50 50
     
51 51
      bar.setProgress(50);
52
      
53
      windText.setText(getString(R.string.wind_placeholder,50));
54 52
      }
55 53

  
56 54
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/examples/wind/WindEffectsManager.java
35 35

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

  
40 46
  private Static3D  shearFactor;
......
46 52
  private Dynamic5D windDynamic2;
47 53
  private Static5D  windFactor31, windFactor32;
48 54
  private Dynamic5D windDynamic3;
55
  private Static5D  windFactor41, windFactor42;
56
  private Dynamic5D windDynamic4;
49 57

  
50 58
///////////////////////////////////////////////////////////////////////////////////////////////////
51 59

  
52 60
  WindEffectsManager(DistortedTexture texture)
53 61
    {
62
    lastTime = 0;
63

  
54 64
    mHeight = texture.getHeight();
55 65
    mWidth  = texture.getWidth();
56 66

  
......
81 91
    windDynamic3.add(windFactor32);
82 92
    windDynamic3.setMode(Dynamic.MODE_JUMP);
83 93
    windDynamic3.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
94

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

  
86 104
///////////////////////////////////////////////////////////////////////////////////////////////////
87 105

  
88
  void apply(DistortedEffects effects, int wind)
106
  void apply(DistortedEffects effects)
89 107
    {
90
    Static3D midLeft = new Static3D(0,mHeight/2,0);
91
    Static3D midRight = new Static3D(mWidth,mHeight/2,0);
108
    Static3D midLeft    = new Static3D(0,mHeight/2,0);
109
    Static3D midRight   = new Static3D(mWidth,mHeight/2,0);
110
    Static3D tadRight   = new Static3D(3*mWidth/4,mHeight/2,0);
92 111
    Static4D windRegion = new Static4D(0,0,0,mHeight);
93 112

  
94
    setWind(wind);
113
    setWind(0);
95 114

  
96 115
    effects.apply( new MatrixEffectShear(shearFactor,midLeft) );
97 116
    effects.apply( new MatrixEffectScale(scaleFactor) );
......
99 118
    effects.apply( new VertexEffectWave(windDynamic1, midRight, windRegion) );
100 119
    effects.apply( new VertexEffectWave(windDynamic2, midRight, windRegion) );
101 120
    effects.apply( new VertexEffectWave(windDynamic3, midRight, windRegion) );
121
    effects.apply( new VertexEffectWave(windDynamic4, tadRight, windRegion) );
102 122
    }
103 123

  
104 124
///////////////////////////////////////////////////////////////////////////////////////////////////
105 125

  
106
  synchronized void setWind(int wind)
126
  private void setWind(int wind)
107 127
    {
108
    float tanAngle = (wind-50)/50.0f;
128
    mWind = wind;
109 129

  
130
    float tanAngle = (wind-50)/50.0f;
110 131
    shearFactor.set2(tanAngle);
111 132
    scaleFactor.set1(1/(float)Math.sqrt(1+tanAngle*tanAngle));
112
    windDynamic1.setDuration( wind > 0 ? 100000/wind : Long.MAX_VALUE);
113
    windDynamic2.setDuration( wind > 0 ?  80000/wind : Long.MAX_VALUE);
114
    windDynamic3.setDuration( wind > 0 ? 100000/wind : Long.MAX_VALUE);
115 133

  
116
    float waveA = (mHeight/(20.0f-0.15f*wind));
117
    windFactor21.set1(waveA);
118
    windFactor22.set1(waveA);
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.set1(wave2);
140
    windFactor22.set1(wave2);
141

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

  
146
    float wave4 = (mHeight*(wind*(100-wind)/50000.0f));
147
    windFactor41.set1(wave4);
148
    windFactor42.set1(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;
119 180

  
120
    float waveB = (mHeight/(wind+5.0f));
121
    windFactor31.set1(waveB);
122
    windFactor32.set1(waveB);
181
      setWind( (int)mWind );
182
      }
183

  
184
    lastTime = currTime;
123 185
    }
186

  
187
///////////////////////////////////////////////////////////////////////////////////////////////////
124 188
  }
src/main/java/org/distorted/examples/wind/WindGust.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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
import java.util.Random;
23

  
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

  
26
class WindGust
27
  {
28
  private static final long TIME_PERIOD = 1000;
29
  private int mAvgWind;
30
  private boolean mBlowingNow;
31
  private long mLastTime;
32
  private static Random mRnd = new Random();
33

  
34
  WindGust()
35
    {
36
    mAvgWind = 0;
37
    mBlowingNow = false;
38
    mLastTime = 0;
39
    }
40

  
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

  
43
  void setAverageWind(int wind)
44
    {
45
    mAvgWind = wind;
46
    }
47

  
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

  
50
  boolean isWindBlowingNow(long currTime)
51
    {
52
    if( mLastTime>0 )
53
      {
54
      int lastPeriod = (int)(mLastTime/TIME_PERIOD);
55
      int currPeriod = (int)( currTime/TIME_PERIOD);
56

  
57
      if( currPeriod>lastPeriod ) mBlowingNow=(mRnd.nextInt(100)<=mAvgWind);
58
      }
59

  
60
    mLastTime = currTime;
61

  
62
    return mBlowingNow;
63
    }
64
  }
src/main/java/org/distorted/examples/wind/WindRenderer.java
51 51
   private DistortedTexture mTexture;
52 52
   private DistortedScreen mScreen;
53 53
   private WindEffectsManager mManager;
54
   private WindGust mGust;
54 55
   private Static3D mMove, mScale;
55 56
   private int mObjWidth, mObjHeight;
56
   private int mWind;
57 57

  
58 58
///////////////////////////////////////////////////////////////////////////////////////////////////
59 59

  
......
63 63

  
64 64
      mTexture = new DistortedTexture(50,30);
65 65
      mManager = new WindEffectsManager(mTexture);
66
      mGust    = new WindGust();
66 67
      mScreen  = new DistortedScreen();
67 68

  
68 69
      DistortedEffects effects = new DistortedEffects();
......
82 83
      Static3D center= new Static3D(0,mObjHeight/2,0);
83 84

  
84 85
      effects.apply( new MatrixEffectRotate(angle, axis, center) );
85
      mManager.apply(effects,mWind);
86
      mManager.apply(effects);
86 87
      }
87 88

  
88 89
///////////////////////////////////////////////////////////////////////////////////////////////////
89 90

  
90 91
   void setWind(int wind)
91 92
      {
92
      mWind = wind;
93
      mManager.setWind(mWind);
93
      mGust.setAverageWind(wind);
94 94
      }
95 95
   
96 96
///////////////////////////////////////////////////////////////////////////////////////////////////
97 97
   
98 98
   public void onDrawFrame(GL10 glUnused) 
99 99
      {
100
      mScreen.render( System.currentTimeMillis() );
100
      long time = System.currentTimeMillis();
101

  
102
      if( mGust.isWindBlowingNow(time) ) mManager.increaseWind(time);
103
      else                               mManager.decreaseWind(time);
104

  
105
      mScreen.render(time);
101 106
      }
102 107

  
103 108
///////////////////////////////////////////////////////////////////////////////////////////////////
104 109
    
105 110
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
106 111
      {
107
      float factor = ( (float)(width<height? width:height) )/(mObjHeight + 1.4f*mObjWidth);
108
      mMove.set( factor*mObjHeight*0.58f , height - factor*mObjHeight*1.08f , 0 );
112
      int min = width<height? width:height;
113

  
114
      float factor = ((float)min)/(mObjHeight + 1.4f*mObjWidth);
115
      mMove.set( factor*mObjHeight*0.58f +(width-min)/2, height - factor*mObjHeight*1.08f -(height-min)/2, 0 );
109 116
      mScale.set(factor,factor,factor);
110 117
      mScreen.resize(width, height);
111 118
      }

Also available in: Unified diff