1 |
bc0a685b
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
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 |
427ab7bf
|
Leszek Koltunski
|
|
20 |
5ceea148
|
Leszek Koltunski
|
package org.distorted.examples.objecttree;
|
21 |
427ab7bf
|
Leszek Koltunski
|
|
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 |
5068fa06
|
Leszek Koltunski
|
import org.distorted.examples.R;
|
29 |
427ab7bf
|
Leszek Koltunski
|
|
30 |
6646c476
|
leszek
|
import org.distorted.library.effect.FragmentEffectChroma;
|
31 |
|
|
import org.distorted.library.effect.MatrixEffectMove;
|
32 |
|
|
import org.distorted.library.effect.MatrixEffectRotate;
|
33 |
|
|
import org.distorted.library.effect.MatrixEffectScale;
|
34 |
|
|
import org.distorted.library.effect.VertexEffectSink;
|
35 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedEffects;
|
36 |
|
|
import org.distorted.library.main.DistortedFramebuffer;
|
37 |
|
|
import org.distorted.library.main.DistortedNode;
|
38 |
|
|
import org.distorted.library.main.DistortedScreen;
|
39 |
|
|
import org.distorted.library.main.Distorted;
|
40 |
107e4b72
|
Leszek Koltunski
|
import org.distorted.library.mesh.MeshCubes;
|
41 |
|
|
import org.distorted.library.mesh.MeshFlat;
|
42 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedTexture;
|
43 |
c0486401
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic;
|
44 |
59759251
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic1D;
|
45 |
|
|
import org.distorted.library.type.Static1D;
|
46 |
7589635e
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
47 |
427ab7bf
|
Leszek Koltunski
|
|
48 |
|
|
import android.graphics.Bitmap;
|
49 |
|
|
import android.graphics.BitmapFactory;
|
50 |
41a85bb7
|
Leszek Koltunski
|
import android.opengl.GLES31;
|
51 |
427ab7bf
|
Leszek Koltunski
|
import android.opengl.GLSurfaceView;
|
52 |
|
|
|
53 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
54 |
|
|
|
55 |
5ceea148
|
Leszek Koltunski
|
class ObjectTreeRenderer implements GLSurfaceView.Renderer
|
56 |
427ab7bf
|
Leszek Koltunski
|
{
|
57 |
|
|
private GLSurfaceView mView;
|
58 |
d04a4886
|
Leszek Koltunski
|
private DistortedEffects mEffects;
|
59 |
59540ba2
|
Leszek Koltunski
|
private DistortedTexture mLisaTexture, mGridTexture;
|
60 |
d218d64e
|
leszek
|
private DistortedScreen mScreen;
|
61 |
8744aa41
|
Leszek Koltunski
|
private DistortedNode mRoot;
|
62 |
5f2a53b6
|
leszek
|
private MeshFlat mMeshFlat;
|
63 |
|
|
private MeshCubes mMeshCubes;
|
64 |
392e16fd
|
Leszek Koltunski
|
private int lisaHeight, lisaWidth;
|
65 |
b5636dbf
|
leszek
|
private boolean mDepth;
|
66 |
6646c476
|
leszek
|
private Static3D mScale, mMove;
|
67 |
392e16fd
|
Leszek Koltunski
|
|
68 |
427ab7bf
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69 |
|
|
|
70 |
5ceea148
|
Leszek Koltunski
|
ObjectTreeRenderer(GLSurfaceView v)
|
71 |
427ab7bf
|
Leszek Koltunski
|
{
|
72 |
392e16fd
|
Leszek Koltunski
|
mView = v;
|
73 |
b5636dbf
|
leszek
|
mDepth = true;
|
74 |
6646c476
|
leszek
|
mScale = new Static3D(1,1,1);
|
75 |
|
|
mMove = new Static3D(0,0,0);
|
76 |
3c70973c
|
Leszek Koltunski
|
Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
|
77 |
|
|
chromaDyn.add(new Static1D(0.0f));
|
78 |
|
|
chromaDyn.add(new Static1D(0.8f));
|
79 |
|
|
|
80 |
|
|
mEffects= new DistortedEffects();
|
81 |
6646c476
|
leszek
|
mEffects.apply(new MatrixEffectMove(mMove));
|
82 |
|
|
mEffects.apply(new MatrixEffectScale(mScale));
|
83 |
3c70973c
|
Leszek Koltunski
|
mEffects.apply(new FragmentEffectChroma(chromaDyn, new Static3D(0,0,1)));
|
84 |
|
|
|
85 |
e4330c89
|
Leszek Koltunski
|
mScreen = new DistortedScreen();
|
86 |
427ab7bf
|
Leszek Koltunski
|
}
|
87 |
|
|
|
88 |
1746198f
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
89 |
|
|
|
90 |
b5636dbf
|
leszek
|
private void setDepthPriv()
|
91 |
|
|
{
|
92 |
41a85bb7
|
Leszek Koltunski
|
if( mDepth ) mRoot.glEnable (GLES31.GL_DEPTH_TEST);
|
93 |
|
|
else mRoot.glDisable(GLES31.GL_DEPTH_TEST);
|
94 |
fc695380
|
leszek
|
|
95 |
b5636dbf
|
leszek
|
mRoot.glDepthMask(mDepth);
|
96 |
fc695380
|
leszek
|
|
97 |
b5636dbf
|
leszek
|
// we can also, to save memory, delete/recreate
|
98 |
|
|
// the depth buffer each time. This is optional.
|
99 |
559da65d
|
Leszek Koltunski
|
mRoot.enableDepthStencil(mDepth ? DistortedFramebuffer.DEPTH_NO_STENCIL:DistortedFramebuffer.NO_DEPTH_NO_STENCIL);
|
100 |
b5636dbf
|
leszek
|
}
|
101 |
|
|
|
102 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
103 |
|
|
|
104 |
|
|
void setDepth(boolean depth)
|
105 |
|
|
{
|
106 |
|
|
mDepth = depth;
|
107 |
|
|
if( mRoot!=null ) setDepthPriv();
|
108 |
1746198f
|
Leszek Koltunski
|
}
|
109 |
|
|
|
110 |
427ab7bf
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111 |
|
|
|
112 |
1746198f
|
Leszek Koltunski
|
public void onDrawFrame(GL10 glUnused)
|
113 |
427ab7bf
|
Leszek Koltunski
|
{
|
114 |
fe59d375
|
Leszek Koltunski
|
mScreen.render(System.currentTimeMillis());
|
115 |
427ab7bf
|
Leszek Koltunski
|
}
|
116 |
|
|
|
117 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118 |
|
|
|
119 |
1746198f
|
Leszek Koltunski
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
120 |
427ab7bf
|
Leszek Koltunski
|
{
|
121 |
630703d1
|
leszek
|
if( (float)lisaHeight/lisaWidth > (float)height/width )
|
122 |
|
|
{
|
123 |
|
|
int w = (height*lisaWidth)/lisaHeight;
|
124 |
|
|
float factor = (float)height/lisaHeight;
|
125 |
|
|
|
126 |
6646c476
|
leszek
|
mMove.set((width-w)/2,0,0);
|
127 |
|
|
mScale.set(factor,factor,factor);
|
128 |
630703d1
|
leszek
|
}
|
129 |
|
|
else
|
130 |
|
|
{
|
131 |
|
|
int h = (width*lisaHeight)/lisaWidth;
|
132 |
|
|
float factor = (float)width/lisaWidth;
|
133 |
|
|
|
134 |
6646c476
|
leszek
|
mMove.set(0,(height-h)/2,0);
|
135 |
|
|
mScale.set(factor,factor,factor);
|
136 |
630703d1
|
leszek
|
}
|
137 |
427ab7bf
|
Leszek Koltunski
|
|
138 |
392e16fd
|
Leszek Koltunski
|
mScreen.resize(width, height);
|
139 |
427ab7bf
|
Leszek Koltunski
|
}
|
140 |
|
|
|
141 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142 |
|
|
|
143 |
1746198f
|
Leszek Koltunski
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
144 |
427ab7bf
|
Leszek Koltunski
|
{
|
145 |
3c70973c
|
Leszek Koltunski
|
// TODO
|
146 |
|
|
// This appears to be needed in case we create the app anew with NO DEPTH
|
147 |
|
|
// (i.e. we set 'no depth' and then rotate screen). Investigate.
|
148 |
41a85bb7
|
Leszek Koltunski
|
GLES31.glEnable(GLES31.GL_CULL_FACE);
|
149 |
|
|
GLES31.glCullFace(GLES31.GL_BACK);
|
150 |
|
|
GLES31.glFrontFace(GLES31.GL_CW);
|
151 |
e7a4ef16
|
Leszek Koltunski
|
|
152 |
427ab7bf
|
Leszek Koltunski
|
InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
|
153 |
c0486401
|
Leszek Koltunski
|
InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
|
154 |
427ab7bf
|
Leszek Koltunski
|
|
155 |
|
|
Bitmap bitmap1, bitmap2;
|
156 |
|
|
|
157 |
|
|
try
|
158 |
|
|
{
|
159 |
|
|
bitmap1 = BitmapFactory.decodeStream(is1);
|
160 |
|
|
bitmap2 = BitmapFactory.decodeStream(is2);
|
161 |
|
|
}
|
162 |
|
|
finally
|
163 |
|
|
{
|
164 |
|
|
try
|
165 |
|
|
{
|
166 |
|
|
is1.close();
|
167 |
|
|
is2.close();
|
168 |
|
|
}
|
169 |
|
|
catch(IOException e) { }
|
170 |
|
|
}
|
171 |
|
|
|
172 |
f6d884d5
|
Leszek Koltunski
|
lisaWidth = bitmap1.getWidth();
|
173 |
|
|
lisaHeight = bitmap1.getHeight();
|
174 |
371d99fa
|
Leszek Koltunski
|
int gridWidth = bitmap2.getWidth();
|
175 |
|
|
int gridHeight= bitmap2.getHeight();
|
176 |
f6d884d5
|
Leszek Koltunski
|
|
177 |
59540ba2
|
Leszek Koltunski
|
if( mLisaTexture==null ) mLisaTexture = new DistortedTexture(lisaWidth,lisaHeight);
|
178 |
|
|
if( mGridTexture==null ) mGridTexture = new DistortedTexture(gridWidth,gridHeight);
|
179 |
|
|
mLisaTexture.setTexture(bitmap1);
|
180 |
|
|
mGridTexture.setTexture(bitmap2);
|
181 |
371d99fa
|
Leszek Koltunski
|
DistortedEffects gridEffects = new DistortedEffects();
|
182 |
e8b6aa95
|
Leszek Koltunski
|
|
183 |
4b676b3f
|
Leszek Koltunski
|
final int GRID = 10;
|
184 |
|
|
|
185 |
5f2a53b6
|
leszek
|
if( mMeshFlat==null ) mMeshFlat = new MeshFlat(1,1);
|
186 |
58e9e190
|
leszek
|
if( mMeshCubes==null) mMeshCubes= new MeshCubes(GRID,GRID,1);
|
187 |
5f2a53b6
|
leszek
|
|
188 |
|
|
mRoot = new DistortedNode(mLisaTexture, mEffects, mMeshFlat);
|
189 |
|
|
mRoot.attach(mGridTexture,gridEffects,mMeshCubes);
|
190 |
7589635e
|
Leszek Koltunski
|
|
191 |
b5636dbf
|
leszek
|
setDepthPriv();
|
192 |
|
|
|
193 |
fe59d375
|
Leszek Koltunski
|
mScreen.detachAll();
|
194 |
|
|
mScreen.attach(mRoot);
|
195 |
|
|
|
196 |
630703d1
|
leszek
|
float factor = lisaWidth/(2.0f*gridWidth);
|
197 |
6646c476
|
leszek
|
MatrixEffectMove move = new MatrixEffectMove( new Static3D((lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2, gridWidth/(2.0f*GRID)));
|
198 |
|
|
MatrixEffectScale scale = new MatrixEffectScale( new Static3D(factor,factor,factor) );
|
199 |
|
|
gridEffects.apply(move);
|
200 |
|
|
gridEffects.apply(scale);
|
201 |
7bf107f7
|
Leszek Koltunski
|
|
202 |
c0486401
|
Leszek Koltunski
|
Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
|
203 |
|
|
rotDyn.add(new Static1D( 0));
|
204 |
|
|
rotDyn.add(new Static1D(360));
|
205 |
|
|
rotDyn.setMode(Dynamic.MODE_JUMP);
|
206 |
6646c476
|
leszek
|
MatrixEffectRotate rotate = new MatrixEffectRotate(rotDyn, new Static3D(1,0,0), new Static3D(gridWidth/2,gridHeight/2,gridWidth/(2*GRID)));
|
207 |
|
|
gridEffects.apply(rotate);
|
208 |
c0486401
|
Leszek Koltunski
|
|
209 |
|
|
Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
|
210 |
7bf107f7
|
Leszek Koltunski
|
sinkDyn.add(new Static1D(1.0f));
|
211 |
c0486401
|
Leszek Koltunski
|
sinkDyn.add(new Static1D(0.3f));
|
212 |
6646c476
|
leszek
|
VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
|
213 |
|
|
gridEffects.apply(sink);
|
214 |
59759251
|
Leszek Koltunski
|
|
215 |
885b9cca
|
leszek
|
VertexEffectSink.enable();
|
216 |
|
|
FragmentEffectChroma.enable();
|
217 |
6637d0f2
|
Leszek Koltunski
|
|
218 |
427ab7bf
|
Leszek Koltunski
|
try
|
219 |
|
|
{
|
220 |
76f9798b
|
Leszek Koltunski
|
Distorted.onCreate(mView.getContext());
|
221 |
427ab7bf
|
Leszek Koltunski
|
}
|
222 |
|
|
catch(Exception ex)
|
223 |
|
|
{
|
224 |
5ceea148
|
Leszek Koltunski
|
android.util.Log.e("ObjectTree", ex.getMessage() );
|
225 |
427ab7bf
|
Leszek Koltunski
|
}
|
226 |
|
|
}
|
227 |
|
|
}
|