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