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.catanddog;
|
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
|
import org.distorted.library.effect.FragmentEffectAlpha;
|
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.DistortedLibrary;
|
35
|
import org.distorted.library.main.DistortedEffects;
|
36
|
import org.distorted.library.main.DistortedScreen;
|
37
|
import org.distorted.library.main.DistortedTexture;
|
38
|
import org.distorted.library.mesh.MeshRectangles;
|
39
|
import org.distorted.library.type.Dynamic;
|
40
|
import org.distorted.library.type.Dynamic1D;
|
41
|
import org.distorted.library.type.Dynamic3D;
|
42
|
import org.distorted.library.type.Static1D;
|
43
|
import org.distorted.library.type.Static3D;
|
44
|
|
45
|
import android.graphics.Bitmap;
|
46
|
import android.graphics.BitmapFactory;
|
47
|
import android.opengl.GLSurfaceView;
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
class CatAndDogRenderer implements GLSurfaceView.Renderer
|
52
|
{
|
53
|
private static final int DURATION_CHROMA_ALPHA = 3000;
|
54
|
private static final int DURATION_MATRIX_EFFECTS = 10000;
|
55
|
|
56
|
private GLSurfaceView mView;
|
57
|
private DistortedEffects mEffects;
|
58
|
private MeshRectangles mMesh;
|
59
|
private DistortedTexture mTexture;
|
60
|
private DistortedScreen mScreen;
|
61
|
private int mObjHeight, mObjWidth;
|
62
|
private Static3D mMoveStartingPoint, mMoveEndPoint;
|
63
|
|
64
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
65
|
|
66
|
CatAndDogRenderer(GLSurfaceView v)
|
67
|
{
|
68
|
mView = v;
|
69
|
|
70
|
mMesh = new MeshRectangles(1,1); // no vertex effects, grid can be a (1x1) quad.
|
71
|
|
72
|
Dynamic3D moveDyn= new Dynamic3D(DURATION_MATRIX_EFFECTS,0.0f);
|
73
|
|
74
|
mMoveStartingPoint = new Static3D(0,0,0);
|
75
|
mMoveEndPoint = new Static3D(0,0,0);
|
76
|
|
77
|
moveDyn.add(mMoveStartingPoint);
|
78
|
moveDyn.add(mMoveEndPoint);
|
79
|
|
80
|
Static3D chromaCenter= new Static3D( 130, 50, 0 );
|
81
|
Static3D chromaRegion= new Static3D( 100, 100, 100 );
|
82
|
Static3D alphaCenter = new Static3D(-170, 50, 0 );
|
83
|
Static3D alphaRegion = new Static3D( 100, 100, 100 );
|
84
|
|
85
|
Dynamic1D chromaDyn = new Dynamic1D(DURATION_CHROMA_ALPHA,0.0f);
|
86
|
chromaDyn.add(new Static1D(1));
|
87
|
chromaDyn.add(new Static1D(0));
|
88
|
Dynamic1D alphaDyn = new Dynamic1D(DURATION_CHROMA_ALPHA,0.0f);
|
89
|
alphaDyn.add(new Static1D(1));
|
90
|
alphaDyn.add(new Static1D(0));
|
91
|
Dynamic3D diScale = new Dynamic3D(DURATION_MATRIX_EFFECTS,0.0f);
|
92
|
diScale.add(new Static3D(1,1,1));
|
93
|
diScale.add(new Static3D(0.33f,0.33f,1));
|
94
|
diScale.add(new Static3D(1,1,1));
|
95
|
diScale.setMode(Dynamic.MODE_PATH);
|
96
|
Dynamic1D diRotate = new Dynamic1D(DURATION_MATRIX_EFFECTS,0.0f);
|
97
|
diRotate.add(new Static1D( 0));
|
98
|
diRotate.add(new Static1D(360));
|
99
|
|
100
|
mEffects = new DistortedEffects();
|
101
|
mEffects.apply( new FragmentEffectChroma( chromaDyn, new Static3D(1,0,0), chromaCenter, chromaRegion ,true) );
|
102
|
mEffects.apply( new FragmentEffectAlpha(alphaDyn, alphaCenter, alphaRegion, false) );
|
103
|
mEffects.apply( new MatrixEffectMove(moveDyn));
|
104
|
mEffects.apply( new MatrixEffectScale(diScale));
|
105
|
mEffects.apply( new MatrixEffectRotate( diRotate, new Static3D(0,0,1), new Static3D(0,0,0)) );
|
106
|
|
107
|
mScreen = new DistortedScreen();
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
113
|
{
|
114
|
mMoveStartingPoint.set( (width-mObjWidth)/2, (height-mObjHeight)/2, 0);
|
115
|
mMoveEndPoint .set(-(width-mObjWidth)/2,-(height-mObjHeight)/2, 0);
|
116
|
|
117
|
mScreen.resize(width, height);
|
118
|
}
|
119
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
120
|
|
121
|
public void onDrawFrame(GL10 glUnused)
|
122
|
{
|
123
|
mScreen.render( System.currentTimeMillis() );
|
124
|
}
|
125
|
|
126
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127
|
|
128
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
129
|
{
|
130
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat_and_dog);
|
131
|
Bitmap bitmap;
|
132
|
|
133
|
try
|
134
|
{
|
135
|
bitmap = BitmapFactory.decodeStream(is);
|
136
|
}
|
137
|
finally
|
138
|
{
|
139
|
try
|
140
|
{
|
141
|
is.close();
|
142
|
}
|
143
|
catch(IOException e) { }
|
144
|
}
|
145
|
|
146
|
mObjHeight = bitmap.getHeight();
|
147
|
mObjWidth = bitmap.getWidth();
|
148
|
|
149
|
mMesh.setStretch(mObjWidth,mObjHeight,0);
|
150
|
|
151
|
if( mTexture==null ) mTexture = new DistortedTexture();
|
152
|
mTexture.setTexture(bitmap);
|
153
|
|
154
|
mScreen.detachAll();
|
155
|
mScreen.attach(mTexture,mEffects,mMesh);
|
156
|
|
157
|
FragmentEffectChroma.enable();
|
158
|
FragmentEffectAlpha.enable();
|
159
|
|
160
|
try
|
161
|
{
|
162
|
DistortedLibrary.onCreate(mView.getContext());
|
163
|
}
|
164
|
catch(Exception ex)
|
165
|
{
|
166
|
android.util.Log.e("CatAndDog", ex.getMessage() );
|
167
|
}
|
168
|
}
|
169
|
}
|