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.effectqueue;
|
21
|
|
22
|
import javax.microedition.khronos.egl.EGLConfig;
|
23
|
import javax.microedition.khronos.opengles.GL10;
|
24
|
|
25
|
import android.graphics.Bitmap;
|
26
|
import android.graphics.Canvas;
|
27
|
import android.graphics.Paint;
|
28
|
import android.graphics.Paint.Style;
|
29
|
import android.opengl.GLSurfaceView;
|
30
|
|
31
|
import org.distorted.library.effect.EffectName;
|
32
|
import org.distorted.library.effect.FragmentEffectAlpha;
|
33
|
import org.distorted.library.effect.FragmentEffectChroma;
|
34
|
import org.distorted.library.effect.FragmentEffectSaturation;
|
35
|
import org.distorted.library.effect.MatrixEffectScale;
|
36
|
import org.distorted.library.effect.VertexEffectDistort;
|
37
|
import org.distorted.library.effect.VertexEffectSink;
|
38
|
import org.distorted.library.main.DistortedEffects;
|
39
|
import org.distorted.library.main.DistortedScreen;
|
40
|
import org.distorted.library.main.MeshFlat;
|
41
|
import org.distorted.library.main.DistortedTexture;
|
42
|
import org.distorted.library.main.Distorted;
|
43
|
import org.distorted.library.message.EffectListener;
|
44
|
import org.distorted.library.message.EffectMessage;
|
45
|
import org.distorted.library.type.Static3D;
|
46
|
|
47
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
48
|
|
49
|
class EffectQueueRenderer implements GLSurfaceView.Renderer, EffectListener
|
50
|
{
|
51
|
private static final int NUMLINES = 10;
|
52
|
final int BWID = 300;
|
53
|
final int BHEI = 400;
|
54
|
|
55
|
private EffectQueueSurfaceView mView;
|
56
|
private Paint mPaint;
|
57
|
private int texWidth, texHeight;
|
58
|
private Static3D mScale;
|
59
|
|
60
|
private DistortedTexture mTexture;
|
61
|
private MeshFlat mMesh;
|
62
|
private DistortedEffects mEffects;
|
63
|
private DistortedScreen mScreen;
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
|
67
|
EffectQueueRenderer(EffectQueueSurfaceView v)
|
68
|
{
|
69
|
mPaint = new Paint();
|
70
|
mPaint.setAntiAlias(true);
|
71
|
mPaint.setFakeBoldText(true);
|
72
|
mPaint.setStyle(Style.FILL);
|
73
|
|
74
|
mView = v;
|
75
|
|
76
|
texWidth = BWID;
|
77
|
texHeight= BHEI;
|
78
|
mScale = new Static3D(1,1,1);
|
79
|
MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
|
80
|
|
81
|
mMesh = new MeshFlat(80,80*texHeight/texWidth);
|
82
|
mTexture = new DistortedTexture(texWidth,texHeight);
|
83
|
mEffects = new DistortedEffects();
|
84
|
mEffects.apply(scaleEffect);
|
85
|
mEffects.registerForMessages(this);
|
86
|
|
87
|
mScreen = new DistortedScreen();
|
88
|
}
|
89
|
|
90
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
91
|
|
92
|
DistortedEffects getEffects()
|
93
|
{
|
94
|
return mEffects;
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
100
|
{
|
101
|
Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
|
102
|
Canvas canvas = new Canvas(bitmap);
|
103
|
|
104
|
mPaint.setColor(0xff008800);
|
105
|
canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
|
106
|
mPaint.setColor(0xffffffff);
|
107
|
|
108
|
for(int i=0; i<=NUMLINES ; i++ )
|
109
|
{
|
110
|
canvas.drawRect(texWidth*i/NUMLINES - 1, 0, texWidth*i/NUMLINES + 1, texHeight , mPaint);
|
111
|
canvas.drawRect( 0, texHeight*i/NUMLINES -1, texWidth , texHeight*i/NUMLINES + 1, mPaint);
|
112
|
}
|
113
|
mTexture.setTexture(bitmap);
|
114
|
|
115
|
mScreen.detachAll();
|
116
|
mScreen.attach(mTexture,mEffects,mMesh);
|
117
|
|
118
|
VertexEffectDistort.enable();
|
119
|
VertexEffectSink.enable();
|
120
|
FragmentEffectAlpha.enable();
|
121
|
FragmentEffectChroma.enable();
|
122
|
FragmentEffectSaturation.enable();
|
123
|
|
124
|
try
|
125
|
{
|
126
|
Distorted.onCreate(mView.getContext());
|
127
|
}
|
128
|
catch(Exception ex)
|
129
|
{
|
130
|
android.util.Log.e("EffectQueue", ex.getMessage() );
|
131
|
}
|
132
|
}
|
133
|
|
134
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135
|
|
136
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
137
|
{
|
138
|
mScale.set((float)width/texWidth,(float)height/texHeight,1);
|
139
|
mScreen.resize(width,height);
|
140
|
mView.setScreenSize(width,height);
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
|
145
|
public void onDrawFrame(GL10 glUnused)
|
146
|
{
|
147
|
mScreen.render( System.currentTimeMillis() );
|
148
|
}
|
149
|
|
150
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
151
|
// the library sending messages to us. This is running on a library 'MessageSender' thread.
|
152
|
|
153
|
public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
|
154
|
{
|
155
|
EffectQueueActivity act = (EffectQueueActivity)mView.getContext();
|
156
|
|
157
|
switch(em)
|
158
|
{
|
159
|
case EFFECT_REMOVED : act.effectRemoved(effectID) ; break;
|
160
|
case EFFECT_FINISHED: act.effectFinished(effectID); break;
|
161
|
default : break;
|
162
|
}
|
163
|
}
|
164
|
}
|