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.olimpic;
|
21
|
|
22
|
import java.io.IOException;
|
23
|
import java.io.InputStream;
|
24
|
|
25
|
import javax.microedition.khronos.egl.EGLConfig;
|
26
|
import javax.microedition.khronos.opengles.GL10;
|
27
|
|
28
|
import org.distorted.examples.R;
|
29
|
|
30
|
import org.distorted.library.effect.FragmentEffectChroma;
|
31
|
import org.distorted.library.effect.MatrixEffectMove;
|
32
|
import org.distorted.library.effect.MatrixEffectRotate;
|
33
|
import org.distorted.library.effect.MatrixEffectScale;
|
34
|
import org.distorted.library.main.DistortedEffects;
|
35
|
import org.distorted.library.main.DistortedScreen;
|
36
|
import org.distorted.library.main.DistortedNode;
|
37
|
import org.distorted.library.mesh.MeshFlat;
|
38
|
import org.distorted.library.type.Dynamic1D;
|
39
|
import org.distorted.library.type.Static3D;
|
40
|
import org.distorted.library.type.Static1D;
|
41
|
import org.distorted.library.main.Distorted;
|
42
|
import org.distorted.library.main.DistortedTexture;
|
43
|
|
44
|
import android.graphics.Bitmap;
|
45
|
import android.graphics.BitmapFactory;
|
46
|
import android.opengl.GLSurfaceView;
|
47
|
|
48
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49
|
|
50
|
class OlimpicRenderer implements GLSurfaceView.Renderer
|
51
|
{
|
52
|
private static final int LEAF_SIZE = 100;
|
53
|
private static final int NUM_CIRCLES = 5;
|
54
|
static final int NUM_LEAVES = 8;
|
55
|
|
56
|
private GLSurfaceView mView;
|
57
|
private DistortedNode mRoot;
|
58
|
private DistortedTexture mLeaf;
|
59
|
private DistortedScreen mScreen;
|
60
|
private MeshFlat mMesh;
|
61
|
private int mScreenW, mScreenH;
|
62
|
private int mPrevRendered, mCurrRendered;
|
63
|
private Static3D mMove, mScale;
|
64
|
|
65
|
private DistortedNode[] mCircleNode = new DistortedNode[NUM_CIRCLES];
|
66
|
private DistortedEffects[] mEffects = new DistortedEffects[NUM_LEAVES];
|
67
|
|
68
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69
|
|
70
|
void pressed(int color, int number, boolean checked)
|
71
|
{
|
72
|
if( color>=0 && color<NUM_CIRCLES )
|
73
|
{
|
74
|
if( number==0 )
|
75
|
{
|
76
|
if( checked ) mRoot.attach(mCircleNode[color]);
|
77
|
else mRoot.detach(mCircleNode[color]);
|
78
|
}
|
79
|
else if( number>0 && number<=NUM_LEAVES )
|
80
|
{
|
81
|
if( checked ) mCircleNode[color].attach( mLeaf, mEffects[number-1], mMesh);
|
82
|
else mCircleNode[color].detach(mEffects[number-1]);
|
83
|
}
|
84
|
}
|
85
|
}
|
86
|
|
87
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
88
|
|
89
|
OlimpicRenderer(GLSurfaceView v)
|
90
|
{
|
91
|
mView = v;
|
92
|
|
93
|
mPrevRendered = -1;
|
94
|
mCurrRendered = -1;
|
95
|
|
96
|
mLeaf = new DistortedTexture(LEAF_SIZE,LEAF_SIZE);
|
97
|
DistortedTexture surface = new DistortedTexture(3*LEAF_SIZE,3*LEAF_SIZE);
|
98
|
mMesh = new MeshFlat(1,1);
|
99
|
mMove = new Static3D(0,0,0);
|
100
|
mScale= new Static3D(1,1,1);
|
101
|
|
102
|
DistortedEffects effects = new DistortedEffects();
|
103
|
effects.apply(new MatrixEffectMove(mMove));
|
104
|
effects.apply(new MatrixEffectScale(mScale));
|
105
|
|
106
|
mScreenW = 9*LEAF_SIZE;
|
107
|
mScreenH = 9*LEAF_SIZE;
|
108
|
mRoot = new DistortedNode(new DistortedTexture(mScreenW,mScreenH), effects, mMesh);
|
109
|
|
110
|
Dynamic1D rot = new Dynamic1D(5000,0.0f);
|
111
|
rot.setMode(Dynamic1D.MODE_JUMP);
|
112
|
rot.add(new Static1D( 0));
|
113
|
rot.add(new Static1D(360));
|
114
|
|
115
|
int[] colors = new int[] {0,0,1, 0,0,0, 1,0,0, 1,1,0, 0,1,0}; // blue, black, red, yellow, green
|
116
|
int[] positions = new int[] {0,2*LEAF_SIZE, 3*LEAF_SIZE,2*LEAF_SIZE, 6*LEAF_SIZE,2*LEAF_SIZE, 3*LEAF_SIZE/2,9*LEAF_SIZE/2, 9*LEAF_SIZE/2,9*LEAF_SIZE/2};
|
117
|
|
118
|
Static3D center = new Static3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
|
119
|
Static3D axis = new Static3D(0,0,1);
|
120
|
Static3D moveVector = new Static3D(0,LEAF_SIZE,0);
|
121
|
|
122
|
for(int j=0; j<NUM_LEAVES; j++)
|
123
|
{
|
124
|
mEffects[j] = new DistortedEffects();
|
125
|
mEffects[j].apply( new MatrixEffectRotate(new Static1D(j*(360/NUM_LEAVES)), axis, center) );
|
126
|
mEffects[j].apply(new MatrixEffectMove(moveVector));
|
127
|
}
|
128
|
|
129
|
for(int i=0; i<NUM_CIRCLES; i++)
|
130
|
{
|
131
|
effects = new DistortedEffects();
|
132
|
effects.apply( new MatrixEffectMove(new Static3D(positions[2*i], positions[2*i+1], 0)) );
|
133
|
effects.apply( new MatrixEffectRotate(rot, axis, center) );
|
134
|
effects.apply( new FragmentEffectChroma(new Static1D(0.5f), new Static3D(colors[3*i],colors[3*i+1], colors[3*i+2])) );
|
135
|
|
136
|
mCircleNode[i] = new DistortedNode( surface, effects, mMesh);
|
137
|
mRoot.attach(mCircleNode[i]);
|
138
|
|
139
|
for(int j=0; j<NUM_LEAVES; j++) mCircleNode[i].attach(mLeaf, mEffects[j], mMesh);
|
140
|
}
|
141
|
|
142
|
mScreen = new DistortedScreen();
|
143
|
mScreen.attach(mRoot);
|
144
|
}
|
145
|
|
146
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
147
|
|
148
|
public void onDrawFrame(GL10 glUnused)
|
149
|
{
|
150
|
mCurrRendered = mScreen.render(System.currentTimeMillis());
|
151
|
|
152
|
if( mCurrRendered!=mPrevRendered )
|
153
|
{
|
154
|
mPrevRendered = mCurrRendered;
|
155
|
|
156
|
final OlimpicActivity act = (OlimpicActivity)mView.getContext();
|
157
|
|
158
|
act.runOnUiThread(new Runnable()
|
159
|
{
|
160
|
public void run()
|
161
|
{
|
162
|
act.setText("rendered: "+mCurrRendered+" objects");
|
163
|
}
|
164
|
});
|
165
|
}
|
166
|
}
|
167
|
|
168
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
169
|
|
170
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
171
|
{
|
172
|
if( (float)mScreenH/mScreenW > (float)height/width )
|
173
|
{
|
174
|
int w = (height*mScreenW)/mScreenH;
|
175
|
float factor = (float)height/mScreenH;
|
176
|
mMove.set((width-w)/2 ,0, 0);
|
177
|
mScale.set(factor,factor,factor);
|
178
|
}
|
179
|
else
|
180
|
{
|
181
|
int h = (width*mScreenH)/mScreenW;
|
182
|
float factor = (float)width/mScreenW;
|
183
|
mMove.set(0,(height-h)/2,0);
|
184
|
mScale.set(factor,factor,factor);
|
185
|
}
|
186
|
|
187
|
mScreen.resize(width, height);
|
188
|
}
|
189
|
|
190
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
191
|
|
192
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
193
|
{
|
194
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
|
195
|
Bitmap leaf;
|
196
|
|
197
|
try
|
198
|
{
|
199
|
leaf = BitmapFactory.decodeStream(is);
|
200
|
}
|
201
|
finally
|
202
|
{
|
203
|
try
|
204
|
{
|
205
|
is.close();
|
206
|
}
|
207
|
catch(IOException e) { }
|
208
|
}
|
209
|
|
210
|
mLeaf.setTexture(leaf);
|
211
|
|
212
|
FragmentEffectChroma.enable();
|
213
|
|
214
|
try
|
215
|
{
|
216
|
Distorted.onCreate(mView.getContext());
|
217
|
}
|
218
|
catch(Exception ex)
|
219
|
{
|
220
|
android.util.Log.e("Olympic", ex.getMessage() );
|
221
|
}
|
222
|
}
|
223
|
}
|