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 |
5068fa06
|
Leszek Koltunski
|
package org.distorted.examples.cubes;
|
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 |
37b324c4
|
Leszek Koltunski
|
import org.distorted.library.effect.MatrixEffectMove;
|
31 |
|
|
import org.distorted.library.effect.MatrixEffectQuaternion;
|
32 |
|
|
import org.distorted.library.effect.MatrixEffectScale;
|
33 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.DistortedEffects;
|
34 |
|
|
import org.distorted.library.main.DistortedScreen;
|
35 |
|
|
import org.distorted.library.main.DistortedTexture;
|
36 |
|
|
import org.distorted.library.main.MeshObject;
|
37 |
7589635e
|
Leszek Koltunski
|
import org.distorted.library.type.DynamicQuat;
|
38 |
|
|
import org.distorted.library.type.Static4D;
|
39 |
|
|
import org.distorted.library.type.Static3D;
|
40 |
01782e85
|
Leszek Koltunski
|
import org.distorted.library.main.Distorted;
|
41 |
427ab7bf
|
Leszek Koltunski
|
|
42 |
|
|
import android.graphics.Bitmap;
|
43 |
|
|
import android.graphics.BitmapFactory;
|
44 |
|
|
import android.opengl.GLSurfaceView;
|
45 |
|
|
|
46 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47 |
|
|
|
48 |
|
|
class CubesRenderer implements GLSurfaceView.Renderer
|
49 |
|
|
{
|
50 |
|
|
private GLSurfaceView mView;
|
51 |
f6d884d5
|
Leszek Koltunski
|
private DistortedTexture mTexture;
|
52 |
d04a4886
|
Leszek Koltunski
|
private DistortedEffects mEffects;
|
53 |
b01acdaf
|
Leszek Koltunski
|
private MeshObject mMesh;
|
54 |
d218d64e
|
leszek
|
private DistortedScreen mScreen;
|
55 |
8a99c681
|
Leszek Koltunski
|
private int mObjWidth, mObjHeight, mObjDepth;
|
56 |
37b324c4
|
Leszek Koltunski
|
private Static3D mMove, mScale, mCenter;
|
57 |
261fe5bd
|
Leszek Koltunski
|
|
58 |
7589635e
|
Leszek Koltunski
|
Static4D mQuat1, mQuat2;
|
59 |
427ab7bf
|
Leszek Koltunski
|
int mScreenMin;
|
60 |
|
|
|
61 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
62 |
|
|
|
63 |
2b261a18
|
Leszek Koltunski
|
CubesRenderer(GLSurfaceView v)
|
64 |
427ab7bf
|
Leszek Koltunski
|
{
|
65 |
|
|
mView = v;
|
66 |
261fe5bd
|
Leszek Koltunski
|
|
67 |
37b324c4
|
Leszek Koltunski
|
mMove = new Static3D(0,0,0);
|
68 |
|
|
mScale= new Static3D(1,1,1);
|
69 |
|
|
mCenter=new Static3D(0,0,0);
|
70 |
|
|
|
71 |
e8b6aa95
|
Leszek Koltunski
|
CubesActivity act = (CubesActivity)v.getContext();
|
72 |
|
|
|
73 |
f6d884d5
|
Leszek Koltunski
|
mTexture = act.getTexture();
|
74 |
7fca6741
|
Leszek Koltunski
|
mMesh = act.getMesh();
|
75 |
261fe5bd
|
Leszek Koltunski
|
|
76 |
f6d884d5
|
Leszek Koltunski
|
mObjWidth = mTexture.getWidth();
|
77 |
|
|
mObjHeight= mTexture.getHeight();
|
78 |
8a99c681
|
Leszek Koltunski
|
mObjDepth = mTexture.getDepth(mMesh);
|
79 |
261fe5bd
|
Leszek Koltunski
|
|
80 |
7589635e
|
Leszek Koltunski
|
mQuat1 = new Static4D(0,0,0,1); // unity
|
81 |
|
|
mQuat2 = new Static4D(0,0,0,1); // quaternions
|
82 |
427ab7bf
|
Leszek Koltunski
|
|
83 |
37b324c4
|
Leszek Koltunski
|
DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
|
84 |
|
|
DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
|
85 |
|
|
|
86 |
|
|
quatInt1.add(mQuat1);
|
87 |
|
|
quatInt2.add(mQuat2);
|
88 |
833685d0
|
Leszek Koltunski
|
|
89 |
37b324c4
|
Leszek Koltunski
|
mEffects = new DistortedEffects();
|
90 |
|
|
mEffects.apply( new MatrixEffectMove(mMove) );
|
91 |
|
|
mEffects.apply( new MatrixEffectScale(mScale));
|
92 |
|
|
mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) );
|
93 |
|
|
mEffects.apply( new MatrixEffectQuaternion(quatInt2, mCenter) );
|
94 |
392e16fd
|
Leszek Koltunski
|
|
95 |
e4330c89
|
Leszek Koltunski
|
mScreen = new DistortedScreen();
|
96 |
427ab7bf
|
Leszek Koltunski
|
}
|
97 |
|
|
|
98 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
99 |
|
|
|
100 |
|
|
public void onDrawFrame(GL10 glUnused)
|
101 |
|
|
{
|
102 |
fe59d375
|
Leszek Koltunski
|
mScreen.render( System.currentTimeMillis() );
|
103 |
427ab7bf
|
Leszek Koltunski
|
}
|
104 |
|
|
|
105 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106 |
|
|
|
107 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
108 |
|
|
{
|
109 |
|
|
mScreenMin = width<height ? width:height;
|
110 |
37b324c4
|
Leszek Koltunski
|
float factor = ( width*mObjHeight > height*mObjWidth ) ? (0.75f*height)/mObjHeight : (0.75f*width)/mObjWidth;
|
111 |
|
|
mMove.set( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0);
|
112 |
|
|
mScale.set(factor,factor,factor);
|
113 |
|
|
mCenter.set( (float)mObjWidth/2, (float)mObjHeight/2, -(float)mObjDepth/2 );
|
114 |
392e16fd
|
Leszek Koltunski
|
mScreen.resize(width, height);
|
115 |
427ab7bf
|
Leszek Koltunski
|
}
|
116 |
|
|
|
117 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
118 |
|
|
|
119 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
120 |
|
|
{
|
121 |
|
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
|
122 |
|
|
Bitmap bitmap;
|
123 |
|
|
|
124 |
|
|
try
|
125 |
|
|
{
|
126 |
|
|
bitmap = BitmapFactory.decodeStream(is);
|
127 |
|
|
}
|
128 |
|
|
finally
|
129 |
|
|
{
|
130 |
|
|
try
|
131 |
|
|
{
|
132 |
|
|
is.close();
|
133 |
|
|
}
|
134 |
|
|
catch(IOException e) { }
|
135 |
|
|
}
|
136 |
|
|
|
137 |
f6d884d5
|
Leszek Koltunski
|
mTexture.setTexture(bitmap);
|
138 |
fe59d375
|
Leszek Koltunski
|
|
139 |
|
|
mScreen.detachAll();
|
140 |
|
|
mScreen.attach(mTexture,mEffects,mMesh);
|
141 |
|
|
|
142 |
427ab7bf
|
Leszek Koltunski
|
try
|
143 |
|
|
{
|
144 |
76f9798b
|
Leszek Koltunski
|
Distorted.onCreate(mView.getContext());
|
145 |
427ab7bf
|
Leszek Koltunski
|
}
|
146 |
|
|
catch(Exception ex)
|
147 |
|
|
{
|
148 |
|
|
android.util.Log.e("Cubes", ex.getMessage() );
|
149 |
|
|
}
|
150 |
|
|
}
|
151 |
|
|
}
|