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.Distorted;
|
31
|
import org.distorted.library.DistortedBitmap;
|
32
|
import org.distorted.library.EffectTypes;
|
33
|
import org.distorted.library.Float1D;
|
34
|
import org.distorted.library.Float2D;
|
35
|
import org.distorted.library.Float3D;
|
36
|
import org.distorted.library.Float4D;
|
37
|
import org.distorted.library.Interpolator1D;
|
38
|
import org.distorted.library.Interpolator2D;
|
39
|
|
40
|
import android.app.AlertDialog;
|
41
|
import android.content.Context;
|
42
|
import android.content.DialogInterface;
|
43
|
import android.graphics.Bitmap;
|
44
|
import android.graphics.BitmapFactory;
|
45
|
import android.opengl.GLES20;
|
46
|
import android.opengl.GLSurfaceView;
|
47
|
|
48
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
49
|
|
50
|
class CheckRenderer implements GLSurfaceView.Renderer
|
51
|
{
|
52
|
private GLSurfaceView mView;
|
53
|
private DistortedBitmap mSuccess;
|
54
|
private int bmpHeight, bmpWidth;
|
55
|
|
56
|
public static String compilationResult;
|
57
|
public static String compilationTitle;
|
58
|
|
59
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
60
|
|
61
|
public CheckRenderer(GLSurfaceView view)
|
62
|
{
|
63
|
mView = view;
|
64
|
|
65
|
Distorted.setMaxVertex(CheckActivity.getMaxV());
|
66
|
Distorted.setMaxFragment(CheckActivity.getMaxF());
|
67
|
}
|
68
|
|
69
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
70
|
|
71
|
public void onDrawFrame(GL10 glUnused)
|
72
|
{
|
73
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
74
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
75
|
|
76
|
mSuccess.draw(System.currentTimeMillis());
|
77
|
}
|
78
|
|
79
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
80
|
|
81
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
82
|
{
|
83
|
mSuccess.abortEffects(EffectTypes.MATRIX);
|
84
|
|
85
|
if( bmpHeight/bmpWidth > height/width )
|
86
|
{
|
87
|
int w = (height*bmpWidth)/bmpHeight;
|
88
|
mSuccess.move((width-w)/2 ,0, 0);
|
89
|
mSuccess.scale((float)height/bmpHeight);
|
90
|
}
|
91
|
else
|
92
|
{
|
93
|
int h = (width*bmpHeight)/bmpWidth;
|
94
|
mSuccess.move(0 ,(height-h)/2, 0);
|
95
|
mSuccess.scale((float)width/bmpWidth);
|
96
|
}
|
97
|
|
98
|
Distorted.onSurfaceChanged(width, height);
|
99
|
}
|
100
|
|
101
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
102
|
|
103
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
104
|
{
|
105
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
|
106
|
Bitmap bitmap;
|
107
|
|
108
|
try
|
109
|
{
|
110
|
bitmap = BitmapFactory.decodeStream(is);
|
111
|
}
|
112
|
finally
|
113
|
{
|
114
|
try
|
115
|
{
|
116
|
is.close();
|
117
|
}
|
118
|
catch(IOException e) { }
|
119
|
}
|
120
|
|
121
|
mSuccess = new DistortedBitmap(bitmap, 30);
|
122
|
|
123
|
bmpHeight = bitmap.getHeight();
|
124
|
bmpWidth = bitmap.getWidth();
|
125
|
|
126
|
// Try adding 2 Vertex Effects to the Bitmap.
|
127
|
// This will fail if we have set maxVertexEffects to something < 2.
|
128
|
//
|
129
|
// Even if adding some of the Effects fails, the App will still start - you just won't see
|
130
|
// the effects that failed to add.
|
131
|
Float2D pDown = new Float2D(bmpWidth/2, 0);
|
132
|
Float3D vDown = new Float3D( 0,-bmpHeight,0);
|
133
|
Float4D mRegion = new Float4D( 0, 0, 40 ,40 );
|
134
|
|
135
|
Interpolator2D mPoint = new Interpolator2D();
|
136
|
mPoint.setCount(0.0f);
|
137
|
mPoint.setDuration(2000);
|
138
|
mPoint.add(new Float2D( 0, bmpHeight/2));
|
139
|
mPoint.add(new Float2D( bmpWidth, bmpHeight/2));
|
140
|
|
141
|
mSuccess.swirl( 30, mRegion, mPoint );
|
142
|
mSuccess.deform(vDown, pDown, 2000, 0.0f);
|
143
|
|
144
|
// Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
|
145
|
Float3D color = new Float3D(1,0,0);
|
146
|
Interpolator1D inter = new Interpolator1D();
|
147
|
inter.setCount(0.0f);
|
148
|
inter.setDuration(2000);
|
149
|
inter.add(new Float1D(0));
|
150
|
inter.add(new Float1D(1));
|
151
|
|
152
|
mSuccess.chroma(inter, color, null, new Float2D(0,0) );
|
153
|
|
154
|
try
|
155
|
{
|
156
|
Distorted.onSurfaceCreated(mView.getContext());
|
157
|
}
|
158
|
catch(Exception ex)
|
159
|
{
|
160
|
// We have failed to compile or link shaders - probably too many effects.
|
161
|
// (each effect adds a certain number of Uniforms to the shader and there
|
162
|
// can only be a limited number of those )
|
163
|
|
164
|
compilationTitle = ex.getClass().getSimpleName();
|
165
|
compilationResult= ex.getMessage();
|
166
|
|
167
|
String ver;
|
168
|
|
169
|
ver = GLES20.glGetString(GLES20.GL_SHADING_LANGUAGE_VERSION);
|
170
|
compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
|
171
|
ver = GLES20.glGetString(GLES20.GL_VERSION);
|
172
|
compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
|
173
|
ver = GLES20.glGetString(GLES20.GL_VENDOR);
|
174
|
compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
|
175
|
ver = GLES20.glGetString(GLES20.GL_RENDERER);
|
176
|
compilationResult += "\nGL renderer: "+(ver==null ? "null" : ver);
|
177
|
|
178
|
CheckActivity act = (CheckActivity)mView.getContext();
|
179
|
|
180
|
act.runOnUiThread(new Runnable()
|
181
|
{
|
182
|
public void run()
|
183
|
{
|
184
|
Context context = mView.getContext();
|
185
|
|
186
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
187
|
builder.setTitle(compilationTitle);
|
188
|
builder.setMessage(compilationResult);
|
189
|
builder.setCancelable(true);
|
190
|
|
191
|
builder.setOnCancelListener(new DialogInterface.OnCancelListener()
|
192
|
{
|
193
|
@Override
|
194
|
public void onCancel(DialogInterface dialog)
|
195
|
{
|
196
|
((CheckActivity)mView.getContext()).finish();
|
197
|
}
|
198
|
});
|
199
|
|
200
|
AlertDialog alert = builder.create();
|
201
|
alert.show();
|
202
|
}
|
203
|
});
|
204
|
}
|
205
|
}
|
206
|
|
207
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
208
|
|
209
|
}
|