Project

General

Profile

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

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

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.DistortedEffects;
25
import org.distorted.library.DistortedTexture;
26
import org.distorted.library.type.Dynamic;
27
import org.distorted.library.type.Dynamic3D;
28
import org.distorted.library.type.Dynamic5D;
29
import org.distorted.library.type.Static3D;
30
import org.distorted.library.type.Static4D;
31
import org.distorted.library.type.Static5D;
32

    
33
class WindEffectsManager
34
  {
35
  private int mHeight, mWidth;
36

    
37
  private Static3D  shearFactor;
38
  private Dynamic3D shearDynamic;
39
  private Static3D  scaleFactor;
40
  private Dynamic3D scaleDynamic;
41
  private Static3D  deformForce;
42
  private Static5D  windFactor11, windFactor12;
43
  private Dynamic5D windDynamic1;
44
  private Static5D  windFactor21, windFactor22;
45
  private Dynamic5D windDynamic2;
46
  private Static5D  windFactor31, windFactor32;
47
  private Dynamic5D windDynamic3;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  WindEffectsManager(DistortedTexture texture)
52
    {
53
    mHeight = texture.getHeight();
54
    mWidth  = texture.getWidth();
55

    
56
    shearFactor = new Static3D(0,0,0);
57
    shearDynamic= new Dynamic3D();
58
    shearDynamic.add(shearFactor);
59

    
60
    scaleFactor = new Static3D(1,1,1);
61
    scaleDynamic= new Dynamic3D();
62
    scaleDynamic.add(scaleFactor);
63

    
64
    deformForce = new Static3D(mWidth/3,0,0);
65

    
66
    windFactor11 = new Static5D(mHeight/10,mHeight/5, 180, 0, 90);
67
    windFactor12 = new Static5D(mHeight/10,mHeight/5,-180, 0, 90);
68
    windDynamic1 = new Dynamic5D(1000,0.0f);
69
    windDynamic1.add(windFactor11);
70
    windDynamic1.add(windFactor12);
71
    windDynamic1.setMode(Dynamic.MODE_JUMP);
72
    windDynamic1.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
73

    
74
    windFactor21 = new Static5D(mHeight/10,mHeight/5,-180, 90, 10);
75
    windFactor22 = new Static5D(mHeight/10,mHeight/5,+180, 90, 10);
76
    windDynamic2 = new Dynamic5D(1000,0.0f);
77
    windDynamic2.add(windFactor21);
78
    windDynamic2.add(windFactor22);
79
    windDynamic2.setMode(Dynamic.MODE_JUMP);
80
    windDynamic2.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
81

    
82
    windFactor31 = new Static5D(mHeight/10,mHeight/10,-180, 90, 90);
83
    windFactor32 = new Static5D(mHeight/10,mHeight/10,+180, 90, 90);
84
    windDynamic3 = new Dynamic5D(1000,0.0f);
85
    windDynamic3.add(windFactor31);
86
    windDynamic3.add(windFactor32);
87
    windDynamic3.setMode(Dynamic.MODE_JUMP);
88
    windDynamic3.setAccessMode(Dynamic.ACCESS_SEQUENTIAL);
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
  synchronized void apply(DistortedEffects effects, int wind)
94
    {
95
    Static3D midLeft = new Static3D(0,mHeight/2,0);
96
    Static3D midRight = new Static3D(mWidth,mHeight/2,0);
97
    Static4D windRegion = new Static4D(0,0,mWidth,mHeight);
98

    
99
    setWind(wind);
100

    
101
    effects.shear(shearDynamic,midLeft);
102
    effects.scale(scaleDynamic);
103
    effects.deform(deformForce,midRight);
104
    effects.wave(windDynamic1, midRight, windRegion);
105
    effects.wave(windDynamic2, midRight, windRegion);
106
    effects.wave(windDynamic3, midRight, windRegion);
107
    }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  synchronized void setWind(int wind)
112
    {
113
    float tanAngle = (wind-50)/50.0f;
114

    
115
    shearFactor.set2(tanAngle);
116
    scaleFactor.set1(1/(float)Math.sqrt(1+tanAngle*tanAngle));
117
    windDynamic1.setDuration( wind > 0 ? 100000/wind : Long.MAX_VALUE);
118
    windDynamic2.setDuration( wind > 0 ?  80000/wind : Long.MAX_VALUE);
119
    windDynamic3.setDuration( wind > 0 ? 100000/wind : Long.MAX_VALUE);
120

    
121
    float waveA = (mHeight/(20.0f-0.15f*wind));
122
    windFactor21.set1(waveA);
123
    windFactor22.set1(waveA);
124

    
125
    float waveB = (mHeight/(wind+5.0f));
126
    windFactor31.set1(waveB);
127
    windFactor32.set1(waveB);
128
    }
129
  }
(2-2/4)