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