| 1 |
08f92d82
|
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 |
|
|
|
| 20 |
|
|
package org.distorted.examples.vertex3d;
|
| 21 |
|
|
|
| 22 |
|
|
import android.graphics.Bitmap;
|
| 23 |
|
|
import android.graphics.BitmapFactory;
|
| 24 |
|
|
import android.opengl.GLES20;
|
| 25 |
|
|
import android.opengl.GLSurfaceView;
|
| 26 |
|
|
|
| 27 |
|
|
import org.distorted.examples.R;
|
| 28 |
|
|
import org.distorted.library.Distorted;
|
| 29 |
794e6c4f
|
Leszek Koltunski
|
import org.distorted.library.DistortedBitmap;
|
| 30 |
261fe5bd
|
Leszek Koltunski
|
import org.distorted.library.DistortedObject;
|
| 31 |
08f92d82
|
Leszek Koltunski
|
import org.distorted.library.EffectTypes;
|
| 32 |
950511ed
|
Leszek Koltunski
|
import org.distorted.library.type.Dynamic3D;
|
| 33 |
833685d0
|
Leszek Koltunski
|
import org.distorted.library.type.DynamicQuat;
|
| 34 |
08f92d82
|
Leszek Koltunski
|
import org.distorted.library.type.Static3D;
|
| 35 |
|
|
import org.distorted.library.type.Static4D;
|
| 36 |
|
|
|
| 37 |
|
|
import java.io.IOException;
|
| 38 |
|
|
import java.io.InputStream;
|
| 39 |
|
|
|
| 40 |
|
|
import javax.microedition.khronos.egl.EGLConfig;
|
| 41 |
|
|
import javax.microedition.khronos.opengles.GL10;
|
| 42 |
|
|
|
| 43 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 44 |
|
|
|
| 45 |
|
|
class Vertex3DRenderer implements GLSurfaceView.Renderer
|
| 46 |
|
|
{
|
| 47 |
|
|
private GLSurfaceView mView;
|
| 48 |
d2337a3a
|
Leszek Koltunski
|
private DistortedObject mObject;
|
| 49 |
56cbe1cf
|
Leszek Koltunski
|
private DistortedBitmap mBackground;
|
| 50 |
950511ed
|
Leszek Koltunski
|
private DistortedBitmap mCenter;
|
| 51 |
6f779cd4
|
Leszek Koltunski
|
private DistortedBitmap mRegion;
|
| 52 |
261fe5bd
|
Leszek Koltunski
|
private int mObjWidth, mObjHeight;
|
| 53 |
833685d0
|
Leszek Koltunski
|
private DynamicQuat mQuatInt1, mQuatInt2;
|
| 54 |
950511ed
|
Leszek Koltunski
|
|
| 55 |
24991bc2
|
Leszek Koltunski
|
private Dynamic3D mCenterInter, mRegionInter;
|
| 56 |
|
|
private Static3D mCenterPoint, mRegionPoint;
|
| 57 |
6f779cd4
|
Leszek Koltunski
|
private Dynamic3D mRegionScaleInter;
|
| 58 |
|
|
private Static3D mRegionScalePoint;
|
| 59 |
|
|
|
| 60 |
|
|
private float mFactorObj, mFactorReg;
|
| 61 |
950511ed
|
Leszek Koltunski
|
|
| 62 |
833685d0
|
Leszek Koltunski
|
Static4D mQuat1, mQuat2;
|
| 63 |
d2337a3a
|
Leszek Koltunski
|
int mScreenMin;
|
| 64 |
833685d0
|
Leszek Koltunski
|
|
| 65 |
08f92d82
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 66 |
|
|
|
| 67 |
|
|
public Vertex3DRenderer(GLSurfaceView v)
|
| 68 |
|
|
{
|
| 69 |
|
|
mView = v;
|
| 70 |
d40cfeb2
|
Leszek Koltunski
|
|
| 71 |
56cbe1cf
|
Leszek Koltunski
|
mObject = ((Vertex3DActivity)v.getContext()).getObject();
|
| 72 |
|
|
mBackground = new DistortedBitmap(100, 100, 1);
|
| 73 |
950511ed
|
Leszek Koltunski
|
mCenter = new DistortedBitmap(100, 100, 1);
|
| 74 |
6f779cd4
|
Leszek Koltunski
|
mRegion = new DistortedBitmap(100, 100, 1);
|
| 75 |
d40cfeb2
|
Leszek Koltunski
|
|
| 76 |
261fe5bd
|
Leszek Koltunski
|
mObjWidth = mObject.getWidth();
|
| 77 |
|
|
mObjHeight= mObject.getHeight();
|
| 78 |
d40cfeb2
|
Leszek Koltunski
|
|
| 79 |
833685d0
|
Leszek Koltunski
|
mQuat1 = new Static4D(0,0,0,1); // unity
|
| 80 |
|
|
mQuat2 = new Static4D(0,0,0,1); // quaternions
|
| 81 |
|
|
|
| 82 |
|
|
mQuatInt1 = new DynamicQuat(0,0.5f);
|
| 83 |
|
|
mQuatInt2 = new DynamicQuat(0,0.5f);
|
| 84 |
|
|
|
| 85 |
|
|
mQuatInt1.add(mQuat1);
|
| 86 |
|
|
mQuatInt2.add(mQuat2);
|
| 87 |
950511ed
|
Leszek Koltunski
|
|
| 88 |
24991bc2
|
Leszek Koltunski
|
mCenterInter= new Dynamic3D();
|
| 89 |
|
|
mCenterPoint= new Static3D(0,0,0);
|
| 90 |
|
|
mCenterInter.add(mCenterPoint);
|
| 91 |
|
|
|
| 92 |
|
|
mRegionInter= new Dynamic3D();
|
| 93 |
|
|
mRegionPoint= new Static3D(0,0,0);
|
| 94 |
|
|
mRegionInter.add(mRegionPoint);
|
| 95 |
6f779cd4
|
Leszek Koltunski
|
|
| 96 |
|
|
mRegionScaleInter = new Dynamic3D();
|
| 97 |
|
|
mRegionScalePoint = new Static3D(0,0,0);
|
| 98 |
|
|
mRegionScaleInter.add(mRegionScalePoint);
|
| 99 |
950511ed
|
Leszek Koltunski
|
}
|
| 100 |
|
|
|
| 101 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 102 |
|
|
|
| 103 |
|
|
public void setCenter(float x, float y)
|
| 104 |
|
|
{
|
| 105 |
24991bc2
|
Leszek Koltunski
|
mCenterPoint.set(mFactorObj*x,mFactorObj*y,0);
|
| 106 |
08f92d82
|
Leszek Koltunski
|
}
|
| 107 |
|
|
|
| 108 |
6f779cd4
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 109 |
|
|
|
| 110 |
|
|
public void setRegion(float x, float y, float r)
|
| 111 |
|
|
{
|
| 112 |
17600407
|
Leszek Koltunski
|
mFactorReg = 2*mFactorObj*r/mRegion.getWidth();
|
| 113 |
24991bc2
|
Leszek Koltunski
|
mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
|
| 114 |
9ae05f6c
|
Leszek Koltunski
|
mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
|
| 115 |
6f779cd4
|
Leszek Koltunski
|
}
|
| 116 |
|
|
|
| 117 |
08f92d82
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 118 |
56cbe1cf
|
Leszek Koltunski
|
|
| 119 |
|
|
public void onDrawFrame(GL10 glUnused)
|
| 120 |
08f92d82
|
Leszek Koltunski
|
{
|
| 121 |
|
|
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
| 122 |
|
|
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
|
| 123 |
71d8ad03
|
Leszek Koltunski
|
|
| 124 |
|
|
long time = System.currentTimeMillis();
|
| 125 |
|
|
|
| 126 |
56cbe1cf
|
Leszek Koltunski
|
mBackground.draw(time);
|
| 127 |
71d8ad03
|
Leszek Koltunski
|
mObject.draw(time);
|
| 128 |
950511ed
|
Leszek Koltunski
|
mCenter.draw(time);
|
| 129 |
24991bc2
|
Leszek Koltunski
|
|
| 130 |
|
|
if( Vertex3DActivity.supportsRegion() ) mRegion.draw(time);
|
| 131 |
08f92d82
|
Leszek Koltunski
|
}
|
| 132 |
|
|
|
| 133 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 134 |
56cbe1cf
|
Leszek Koltunski
|
|
| 135 |
|
|
public void onSurfaceChanged(GL10 glUnused, int width, int height)
|
| 136 |
08f92d82
|
Leszek Koltunski
|
{
|
| 137 |
833685d0
|
Leszek Koltunski
|
mScreenMin = width<height ? width:height;
|
| 138 |
|
|
|
| 139 |
261fe5bd
|
Leszek Koltunski
|
mObject.abortEffects(EffectTypes.MATRIX);
|
| 140 |
56cbe1cf
|
Leszek Koltunski
|
mBackground.abortEffects(EffectTypes.MATRIX);
|
| 141 |
950511ed
|
Leszek Koltunski
|
mCenter.abortEffects(EffectTypes.MATRIX);
|
| 142 |
6f779cd4
|
Leszek Koltunski
|
mRegion.abortEffects(EffectTypes.MATRIX);
|
| 143 |
950511ed
|
Leszek Koltunski
|
|
| 144 |
|
|
float factorCen;
|
| 145 |
|
|
int centerSize = mCenter.getWidth();
|
| 146 |
6f779cd4
|
Leszek Koltunski
|
int regionSize = mRegion.getWidth();
|
| 147 |
|
|
|
| 148 |
261fe5bd
|
Leszek Koltunski
|
if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
|
| 149 |
d40cfeb2
|
Leszek Koltunski
|
{
|
| 150 |
950511ed
|
Leszek Koltunski
|
mFactorObj = (0.80f*height)/mObjHeight;
|
| 151 |
|
|
factorCen = (0.08f*height)/centerSize;
|
| 152 |
261fe5bd
|
Leszek Koltunski
|
}
|
| 153 |
d40cfeb2
|
Leszek Koltunski
|
else
|
| 154 |
|
|
{
|
| 155 |
950511ed
|
Leszek Koltunski
|
mFactorObj = (0.80f*width)/mObjWidth;
|
| 156 |
|
|
factorCen = (0.08f*width)/centerSize;
|
| 157 |
d40cfeb2
|
Leszek Koltunski
|
}
|
| 158 |
|
|
|
| 159 |
950511ed
|
Leszek Koltunski
|
Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
|
| 160 |
24991bc2
|
Leszek Koltunski
|
mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
|
| 161 |
|
|
mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
|
| 162 |
9ae05f6c
|
Leszek Koltunski
|
|
| 163 |
17600407
|
Leszek Koltunski
|
mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
|
| 164 |
9ae05f6c
|
Leszek Koltunski
|
mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
|
| 165 |
950511ed
|
Leszek Koltunski
|
|
| 166 |
2bf60c29
|
Leszek Koltunski
|
Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
|
| 167 |
|
|
|
| 168 |
950511ed
|
Leszek Koltunski
|
mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
|
| 169 |
|
|
mObject.scale(mFactorObj);
|
| 170 |
2bf60c29
|
Leszek Koltunski
|
mObject.quaternion(mQuatInt1, rotateObj);
|
| 171 |
|
|
mObject.quaternion(mQuatInt2, rotateObj);
|
| 172 |
833685d0
|
Leszek Koltunski
|
|
| 173 |
950511ed
|
Leszek Koltunski
|
Static3D rotateCen = new Static3D(width/2,height/2, 0);
|
| 174 |
|
|
|
| 175 |
|
|
mCenter.quaternion(mQuatInt1, rotateCen);
|
| 176 |
|
|
mCenter.quaternion(mQuatInt2, rotateCen);
|
| 177 |
|
|
|
| 178 |
|
|
mCenter.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
|
| 179 |
|
|
(height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 10) );
|
| 180 |
24991bc2
|
Leszek Koltunski
|
mCenter.move(mCenterInter);
|
| 181 |
950511ed
|
Leszek Koltunski
|
mCenter.scale(factorCen);
|
| 182 |
|
|
|
| 183 |
6f779cd4
|
Leszek Koltunski
|
mRegion.quaternion(mQuatInt1, rotateCen);
|
| 184 |
|
|
mRegion.quaternion(mQuatInt2, rotateCen);
|
| 185 |
|
|
|
| 186 |
9ae05f6c
|
Leszek Koltunski
|
mRegion.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
|
| 187 |
|
|
(height-mFactorObj*mObjHeight)/2 , 12) );
|
| 188 |
24991bc2
|
Leszek Koltunski
|
mRegion.move(mCenterInter);
|
| 189 |
|
|
mRegion.move(mRegionInter);
|
| 190 |
6f779cd4
|
Leszek Koltunski
|
mRegion.scale(mRegionScaleInter);
|
| 191 |
9ae05f6c
|
Leszek Koltunski
|
mRegion.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
|
| 192 |
6f779cd4
|
Leszek Koltunski
|
|
| 193 |
56cbe1cf
|
Leszek Koltunski
|
int backgroundSize = mBackground.getWidth();
|
| 194 |
|
|
float factorBackX = ((float)width)/backgroundSize;
|
| 195 |
|
|
float factorBackY = ((float)height)/backgroundSize;
|
| 196 |
833685d0
|
Leszek Koltunski
|
|
| 197 |
950511ed
|
Leszek Koltunski
|
mBackground.move(new Static3D( -width/2, -height/2,-mFactorObj*(mObjWidth+mObjHeight)/2) );
|
| 198 |
56cbe1cf
|
Leszek Koltunski
|
mBackground.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
|
| 199 |
71d8ad03
|
Leszek Koltunski
|
|
| 200 |
56cbe1cf
|
Leszek Koltunski
|
Distorted.onSurfaceChanged(width, height);
|
| 201 |
08f92d82
|
Leszek Koltunski
|
}
|
| 202 |
|
|
|
| 203 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
| 204 |
56cbe1cf
|
Leszek Koltunski
|
|
| 205 |
|
|
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
|
| 206 |
08f92d82
|
Leszek Koltunski
|
{
|
| 207 |
bcc8e016
|
Leszek Koltunski
|
Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
|
| 208 |
|
|
|
| 209 |
9167cfd4
|
Leszek Koltunski
|
InputStream is1 = act.getResources().openRawResource(R.raw.water);
|
| 210 |
|
|
InputStream is2 = act.getResources().openRawResource(R.raw.center);
|
| 211 |
|
|
InputStream is3 = act.getResources().openRawResource(R.raw.region);
|
| 212 |
71d8ad03
|
Leszek Koltunski
|
|
| 213 |
9167cfd4
|
Leszek Koltunski
|
Bitmap bitmap1,bitmap2,bitmap3;
|
| 214 |
08f92d82
|
Leszek Koltunski
|
|
| 215 |
|
|
try
|
| 216 |
|
|
{
|
| 217 |
71d8ad03
|
Leszek Koltunski
|
bitmap1 = BitmapFactory.decodeStream(is1);
|
| 218 |
|
|
bitmap2 = BitmapFactory.decodeStream(is2);
|
| 219 |
950511ed
|
Leszek Koltunski
|
bitmap3 = BitmapFactory.decodeStream(is3);
|
| 220 |
71d8ad03
|
Leszek Koltunski
|
}
|
| 221 |
08f92d82
|
Leszek Koltunski
|
finally
|
| 222 |
|
|
{
|
| 223 |
|
|
try
|
| 224 |
|
|
{
|
| 225 |
71d8ad03
|
Leszek Koltunski
|
is1.close();
|
| 226 |
|
|
is2.close();
|
| 227 |
950511ed
|
Leszek Koltunski
|
is3.close();
|
| 228 |
9167cfd4
|
Leszek Koltunski
|
}
|
| 229 |
08f92d82
|
Leszek Koltunski
|
catch(IOException e) { }
|
| 230 |
|
|
}
|
| 231 |
|
|
|
| 232 |
9167cfd4
|
Leszek Koltunski
|
mObject.setBitmap( act.getBitmap() );
|
| 233 |
|
|
mBackground.setBitmap(bitmap1);
|
| 234 |
|
|
mCenter.setBitmap(bitmap2);
|
| 235 |
|
|
mRegion.setBitmap(bitmap3);
|
| 236 |
950511ed
|
Leszek Koltunski
|
|
| 237 |
08f92d82
|
Leszek Koltunski
|
try
|
| 238 |
|
|
{
|
| 239 |
|
|
Distorted.onSurfaceCreated(mView.getContext());
|
| 240 |
|
|
}
|
| 241 |
|
|
catch(Exception ex)
|
| 242 |
|
|
{
|
| 243 |
|
|
android.util.Log.e("Vertex3D", ex.getMessage() );
|
| 244 |
|
|
}
|
| 245 |
|
|
}
|
| 246 |
56cbe1cf
|
Leszek Koltunski
|
}
|