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.fragment3d;
|
21
|
|
22
|
import android.graphics.Bitmap;
|
23
|
import android.graphics.BitmapFactory;
|
24
|
import android.opengl.GLES20;
|
25
|
import android.opengl.GLSurfaceView;
|
26
|
|
27
|
import org.distorted.examples.R;
|
28
|
import org.distorted.library.Distorted;
|
29
|
import org.distorted.library.DistortedBitmap;
|
30
|
import org.distorted.library.DistortedCubes;
|
31
|
import org.distorted.library.DistortedObject;
|
32
|
import org.distorted.library.EffectNames;
|
33
|
import org.distorted.library.EffectTypes;
|
34
|
import org.distorted.library.type.Dynamic1D;
|
35
|
import org.distorted.library.type.Dynamic2D;
|
36
|
import org.distorted.library.type.Dynamic3D;
|
37
|
import org.distorted.library.type.DynamicQuat;
|
38
|
import org.distorted.library.type.Static1D;
|
39
|
import org.distorted.library.type.Static2D;
|
40
|
import org.distorted.library.type.Static3D;
|
41
|
import org.distorted.library.type.Static4D;
|
42
|
|
43
|
import java.io.IOException;
|
44
|
import java.io.InputStream;
|
45
|
|
46
|
import javax.microedition.khronos.egl.EGLConfig;
|
47
|
import javax.microedition.khronos.opengles.GL10;
|
48
|
|
49
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
50
|
|
51
|
class Fragment3DRenderer implements GLSurfaceView.Renderer
|
52
|
{
|
53
|
private static final int SIZE = 100;
|
54
|
|
55
|
private GLSurfaceView mView;
|
56
|
private DistortedObject mObject;
|
57
|
private DistortedBitmap mCenter;
|
58
|
private float mFactorCen, mFactorObj;
|
59
|
|
60
|
private int mObjWidth, mObjHeight;
|
61
|
|
62
|
private DynamicQuat mQuatInt1, mQuatInt2;
|
63
|
|
64
|
private EffectNames[] order;
|
65
|
|
66
|
private Dynamic2D mCenterInter;
|
67
|
private Dynamic3D mChromaInter, mMoveInter;
|
68
|
private Dynamic1D mChromaLevelInter, mAlphaInter, mBrightnessInter, mSaturationInter;
|
69
|
|
70
|
private Static2D mCenterPoint;
|
71
|
private Static3D mChromaPoint, mMovePoint;
|
72
|
private Static1D mChromaLevel, mAlphaPoint, mBrightnessPoint, mSaturationPoint;
|
73
|
|
74
|
Static4D mQuat1, mQuat2;
|
75
|
int mScreenMin;
|
76
|
|
77
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
78
|
|
79
|
public void setChroma(float level, float r, float g, float b)
|
80
|
{
|
81
|
mChromaLevel.set(level);
|
82
|
mChromaPoint.set(r, g, b);
|
83
|
}
|
84
|
|
85
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86
|
|
87
|
public void setAlpha(float level)
|
88
|
{
|
89
|
mAlphaPoint.set(level);
|
90
|
}
|
91
|
|
92
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
93
|
|
94
|
public void setBrightness(float level)
|
95
|
{
|
96
|
mBrightnessPoint.set(level);
|
97
|
}
|
98
|
|
99
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
100
|
|
101
|
public void setSaturation(float level)
|
102
|
{
|
103
|
mSaturationPoint.set(level);
|
104
|
}
|
105
|
|
106
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
107
|
|
108
|
public void setCenter(float x, float y)
|
109
|
{
|
110
|
x = (0.012f*x-0.1f)*mObjWidth;
|
111
|
y = (0.012f*y-0.1f)*mObjHeight;
|
112
|
|
113
|
mCenterPoint.set(x,y);
|
114
|
mMovePoint.set(mFactorObj*x,mFactorObj*y,0);
|
115
|
}
|
116
|
|
117
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118
|
|
119
|
public void setOrder(EffectNames[] effects)
|
120
|
{
|
121
|
order = effects;
|
122
|
setFragmentEffects();
|
123
|
}
|
124
|
|
125
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
126
|
|
127
|
private void setFragmentEffects()
|
128
|
{
|
129
|
mObject.abortEffects(EffectTypes.FRAGMENT);
|
130
|
|
131
|
for( int i=0; i<=order.length-1 ; i++ )
|
132
|
{
|
133
|
switch(order[i])
|
134
|
{
|
135
|
case CHROMA : mObject.chroma ( mChromaLevelInter , mChromaInter) ; break;
|
136
|
case ALPHA : mObject.alpha ( mAlphaInter ) ; break;
|
137
|
case BRIGHTNESS: mObject.brightness( mBrightnessInter ) ; break;
|
138
|
case SATURATION: mObject.saturation( mSaturationInter ) ; break;
|
139
|
}
|
140
|
}
|
141
|
}
|
142
|
|
143
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
144
|
|
145
|
public Fragment3DRenderer(GLSurfaceView v)
|
146
|
{
|
147
|
mView = v;
|
148
|
|
149
|
mObject = ((Fragment3DActivity)v.getContext()).getObject();
|
150
|
mCenter = new DistortedBitmap(SIZE, SIZE, 1);
|
151
|
|
152
|
mObjWidth = mObject.getWidth();
|
153
|
mObjHeight= mObject.getHeight();
|
154
|
|
155
|
mCenterPoint = new Static2D(0,0);
|
156
|
mMovePoint = new Static3D(0,0,0);
|
157
|
mChromaPoint = new Static3D(0,0,0);
|
158
|
mChromaLevel = new Static1D(0);
|
159
|
mAlphaPoint = new Static1D(1);
|
160
|
mBrightnessPoint= new Static1D(1);
|
161
|
mSaturationPoint= new Static1D(0.5f);
|
162
|
|
163
|
mCenterInter = new Dynamic2D();
|
164
|
mMoveInter = new Dynamic3D();
|
165
|
mChromaInter = new Dynamic3D();
|
166
|
mChromaLevelInter= new Dynamic1D();
|
167
|
mAlphaInter = new Dynamic1D();
|
168
|
mBrightnessInter = new Dynamic1D();
|
169
|
mSaturationInter = new Dynamic1D();
|
170
|
|
171
|
mCenterInter.add(mCenterPoint);
|
172
|
mMoveInter.add(mMovePoint);
|
173
|
mChromaInter.add(mChromaPoint);
|
174
|
mChromaLevelInter.add(mChromaLevel);
|
175
|
mAlphaInter.add(mAlphaPoint);
|
176
|
mBrightnessInter.add(mBrightnessPoint);
|
177
|
mSaturationInter.add(mSaturationPoint);
|
178
|
|
179
|
mQuat1 = new Static4D(0,0,0,1); // unity
|
180
|
mQuat2 = new Static4D(0,0,0,1); // quaternions
|
181
|
|
182
|
mQuatInt1 = new DynamicQuat(0,0.5f);
|
183
|
mQuatInt2 = new DynamicQuat(0,0.5f);
|
184
|
|
185
|
mQuatInt1.add(mQuat1);
|
186
|
mQuatInt2.add(mQuat2);
|
187
|
}
|
188
|
|
189
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
190
|
|
191
|
public void onDrawFrame(GL10 glUnused)
|
192
|
{
|
193
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
194
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
195
|
|
196
|
long time = System.currentTimeMillis();
|
197
|
|
198
|
mObject.draw(time);
|
199
|
mCenter.draw(time);
|
200
|
}
|
201
|
|
202
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
203
|
|
204
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
205
|
{
|
206
|
mScreenMin = width<height ? width:height;
|
207
|
|
208
|
mObject.abortEffects(EffectTypes.MATRIX);
|
209
|
mCenter.abortEffects(EffectTypes.MATRIX);
|
210
|
|
211
|
int centerSize = mCenter.getWidth();
|
212
|
|
213
|
if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
|
214
|
{
|
215
|
mFactorObj = (0.70f*height)/mObjHeight;
|
216
|
mFactorCen = (0.15f*height)/centerSize;
|
217
|
}
|
218
|
else
|
219
|
{
|
220
|
mFactorObj = (0.70f*width)/mObjWidth;
|
221
|
mFactorCen = (0.15f*width)/centerSize;
|
222
|
}
|
223
|
|
224
|
Fragment3DActivity act = (Fragment3DActivity)mView.getContext();
|
225
|
float cX = act.getCenterX();
|
226
|
float cY = act.getCenterY();
|
227
|
|
228
|
cX = (0.012f*cX-0.1f)*mObjWidth;
|
229
|
cY = (0.012f*cY-0.1f)*mObjHeight;
|
230
|
|
231
|
mMovePoint.set(cX*mFactorObj,cY*mFactorObj,0);
|
232
|
|
233
|
Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
|
234
|
|
235
|
mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
|
236
|
mObject.scale(mFactorObj);
|
237
|
mObject.quaternion(mQuatInt1, rotateObj);
|
238
|
mObject.quaternion(mQuatInt2, rotateObj);
|
239
|
|
240
|
Static3D rotateCen = new Static3D(width/2,height/2, 0);
|
241
|
|
242
|
mCenter.quaternion(mQuatInt1, rotateCen);
|
243
|
mCenter.quaternion(mQuatInt2, rotateCen);
|
244
|
mCenter.move( new Static3D( (width -mFactorCen*centerSize-mFactorObj*mObjWidth )/2 ,
|
245
|
(height-mFactorCen*centerSize-mFactorObj*mObjHeight)/2 , 10) );
|
246
|
mCenter.move(mMoveInter);
|
247
|
mCenter.scale(mFactorCen);
|
248
|
|
249
|
setFragmentEffects();
|
250
|
|
251
|
Distorted.onSurfaceChanged(width, height);
|
252
|
}
|
253
|
|
254
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
255
|
|
256
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
257
|
{
|
258
|
Fragment3DActivity act = (Fragment3DActivity)mView.getContext();
|
259
|
|
260
|
InputStream is1 = act.getResources().openRawResource(act.getBitmap());
|
261
|
InputStream is2 = act.getResources().openRawResource(R.raw.center);
|
262
|
|
263
|
Bitmap bitmap1,bitmap2;
|
264
|
|
265
|
try
|
266
|
{
|
267
|
bitmap1 = BitmapFactory.decodeStream(is1);
|
268
|
bitmap2 = BitmapFactory.decodeStream(is2);
|
269
|
}
|
270
|
finally
|
271
|
{
|
272
|
try
|
273
|
{
|
274
|
is1.close();
|
275
|
is2.close();
|
276
|
}
|
277
|
catch(IOException e) { }
|
278
|
}
|
279
|
|
280
|
mObject.setBitmap(bitmap1);
|
281
|
mCenter.setBitmap(bitmap2);
|
282
|
|
283
|
try
|
284
|
{
|
285
|
Distorted.onSurfaceCreated(mView.getContext());
|
286
|
}
|
287
|
catch(Exception ex)
|
288
|
{
|
289
|
android.util.Log.e("Fragment3D", ex.getMessage() );
|
290
|
}
|
291
|
}
|
292
|
}
|