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.aroundtheworld;
|
21
|
|
22
|
import android.graphics.Bitmap;
|
23
|
import android.graphics.BitmapFactory;
|
24
|
import android.opengl.GLSurfaceView;
|
25
|
|
26
|
import org.distorted.examples.R;
|
27
|
import org.distorted.library.effect.EffectType;
|
28
|
import org.distorted.library.effect.FragmentEffectChroma;
|
29
|
import org.distorted.library.effect.FragmentEffectContrast;
|
30
|
import org.distorted.library.effect.MatrixEffectScale;
|
31
|
import org.distorted.library.effect.VertexEffectDistort;
|
32
|
import org.distorted.library.effect.VertexEffectPinch;
|
33
|
import org.distorted.library.effect.VertexEffectScale;
|
34
|
import org.distorted.library.effect.VertexEffectSink;
|
35
|
import org.distorted.library.effect.VertexEffectSwirl;
|
36
|
import org.distorted.library.main.DistortedLibrary;
|
37
|
import org.distorted.library.main.DistortedEffects;
|
38
|
import org.distorted.library.main.DistortedScreen;
|
39
|
import org.distorted.library.mesh.MeshSquare;
|
40
|
import org.distorted.library.main.DistortedTexture;
|
41
|
import org.distorted.library.type.Static3D;
|
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 AroundTheWorldRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
|
52
|
{
|
53
|
private GLSurfaceView mView;
|
54
|
private DistortedEffects mEffects;
|
55
|
private DistortedTexture mTexture;
|
56
|
private DistortedScreen mScreen;
|
57
|
private MeshSquare mMesh;
|
58
|
private AroundTheWorldEffectsManager mManager;
|
59
|
private Static3D mScaleMatrix, mScaleVertex;
|
60
|
|
61
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62
|
|
63
|
AroundTheWorldRenderer(GLSurfaceView view)
|
64
|
{
|
65
|
mScaleMatrix= new Static3D(1,1,1);
|
66
|
mScaleVertex= new Static3D(1,1,1);
|
67
|
|
68
|
mView = view;
|
69
|
mManager = new AroundTheWorldEffectsManager();
|
70
|
mEffects = new DistortedEffects();
|
71
|
mEffects.apply(new MatrixEffectScale(mScaleMatrix));
|
72
|
mEffects.apply(new VertexEffectScale(mScaleVertex));
|
73
|
|
74
|
mManager.apply(mEffects);
|
75
|
mScreen = new DistortedScreen();
|
76
|
mScreen.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
|
77
|
}
|
78
|
|
79
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
80
|
|
81
|
AroundTheWorldEffectsManager getManager()
|
82
|
{
|
83
|
return mManager;
|
84
|
}
|
85
|
|
86
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87
|
|
88
|
public void onDrawFrame(GL10 glUnused)
|
89
|
{
|
90
|
mScreen.render( System.currentTimeMillis() );
|
91
|
}
|
92
|
|
93
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94
|
|
95
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
96
|
{
|
97
|
float horiRatio = (float)width / mScaleVertex.get0();
|
98
|
float vertRatio = (float)height/ mScaleVertex.get1();
|
99
|
float factor = Math.min(horiRatio,vertRatio);
|
100
|
|
101
|
mScaleMatrix.set( factor,factor,factor );
|
102
|
mScreen.resize(width,height);
|
103
|
}
|
104
|
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106
|
|
107
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
108
|
{
|
109
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.face);
|
110
|
Bitmap bitmap;
|
111
|
|
112
|
try
|
113
|
{
|
114
|
bitmap = BitmapFactory.decodeStream(is);
|
115
|
}
|
116
|
finally
|
117
|
{
|
118
|
try
|
119
|
{
|
120
|
is.close();
|
121
|
}
|
122
|
catch(IOException e) { }
|
123
|
}
|
124
|
|
125
|
int objWidth = bitmap.getWidth();
|
126
|
int objHeight= bitmap.getHeight();
|
127
|
|
128
|
mScaleVertex.set(objWidth,objHeight,1);
|
129
|
|
130
|
if( mTexture==null ) mTexture = new DistortedTexture();
|
131
|
mTexture.setTexture(bitmap);
|
132
|
|
133
|
if( mMesh==null ) mMesh = new MeshSquare(30,30*objHeight/objWidth);
|
134
|
|
135
|
mScreen.detachAll();
|
136
|
mScreen.attach(mTexture, mEffects, mMesh);
|
137
|
|
138
|
DistortedLibrary.setMax(EffectType.FRAGMENT, 9);
|
139
|
|
140
|
VertexEffectScale.enable();
|
141
|
VertexEffectDistort.enable();
|
142
|
VertexEffectSink.enable();
|
143
|
VertexEffectPinch.enable();
|
144
|
VertexEffectSwirl.enable();
|
145
|
FragmentEffectChroma.enable();
|
146
|
FragmentEffectContrast.enable();
|
147
|
|
148
|
DistortedLibrary.onSurfaceCreated(mView.getContext(),this);
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
|
153
|
public void distortedException(Exception ex)
|
154
|
{
|
155
|
android.util.Log.e("AroundTheWorld", ex.getMessage() );
|
156
|
}
|
157
|
}
|