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
|
import android.graphics.Bitmap;
|
23
|
import android.graphics.BitmapFactory;
|
24
|
import android.opengl.GLSurfaceView;
|
25
|
|
26
|
import org.distorted.examples.R;
|
27
|
import org.distorted.library.effect.MatrixEffectMove;
|
28
|
import org.distorted.library.effect.MatrixEffectRotate;
|
29
|
import org.distorted.library.effect.MatrixEffectScale;
|
30
|
import org.distorted.library.effect.VertexEffectDeform;
|
31
|
import org.distorted.library.effect.VertexEffectWave;
|
32
|
import org.distorted.library.main.DistortedLibrary;
|
33
|
import org.distorted.library.main.DistortedScreen;
|
34
|
import org.distorted.library.mesh.MeshCubes;
|
35
|
import org.distorted.library.main.DistortedEffects;
|
36
|
import org.distorted.library.main.DistortedTexture;
|
37
|
import org.distorted.library.type.Static1D;
|
38
|
import org.distorted.library.type.Static3D;
|
39
|
|
40
|
import java.io.IOException;
|
41
|
import java.io.InputStream;
|
42
|
|
43
|
import javax.microedition.khronos.egl.EGLConfig;
|
44
|
import javax.microedition.khronos.opengles.GL10;
|
45
|
|
46
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47
|
|
48
|
class WindRenderer implements GLSurfaceView.Renderer
|
49
|
{
|
50
|
private GLSurfaceView mView;
|
51
|
private DistortedTexture mTexture;
|
52
|
private DistortedScreen mScreen;
|
53
|
private WindEffectsManager mManager;
|
54
|
private WindGust mGust;
|
55
|
private Static3D mMove, mScale;
|
56
|
private int mObjWidth, mObjHeight;
|
57
|
|
58
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59
|
|
60
|
WindRenderer(GLSurfaceView view)
|
61
|
{
|
62
|
mView = view;
|
63
|
|
64
|
mTexture = new DistortedTexture(50,30);
|
65
|
mManager = new WindEffectsManager(mTexture);
|
66
|
mGust = new WindGust();
|
67
|
mScreen = new DistortedScreen();
|
68
|
|
69
|
DistortedEffects effects = new DistortedEffects();
|
70
|
|
71
|
mScreen.attach(mTexture,effects,new MeshCubes(50,30,1));
|
72
|
|
73
|
mObjWidth = mTexture.getWidth();
|
74
|
mObjHeight= mTexture.getHeight();
|
75
|
|
76
|
mManager.apply(effects);
|
77
|
|
78
|
mMove = new Static3D(0,0,0);
|
79
|
mScale= new Static3D(1,1,1);
|
80
|
|
81
|
Static1D angle = new Static1D(-45);
|
82
|
Static3D axis = new Static3D(0,0,1);
|
83
|
Static3D center= new Static3D(0,mObjHeight/2,0);
|
84
|
|
85
|
effects.apply( new MatrixEffectRotate(angle, axis, center) );
|
86
|
effects.apply( new MatrixEffectScale(mScale));
|
87
|
effects.apply( new MatrixEffectMove(mMove));
|
88
|
}
|
89
|
|
90
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
91
|
|
92
|
void setWind(int wind)
|
93
|
{
|
94
|
mGust.setAverageWind(wind);
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
public void onDrawFrame(GL10 glUnused)
|
100
|
{
|
101
|
long time = System.currentTimeMillis();
|
102
|
|
103
|
if( mGust.isWindBlowingNow(time) ) mManager.increaseWind(time);
|
104
|
else mManager.decreaseWind(time);
|
105
|
|
106
|
mScreen.render(time);
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
|
111
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
112
|
{
|
113
|
int min = width<height? width:height;
|
114
|
|
115
|
float factor = ((float)min)/(mObjHeight + 1.4f*mObjWidth);
|
116
|
mMove.set( factor*mObjHeight*0.58f +(width-min)/2, height - factor*mObjHeight*1.08f -(height-min)/2, 0 );
|
117
|
mScale.set(factor,factor,factor);
|
118
|
mScreen.resize(width, height);
|
119
|
}
|
120
|
|
121
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122
|
|
123
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
124
|
{
|
125
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
|
126
|
Bitmap bitmap;
|
127
|
|
128
|
try
|
129
|
{
|
130
|
bitmap = BitmapFactory.decodeStream(is);
|
131
|
}
|
132
|
finally
|
133
|
{
|
134
|
try
|
135
|
{
|
136
|
is.close();
|
137
|
}
|
138
|
catch(IOException e) { }
|
139
|
}
|
140
|
|
141
|
mTexture.setTexture(bitmap);
|
142
|
|
143
|
VertexEffectDeform.enable();
|
144
|
VertexEffectWave.enable();
|
145
|
|
146
|
try
|
147
|
{
|
148
|
DistortedLibrary.onCreate(mView.getContext());
|
149
|
}
|
150
|
catch(Exception ex)
|
151
|
{
|
152
|
android.util.Log.e("Wind", ex.getMessage() );
|
153
|
}
|
154
|
}
|
155
|
}
|