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.check;
|
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.EffectName;
|
31
|
import org.distorted.library.effect.EffectType;
|
32
|
import org.distorted.library.effect.FragmentEffectChroma;
|
33
|
import org.distorted.library.effect.MatrixEffectMove;
|
34
|
import org.distorted.library.effect.MatrixEffectScale;
|
35
|
import org.distorted.library.effect.VertexEffectDeform;
|
36
|
import org.distorted.library.effect.VertexEffectSwirl;
|
37
|
import org.distorted.library.main.Distorted;
|
38
|
import org.distorted.library.main.DistortedEffects;
|
39
|
import org.distorted.library.main.DistortedScreen;
|
40
|
import org.distorted.library.main.DistortedTexture;
|
41
|
import org.distorted.library.main.MeshFlat;
|
42
|
import org.distorted.library.type.Dynamic3D;
|
43
|
import org.distorted.library.type.Static1D;
|
44
|
import org.distorted.library.type.Static3D;
|
45
|
import org.distorted.library.type.Static4D;
|
46
|
import org.distorted.library.type.Dynamic1D;
|
47
|
|
48
|
import android.app.AlertDialog;
|
49
|
import android.content.Context;
|
50
|
import android.content.DialogInterface;
|
51
|
import android.graphics.Bitmap;
|
52
|
import android.graphics.BitmapFactory;
|
53
|
import android.opengl.GLES30;
|
54
|
import android.opengl.GLSurfaceView;
|
55
|
import android.util.Log;
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
class CheckRenderer implements GLSurfaceView.Renderer
|
60
|
{
|
61
|
private static String compilationResult;
|
62
|
private static String compilationTitle;
|
63
|
|
64
|
private GLSurfaceView mView;
|
65
|
private DistortedEffects mEffects;
|
66
|
private DistortedTexture mTexture;
|
67
|
private MeshFlat mMesh;
|
68
|
private DistortedScreen mScreen;
|
69
|
private int mObjHeight, mObjWidth;
|
70
|
private Static3D mMove, mScale, mCenter;
|
71
|
private Static3D mSwirl1, mSwirl2, mDeform1, mDeform2;
|
72
|
|
73
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
74
|
|
75
|
CheckRenderer(GLSurfaceView view)
|
76
|
{
|
77
|
mView = view;
|
78
|
|
79
|
CheckActivity act = (CheckActivity)mView.getContext();
|
80
|
|
81
|
DistortedEffects.setMax(EffectType.VERTEX ,act.getMaxV());
|
82
|
DistortedEffects.setMax(EffectType.FRAGMENT,act.getMaxF());
|
83
|
VertexEffectSwirl.enable();
|
84
|
VertexEffectDeform.enable();
|
85
|
FragmentEffectChroma.enable();
|
86
|
|
87
|
mSwirl1 = new Static3D(0,0,0);
|
88
|
mSwirl2 = new Static3D(0,0,0);
|
89
|
mDeform1= new Static3D(0,0,0);
|
90
|
mDeform2= new Static3D(0,0,0);
|
91
|
mMove = new Static3D(0,0,0);
|
92
|
mScale = new Static3D(1,1,1);
|
93
|
mCenter = new Static3D(0,0,0);
|
94
|
|
95
|
mEffects = new DistortedEffects();
|
96
|
mEffects.apply(new MatrixEffectMove(mMove));
|
97
|
mEffects.apply(new MatrixEffectScale(mScale));
|
98
|
|
99
|
// Try adding 2 Vertex Effects to the Bitmap.
|
100
|
// This will fail if we have set maxVertexEffects to something < 2.
|
101
|
//
|
102
|
// Even if adding some of the Effects fails, the App will still start - you just won't see
|
103
|
// the effects that failed to add.
|
104
|
|
105
|
Dynamic3D dSwirl = new Dynamic3D(2000,0.0f);
|
106
|
dSwirl.add(mSwirl1);
|
107
|
dSwirl.add(mSwirl2);
|
108
|
|
109
|
if( !mEffects.apply( new VertexEffectSwirl(new Static1D(30), dSwirl, new Static4D( 0,0,40,40)) ) )
|
110
|
{
|
111
|
Log.e("Check", "Failed to add Swirl effect!");
|
112
|
}
|
113
|
|
114
|
Dynamic3D dDeform = new Dynamic3D(2000,0.0f);
|
115
|
dDeform.add(mDeform1);
|
116
|
dDeform.add(mDeform2);
|
117
|
|
118
|
if( !mEffects.apply( new VertexEffectDeform(dDeform,mCenter) ) )
|
119
|
{
|
120
|
Log.e("Check", "Failed to add Deform effect!");
|
121
|
}
|
122
|
|
123
|
// Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
|
124
|
Static3D color = new Static3D(1,0,0);
|
125
|
Dynamic1D inter = new Dynamic1D(2000,0.0f);
|
126
|
inter.add(new Static1D(0));
|
127
|
inter.add(new Static1D(1));
|
128
|
|
129
|
if( !mEffects.apply( new FragmentEffectChroma(inter, color) ) )
|
130
|
{
|
131
|
Log.e("Check", "Failed to add Chroma effect!");
|
132
|
}
|
133
|
|
134
|
mScreen = new DistortedScreen();
|
135
|
}
|
136
|
|
137
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138
|
|
139
|
public void onDrawFrame(GL10 glUnused)
|
140
|
{
|
141
|
mScreen.render( System.currentTimeMillis() );
|
142
|
}
|
143
|
|
144
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
145
|
|
146
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
147
|
{
|
148
|
if( (float)mObjHeight/mObjWidth > (float)height/width )
|
149
|
{
|
150
|
int w = (height*mObjWidth)/mObjHeight;
|
151
|
float factor = (float)height/mObjHeight;
|
152
|
mMove.set((width-w)/2,0,0);
|
153
|
mScale.set(factor,factor,factor);
|
154
|
}
|
155
|
else
|
156
|
{
|
157
|
int h = (width*mObjHeight)/mObjWidth;
|
158
|
float factor = (float)width/mObjWidth;
|
159
|
mMove.set(0,(height-h)/2,0);
|
160
|
mScale.set(factor,factor,factor);
|
161
|
}
|
162
|
|
163
|
mScreen.resize(width,height);
|
164
|
}
|
165
|
|
166
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
167
|
|
168
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
169
|
{
|
170
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
|
171
|
Bitmap bitmap;
|
172
|
|
173
|
try
|
174
|
{
|
175
|
bitmap = BitmapFactory.decodeStream(is);
|
176
|
}
|
177
|
finally
|
178
|
{
|
179
|
try
|
180
|
{
|
181
|
is.close();
|
182
|
}
|
183
|
catch(IOException e) { }
|
184
|
}
|
185
|
|
186
|
mObjHeight = bitmap.getHeight();
|
187
|
mObjWidth = bitmap.getWidth();
|
188
|
|
189
|
if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
|
190
|
mTexture.setTexture(bitmap);
|
191
|
|
192
|
if( mMesh==null ) mMesh = new MeshFlat(30,30*mObjHeight/mObjWidth);
|
193
|
|
194
|
mScreen.detachAll();
|
195
|
mScreen.attach(mTexture,mEffects,mMesh);
|
196
|
|
197
|
mSwirl1.set ( 0, mObjHeight/2, 0);
|
198
|
mSwirl2.set (mObjWidth , mObjHeight/2, 0);
|
199
|
mDeform1.set( 0, 0 , 0);
|
200
|
mDeform2.set( 0,-mObjHeight , 0);
|
201
|
mCenter.set (mObjWidth/2, 0 , 0);
|
202
|
|
203
|
try
|
204
|
{
|
205
|
Distorted.onCreate(mView.getContext());
|
206
|
}
|
207
|
catch(Exception ex)
|
208
|
{
|
209
|
// We have failed to compile or link shaders - probably too many effects.
|
210
|
// (each effect adds a certain number of Uniforms to the shader and there
|
211
|
// can only be a limited number of those )
|
212
|
|
213
|
compilationTitle = ex.getClass().getSimpleName();
|
214
|
compilationResult= ex.getMessage();
|
215
|
|
216
|
String ver;
|
217
|
|
218
|
ver = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
|
219
|
compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
|
220
|
ver = GLES30.glGetString(GLES30.GL_VERSION);
|
221
|
compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
|
222
|
ver = GLES30.glGetString(GLES30.GL_VENDOR);
|
223
|
compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
|
224
|
ver = GLES30.glGetString(GLES30.GL_RENDERER);
|
225
|
compilationResult += "\nGL renderer: "+(ver==null ? "null" : ver);
|
226
|
|
227
|
CheckActivity act = (CheckActivity)mView.getContext();
|
228
|
|
229
|
act.runOnUiThread(new Runnable()
|
230
|
{
|
231
|
public void run()
|
232
|
{
|
233
|
Context context = mView.getContext();
|
234
|
|
235
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
236
|
builder.setTitle(compilationTitle);
|
237
|
builder.setMessage(compilationResult);
|
238
|
builder.setCancelable(true);
|
239
|
|
240
|
builder.setOnCancelListener(new DialogInterface.OnCancelListener()
|
241
|
{
|
242
|
@Override
|
243
|
public void onCancel(DialogInterface dialog)
|
244
|
{
|
245
|
((CheckActivity)mView.getContext()).finish();
|
246
|
}
|
247
|
});
|
248
|
|
249
|
AlertDialog alert = builder.create();
|
250
|
alert.show();
|
251
|
}
|
252
|
});
|
253
|
}
|
254
|
}
|
255
|
|
256
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
257
|
|
258
|
}
|