| 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 |
95593730
|
Leszek Koltunski
|
import org.distorted.library.EffectTypes;
|
| 31 |
7589635e
|
Leszek Koltunski
|
import org.distorted.library.type.DynamicQuat;
|
| 32 |
5068fa06
|
Leszek Koltunski
|
import org.distorted.library.DistortedCubes;
|
| 33 |
7589635e
|
Leszek Koltunski
|
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 |
bc0a685b
|
Leszek Koltunski
|
private static final int SIZE = 100;
|
| 47 |
427ab7bf
|
Leszek Koltunski
|
|
| 48 |
bc0a685b
|
Leszek Koltunski
|
private int mCols, mRows;
|
| 49 |
427ab7bf
|
Leszek Koltunski
|
|
| 50 |
|
|
private GLSurfaceView mView;
|
| 51 |
|
|
private DistortedCubes mCubes;
|
| 52 |
7589635e
|
Leszek Koltunski
|
private DynamicQuat mQuatInt1, mQuatInt2;
|
| 53 |
427ab7bf
|
Leszek Koltunski
|
|
| 54 |
7589635e
|
Leszek Koltunski
|
Static4D mQuat1, mQuat2;
|
| 55 |
427ab7bf
|
Leszek Koltunski
|
int mScreenMin;
|
| 56 |
|
|
|
| 57 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 58 |
|
|
|
| 59 |
|
|
public CubesRenderer(GLSurfaceView v)
|
| 60 |
|
|
{
|
| 61 |
|
|
mView = v;
|
| 62 |
|
|
|
| 63 |
|
|
String shape = CubesActivity.getShape();
|
| 64 |
|
|
|
| 65 |
|
|
mCols = CubesActivity.getCols();
|
| 66 |
|
|
mRows = shape.length() / mCols;
|
| 67 |
|
|
|
| 68 |
|
|
mCubes = new DistortedCubes( mCols, shape, SIZE);
|
| 69 |
|
|
|
| 70 |
7589635e
|
Leszek Koltunski
|
mQuat1 = new Static4D(0,0,0,1); // unity
|
| 71 |
|
|
mQuat2 = new Static4D(0,0,0,1); // quaternions
|
| 72 |
427ab7bf
|
Leszek Koltunski
|
|
| 73 |
7589635e
|
Leszek Koltunski
|
mQuatInt1 = new DynamicQuat();
|
| 74 |
|
|
mQuatInt2 = new DynamicQuat();
|
| 75 |
427ab7bf
|
Leszek Koltunski
|
mQuatInt1.setDuration(0);
|
| 76 |
|
|
mQuatInt2.setDuration(0);
|
| 77 |
|
|
mQuatInt1.setCount(0.5f);
|
| 78 |
|
|
mQuatInt2.setCount(0.5f);
|
| 79 |
|
|
|
| 80 |
|
|
mQuatInt1.add(mQuat1);
|
| 81 |
|
|
mQuatInt2.add(mQuat2);
|
| 82 |
|
|
}
|
| 83 |
|
|
|
| 84 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 85 |
|
|
|
| 86 |
|
|
public void onDrawFrame(GL10 glUnused)
|
| 87 |
|
|
{
|
| 88 |
|
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
| 89 |
|
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
| 90 |
|
|
|
| 91 |
|
|
mCubes.draw(System.currentTimeMillis());
|
| 92 |
|
|
}
|
| 93 |
|
|
|
| 94 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 95 |
|
|
|
| 96 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
| 97 |
|
|
{
|
| 98 |
|
|
mScreenMin = width<height ? width:height;
|
| 99 |
|
|
|
| 100 |
a8c3ada7
|
Leszek Koltunski
|
mCubes.abortEffects(EffectTypes.MATRIX);
|
| 101 |
427ab7bf
|
Leszek Koltunski
|
|
| 102 |
|
|
if( mRows/mCols > height/width )
|
| 103 |
|
|
{
|
| 104 |
|
|
int w = (height*mRows)/mCols;
|
| 105 |
7589635e
|
Leszek Koltunski
|
float factor = (float)height/(mRows*SIZE);
|
| 106 |
|
|
|
| 107 |
|
|
mCubes.move( new Static3D((width-w)/2,0,0) );
|
| 108 |
|
|
mCubes.scale( new Static3D(factor,factor,factor) );
|
| 109 |
427ab7bf
|
Leszek Koltunski
|
}
|
| 110 |
|
|
else
|
| 111 |
|
|
{
|
| 112 |
|
|
int h = (width*mRows)/mCols;
|
| 113 |
7589635e
|
Leszek Koltunski
|
float factor = (float)width/(mCols*SIZE);
|
| 114 |
|
|
|
| 115 |
|
|
mCubes.move( new Static3D(0,(height-h)/2,0) );
|
| 116 |
|
|
mCubes.scale( new Static3D(factor,factor,factor) );
|
| 117 |
427ab7bf
|
Leszek Koltunski
|
}
|
| 118 |
|
|
|
| 119 |
7589635e
|
Leszek Koltunski
|
Static3D center = new Static3D(mCols*SIZE/2,mRows*SIZE/2, 0);
|
| 120 |
427ab7bf
|
Leszek Koltunski
|
|
| 121 |
|
|
mCubes.quaternion(center, mQuatInt1);
|
| 122 |
|
|
mCubes.quaternion(center, mQuatInt2);
|
| 123 |
|
|
|
| 124 |
|
|
Distorted.onSurfaceChanged(width, height);
|
| 125 |
|
|
}
|
| 126 |
|
|
|
| 127 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 128 |
|
|
|
| 129 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
| 130 |
|
|
{
|
| 131 |
|
|
InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
|
| 132 |
|
|
Bitmap bitmap;
|
| 133 |
|
|
|
| 134 |
|
|
try
|
| 135 |
|
|
{
|
| 136 |
|
|
bitmap = BitmapFactory.decodeStream(is);
|
| 137 |
|
|
}
|
| 138 |
|
|
finally
|
| 139 |
|
|
{
|
| 140 |
|
|
try
|
| 141 |
|
|
{
|
| 142 |
|
|
is.close();
|
| 143 |
|
|
}
|
| 144 |
|
|
catch(IOException e) { }
|
| 145 |
|
|
}
|
| 146 |
|
|
|
| 147 |
|
|
mCubes.setBitmap(bitmap);
|
| 148 |
|
|
|
| 149 |
|
|
try
|
| 150 |
|
|
{
|
| 151 |
ed1c0b33
|
Leszek Koltunski
|
Distorted.onSurfaceCreated(mView.getContext());
|
| 152 |
427ab7bf
|
Leszek Koltunski
|
}
|
| 153 |
|
|
catch(Exception ex)
|
| 154 |
|
|
{
|
| 155 |
|
|
android.util.Log.e("Cubes", ex.getMessage() );
|
| 156 |
|
|
}
|
| 157 |
|
|
}
|
| 158 |
|
|
}
|