1
|
|
2
|
package org.distorted.examples.olimpic;
|
3
|
|
4
|
import java.io.IOException;
|
5
|
import java.io.InputStream;
|
6
|
|
7
|
import javax.microedition.khronos.egl.EGLConfig;
|
8
|
import javax.microedition.khronos.opengles.GL10;
|
9
|
|
10
|
import org.distorted.examples.R;
|
11
|
|
12
|
import org.distorted.library.EffectTypes;
|
13
|
import org.distorted.library.Interpolator1D;
|
14
|
import org.distorted.library.DistortedNode;
|
15
|
import org.distorted.library.Float3D;
|
16
|
import org.distorted.library.Float1D;
|
17
|
import org.distorted.library.Distorted;
|
18
|
import org.distorted.library.DistortedBitmap;
|
19
|
|
20
|
import android.graphics.Bitmap;
|
21
|
import android.graphics.BitmapFactory;
|
22
|
import android.opengl.GLES20;
|
23
|
import android.opengl.GLSurfaceView;
|
24
|
|
25
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26
|
|
27
|
class OlimpicRenderer implements GLSurfaceView.Renderer
|
28
|
{
|
29
|
private static final int LEAF_SIZE = 100;
|
30
|
private static final int NUM_CIRCLES = 5;
|
31
|
private static final int NUM_LEAVES = 8;
|
32
|
|
33
|
private GLSurfaceView mView;
|
34
|
private DistortedNode[] mCircleNode;
|
35
|
private DistortedNode mScreen;
|
36
|
private DistortedBitmap mLeaf;
|
37
|
private Interpolator1D mRot;
|
38
|
|
39
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
40
|
|
41
|
public OlimpicRenderer(GLSurfaceView v)
|
42
|
{
|
43
|
mView = v;
|
44
|
|
45
|
mLeaf = new DistortedBitmap(LEAF_SIZE,LEAF_SIZE,1);
|
46
|
|
47
|
mScreen = new DistortedNode(new DistortedBitmap(9*LEAF_SIZE,9*LEAF_SIZE,1));
|
48
|
|
49
|
mRot = new Interpolator1D();
|
50
|
mRot.setMode(Interpolator1D.MODE_JUMP);
|
51
|
mRot.setCount(0.0f);
|
52
|
mRot.setDuration(5000);
|
53
|
mRot.add(new Float1D( 0));
|
54
|
mRot.add(new Float1D(360));
|
55
|
|
56
|
mCircleNode = new DistortedNode[NUM_CIRCLES];
|
57
|
|
58
|
int[] colors = new int[] {0,0,1, 0,0,0, 1,0,0, 1,1,0, 0,1,0}; // blue, black, red, yellow, green
|
59
|
int[] positions = new int[] {0*LEAF_SIZE,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};
|
60
|
|
61
|
DistortedBitmap tmp;
|
62
|
Float3D center = new Float3D(3*LEAF_SIZE/2, 3*LEAF_SIZE/2, 0);
|
63
|
|
64
|
for(int i=0; i<NUM_CIRCLES; i++)
|
65
|
{
|
66
|
if( i==0 )
|
67
|
{
|
68
|
mCircleNode[i] = new DistortedNode(new DistortedBitmap(3*LEAF_SIZE,3*LEAF_SIZE,1));
|
69
|
|
70
|
for(int j=0; j<NUM_LEAVES; j++)
|
71
|
{
|
72
|
tmp = new DistortedBitmap(mLeaf, Distorted.CLONE_BITMAP);
|
73
|
mCircleNode[i].attach(tmp);
|
74
|
tmp.rotate(center, j*(360/NUM_LEAVES));
|
75
|
tmp.move(0,LEAF_SIZE,0);
|
76
|
}
|
77
|
}
|
78
|
else
|
79
|
{
|
80
|
mCircleNode[i] = new DistortedNode(mCircleNode[0], Distorted.CLONE_BITMAP|Distorted.CLONE_CHILDREN);
|
81
|
}
|
82
|
|
83
|
mScreen.attach(mCircleNode[i]);
|
84
|
tmp = (DistortedBitmap)mCircleNode[i].getObject();
|
85
|
tmp.move(positions[2*i], positions[2*i+1], 0);
|
86
|
tmp.rotate( center, mRot, 0.0f, 0.0f, 1.0f);
|
87
|
tmp.chroma(0.8f, new Float3D(colors[3*i],colors[3*i+1], colors[3*i+2]));
|
88
|
}
|
89
|
}
|
90
|
|
91
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92
|
|
93
|
public void onDrawFrame(GL10 glUnused)
|
94
|
{
|
95
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
96
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
97
|
|
98
|
mScreen.draw(System.currentTimeMillis());
|
99
|
}
|
100
|
|
101
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
102
|
|
103
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
104
|
{
|
105
|
DistortedBitmap bmp = (DistortedBitmap)mScreen.getObject();
|
106
|
int bmpWidth = bmp.getWidth();
|
107
|
int bmpHeight = bmp.getHeight();
|
108
|
|
109
|
bmp.abortEffects(EffectTypes.MATRIX);
|
110
|
|
111
|
if( bmpHeight/bmpWidth > height/width )
|
112
|
{
|
113
|
int w = (height*bmpWidth)/bmpHeight;
|
114
|
bmp.move((width-w)/2 ,0, 0);
|
115
|
bmp.scale((float)height/bmpHeight);
|
116
|
}
|
117
|
else
|
118
|
{
|
119
|
int h = (width*bmpHeight)/bmpWidth;
|
120
|
bmp.move(0 ,(height-h)/2, 0);
|
121
|
bmp.scale((float)width/bmpWidth);
|
122
|
}
|
123
|
|
124
|
Distorted.onSurfaceChanged(width, height);
|
125
|
}
|
126
|
|
127
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
128
|
|
129
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
130
|
{
|
131
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.leaf);
|
132
|
Bitmap leaf;
|
133
|
|
134
|
try
|
135
|
{
|
136
|
leaf = BitmapFactory.decodeStream(is);
|
137
|
}
|
138
|
finally
|
139
|
{
|
140
|
try
|
141
|
{
|
142
|
is.close();
|
143
|
}
|
144
|
catch(IOException e) { }
|
145
|
}
|
146
|
|
147
|
mLeaf.setBitmap(leaf);
|
148
|
|
149
|
try
|
150
|
{
|
151
|
Distorted.onSurfaceCreated(mView.getContext());
|
152
|
}
|
153
|
catch(Exception ex)
|
154
|
{
|
155
|
android.util.Log.e("Olympic", ex.getMessage() );
|
156
|
}
|
157
|
}
|
158
|
|
159
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
160
|
|
161
|
}
|