1 |
bc0a685b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
f26ab2fd
|
Leszek Koltunski
|
package org.distorted.examples.catanddog;
|
21 |
427ab7bf
|
Leszek Koltunski
|
|
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 |
5068fa06
|
Leszek Koltunski
|
import org.distorted.examples.R;
|
29 |
|
|
import org.distorted.library.Distorted;
|
30 |
d04a4886
|
Leszek Koltunski
|
import org.distorted.library.DistortedEffects;
|
31 |
d218d64e
|
leszek
|
import org.distorted.library.DistortedScreen;
|
32 |
40eef4b9
|
Leszek Koltunski
|
import org.distorted.library.DistortedTexture;
|
33 |
6637d0f2
|
Leszek Koltunski
|
import org.distorted.library.EffectNames;
|
34 |
b01acdaf
|
Leszek Koltunski
|
import org.distorted.library.MeshFlat;
|
35 |
95593730
|
Leszek Koltunski
|
import org.distorted.library.EffectTypes;
|
36 |
7589635e
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic1D;
|
37 |
|
|
import org.distorted.library.type.Dynamic3D;
|
38 |
|
|
import org.distorted.library.type.Static1D;
|
39 |
|
|
import org.distorted.library.type.Static3D;
|
40 |
|
|
import org.distorted.library.type.Static4D;
|
41 |
427ab7bf
|
Leszek Koltunski
|
|
42 |
|
|
import android.graphics.Bitmap;
|
43 |
|
|
import android.graphics.BitmapFactory;
|
44 |
|
|
import android.opengl.GLSurfaceView;
|
45 |
|
|
|
46 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47 |
|
|
|
48 |
f26ab2fd
|
Leszek Koltunski
|
class CatAndDogRenderer implements GLSurfaceView.Renderer
|
49 |
427ab7bf
|
Leszek Koltunski
|
{
|
50 |
|
|
private GLSurfaceView mView;
|
51 |
d04a4886
|
Leszek Koltunski
|
private DistortedEffects mEffects;
|
52 |
b01acdaf
|
Leszek Koltunski
|
private MeshFlat mMesh;
|
53 |
b0ebdf5e
|
Leszek Koltunski
|
private DistortedTexture mTexture;
|
54 |
d218d64e
|
leszek
|
private DistortedScreen mScreen;
|
55 |
427ab7bf
|
Leszek Koltunski
|
private int bmpHeight, bmpWidth;
|
56 |
59759251
|
Leszek Koltunski
|
|
57 |
427ab7bf
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58 |
|
|
|
59 |
6f3024ae
|
Leszek Koltunski
|
CatAndDogRenderer(GLSurfaceView v)
|
60 |
427ab7bf
|
Leszek Koltunski
|
{
|
61 |
|
|
mView = v;
|
62 |
40eef4b9
|
Leszek Koltunski
|
|
63 |
b01acdaf
|
Leszek Koltunski
|
mMesh = new MeshFlat(1,1); // no vertex effects, grid can be a (1x1) quad.
|
64 |
d04a4886
|
Leszek Koltunski
|
mEffects = new DistortedEffects();
|
65 |
40eef4b9
|
Leszek Koltunski
|
|
66 |
|
|
Static4D chromaRegion= new Static4D( 530, 200,100,100);
|
67 |
|
|
Static4D alphaRegion = new Static4D( 230, 200,100,100);
|
68 |
|
|
|
69 |
|
|
Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
|
70 |
|
|
chromaDyn.add(new Static1D(1));
|
71 |
|
|
chromaDyn.add(new Static1D(0));
|
72 |
|
|
|
73 |
392e16fd
|
Leszek Koltunski
|
mEffects.chroma(chromaDyn, new Static3D(1,0,0), chromaRegion ,true);
|
74 |
40eef4b9
|
Leszek Koltunski
|
|
75 |
|
|
Dynamic1D alphaDyn = new Dynamic1D(3000,0.0f);
|
76 |
|
|
alphaDyn.add(new Static1D(1));
|
77 |
|
|
alphaDyn.add(new Static1D(0));
|
78 |
|
|
|
79 |
392e16fd
|
Leszek Koltunski
|
mEffects.alpha( alphaDyn, alphaRegion, false );
|
80 |
|
|
|
81 |
d218d64e
|
leszek
|
mScreen = new DistortedScreen();
|
82 |
427ab7bf
|
Leszek Koltunski
|
}
|
83 |
|
|
|
84 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85 |
|
|
|
86 |
|
|
public void onDrawFrame(GL10 glUnused)
|
87 |
|
|
{
|
88 |
fe59d375
|
Leszek Koltunski
|
mScreen.render( System.currentTimeMillis() );
|
89 |
427ab7bf
|
Leszek Koltunski
|
}
|
90 |
|
|
|
91 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
92 |
|
|
|
93 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
94 |
e7a4ef16
|
Leszek Koltunski
|
{
|
95 |
427ab7bf
|
Leszek Koltunski
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat_and_dog);
|
96 |
|
|
Bitmap bitmap;
|
97 |
|
|
|
98 |
|
|
try
|
99 |
|
|
{
|
100 |
|
|
bitmap = BitmapFactory.decodeStream(is);
|
101 |
|
|
}
|
102 |
|
|
finally
|
103 |
|
|
{
|
104 |
|
|
try
|
105 |
|
|
{
|
106 |
|
|
is.close();
|
107 |
|
|
}
|
108 |
|
|
catch(IOException e) { }
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
bmpHeight = bitmap.getHeight();
|
112 |
|
|
bmpWidth = bitmap.getWidth();
|
113 |
e8b6aa95
|
Leszek Koltunski
|
|
114 |
b0ebdf5e
|
Leszek Koltunski
|
if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
|
115 |
|
|
mTexture.setTexture(bitmap);
|
116 |
fe59d375
|
Leszek Koltunski
|
|
117 |
|
|
mScreen.detachAll();
|
118 |
b0ebdf5e
|
Leszek Koltunski
|
mScreen.attach(mTexture,mEffects,mMesh);
|
119 |
59759251
|
Leszek Koltunski
|
|
120 |
6637d0f2
|
Leszek Koltunski
|
DistortedEffects.enableEffect(EffectNames.SMOOTH_CHROMA);
|
121 |
|
|
DistortedEffects.enableEffect(EffectNames.ALPHA);
|
122 |
|
|
|
123 |
427ab7bf
|
Leszek Koltunski
|
try
|
124 |
|
|
{
|
125 |
76f9798b
|
Leszek Koltunski
|
Distorted.onCreate(mView.getContext());
|
126 |
427ab7bf
|
Leszek Koltunski
|
}
|
127 |
|
|
catch(Exception ex)
|
128 |
|
|
{
|
129 |
|
|
android.util.Log.e("Renderer", ex.getMessage() );
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134 |
|
|
|
135 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
136 |
|
|
{
|
137 |
d79a56d3
|
Leszek Koltunski
|
// 1 cycle = 10000 ms = 10 seconds long
|
138 |
|
|
int duration = 10000;
|
139 |
|
|
|
140 |
|
|
// First abort all effect we might have had before
|
141 |
|
|
// (we will if we got here from a brief stay in the background)
|
142 |
|
|
mEffects.abortEffects(EffectTypes.MATRIX);
|
143 |
|
|
|
144 |
|
|
// Now start applying the effects. Matrix effects are, like the name suggests, multiplications
|
145 |
|
|
// of the Mesh by certain Projective Transformation Matrix. As such, those are - confusingly -
|
146 |
|
|
// applied in exactly the opposite order than given, i.e. here the last SCALE would be the
|
147 |
|
|
// first effect applied to each vertex of our Mesh, followed by a ROTATION, and only then by
|
148 |
|
|
// the MOVE !
|
149 |
|
|
|
150 |
|
|
// MOVES /////////
|
151 |
|
|
// Now, after the SCALE and ROTATE, the center of our Mesh is still exactly at the center of
|
152 |
|
|
// the Screen. Thus we move the Mesh back and forth between (0.5,0.5,0) and (-0.5,-0.5,0) ,
|
153 |
|
|
// i.e. between the lower right and upper left corners of the screen.
|
154 |
f988589e
|
Leszek Koltunski
|
Dynamic3D diMove = new Dynamic3D(duration,0.0f);
|
155 |
d79a56d3
|
Leszek Koltunski
|
Static3D lowerRight = new Static3D( +0.5f, +0.5f, 0);
|
156 |
|
|
Static3D upperLeft = new Static3D( -0.5f, -0.5f, 0);
|
157 |
|
|
diMove.add(lowerRight);
|
158 |
|
|
diMove.add(upperLeft);
|
159 |
|
|
mEffects.move(diMove);
|
160 |
|
|
|
161 |
|
|
// ROTATION //////
|
162 |
|
|
// Now, after the SCALE, our Texture is positioned at the center of the screen - coords (0,0,0).
|
163 |
|
|
// Rotate the whole Mesh around this point, along the (0,0,1) axis (i.e. vector perpendicular to
|
164 |
|
|
// the surface of the screen) starting from 0 degrees to 360 and back.
|
165 |
f988589e
|
Leszek Koltunski
|
Dynamic1D diRotate = new Dynamic1D(duration,0.0f);
|
166 |
7589635e
|
Leszek Koltunski
|
diRotate.add(new Static1D( 0));
|
167 |
|
|
diRotate.add(new Static1D(360));
|
168 |
d79a56d3
|
Leszek Koltunski
|
Static3D axis = new Static3D(0,0,1);
|
169 |
|
|
Static3D center = new Static3D(0,0,0);
|
170 |
|
|
mEffects.rotate( diRotate, axis, center );
|
171 |
|
|
|
172 |
|
|
// SCALING ///////
|
173 |
|
|
// Before we apply any effects, at the very start, out Mesh skinned by the 'Cat and Dog' texture
|
174 |
|
|
// fills up the whole Screen. First we need to scale it down so that the resolution of the
|
175 |
|
|
// original 'Cat and Dog' bitmap are preserved, (qx/qy part) and also apply a Dynamic switching
|
176 |
|
|
// the size from 80% of the Screen to 20% and back.
|
177 |
|
|
Dynamic3D diScale = new Dynamic3D(duration,0.0f);
|
178 |
|
|
float qx = (float)width /bmpWidth;
|
179 |
|
|
float qy = (float)height/bmpHeight;
|
180 |
|
|
float start_scale = 0.8f;
|
181 |
|
|
float end_scale = 0.2f;
|
182 |
|
|
diScale.add(qx<qy ? (new Static3D( start_scale,start_scale*qx/qy,1)) : (new Static3D(start_scale*qy/qx,start_scale,1)));
|
183 |
|
|
diScale.add(qx<qy ? (new Static3D( end_scale ,end_scale *qx/qy,1)) : (new Static3D(end_scale *qy/qx,end_scale ,1)));
|
184 |
392e16fd
|
Leszek Koltunski
|
mEffects.scale(diScale);
|
185 |
7589635e
|
Leszek Koltunski
|
|
186 |
d79a56d3
|
Leszek Koltunski
|
// Finally, tell the Screen itself the underlying OpenGL surface has changed dimensions.
|
187 |
392e16fd
|
Leszek Koltunski
|
mScreen.resize(width, height);
|
188 |
427ab7bf
|
Leszek Koltunski
|
}
|
189 |
|
|
}
|