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