Project

General

Profile

Download (9.11 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DRenderer.java @ 71d8ad03

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 08f92d82 Leszek Koltunski
import org.distorted.library.DistortedCubes;
31 261fe5bd Leszek Koltunski
import org.distorted.library.DistortedObject;
32 08f92d82 Leszek Koltunski
import org.distorted.library.EffectNames;
33
import org.distorted.library.EffectTypes;
34 e2d8ea1d Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
35 794e6c4f Leszek Koltunski
import org.distorted.library.type.Dynamic2D;
36 08f92d82 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
37 833685d0 Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
38 e2d8ea1d Leszek Koltunski
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static2D;
40 08f92d82 Leszek Koltunski
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42
43
import java.io.IOException;
44
import java.io.InputStream;
45
46
import javax.microedition.khronos.egl.EGLConfig;
47
import javax.microedition.khronos.opengles.GL10;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51
class Vertex3DRenderer implements GLSurfaceView.Renderer
52
{
53 794e6c4f Leszek Koltunski
    public static final int SIZE = 100;
54
55 08f92d82 Leszek Koltunski
    private GLSurfaceView mView;
56 261fe5bd Leszek Koltunski
    private static DistortedObject mObject;
57 71d8ad03 Leszek Koltunski
    private DistortedBitmap mCenter;
58 261fe5bd Leszek Koltunski
59
    private int mObjWidth, mObjHeight;
60
61 833685d0 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
62 08f92d82 Leszek Koltunski
63
    private static EffectNames[] order;
64 794e6c4f Leszek Koltunski
65
    private static Dynamic2D mCenterInter;
66 e2d8ea1d Leszek Koltunski
    private static Dynamic3D mDeformInter, mDistortInter;
67
    private static Dynamic1D mSinkInter, mSwirlInter;
68 08f92d82 Leszek Koltunski
69 e2d8ea1d Leszek Koltunski
    private static Static2D mCenterPoint;
70
    private static Static3D mDeformPoint, mDistortPoint;
71
    private static Static1D mSinkPoint, mSwirlPoint;
72 08f92d82 Leszek Koltunski
73 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
74 26b28407 Leszek Koltunski
    static int mScreenMin;
75 833685d0 Leszek Koltunski
76 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
77
78
    public static void setDeform(float x, float y, float z)
79
      {
80 e2d8ea1d Leszek Koltunski
      mDeformPoint.set(x, y, z);
81 08f92d82 Leszek Koltunski
      }
82
    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
85
    public static void setDistort(float x, float y, float z)
86
      {
87 e2d8ea1d Leszek Koltunski
      mDistortPoint.set(x, y, z);
88 08f92d82 Leszek Koltunski
      }
89
     
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
92 e2d8ea1d Leszek Koltunski
    public static void setSink(float s)
93 08f92d82 Leszek Koltunski
      {
94 e2d8ea1d Leszek Koltunski
      mSinkPoint.set(s);
95 08f92d82 Leszek Koltunski
      }
96
    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99 e2d8ea1d Leszek Koltunski
    public static void setSwirl(float s)
100 08f92d82 Leszek Koltunski
      {
101 e2d8ea1d Leszek Koltunski
      mSwirlPoint.set(s);
102 08f92d82 Leszek Koltunski
      }
103
104 794e6c4f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
    public static void setCenter(float x, float y)
107
      {
108
      mCenterPoint.set(x,y);
109
      }
110
111 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
112
113
    public static void setOrder(EffectNames[] effects)
114
      {
115
      order = effects;
116 e2d8ea1d Leszek Koltunski
      setVertexEffects();
117 08f92d82 Leszek Koltunski
      }
118
      
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121 e2d8ea1d Leszek Koltunski
    public static void setVertexEffects()
122 08f92d82 Leszek Koltunski
      {
123 261fe5bd Leszek Koltunski
      mObject.abortEffects(EffectTypes.VERTEX);
124 794e6c4f Leszek Koltunski
125 08f92d82 Leszek Koltunski
      for( int i=0; i<=order.length-1 ; i++ )
126
        {
127
        switch(order[i])
128
          {
129 794e6c4f Leszek Koltunski
          case DEFORM : mObject.deform( mDeformInter , mCenterInter) ; break;
130
          case DISTORT: mObject.distort(mDistortInter, mCenterInter) ; break;
131
          case SINK   : mObject.sink(   mSinkInter   , mCenterInter) ; break;
132
          case SWIRL  : mObject.swirl(  mSwirlInter  , mCenterInter) ; break;
133 08f92d82 Leszek Koltunski
          }
134
        }
135
      }
136
    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
139
    public Vertex3DRenderer(GLSurfaceView v)
140
      {
141
      mView = v;
142 d40cfeb2 Leszek Koltunski
143 794e6c4f Leszek Koltunski
      mObject = new DistortedCubes( Vertex3DActivity.getCols(), Vertex3DActivity.getShape(), SIZE);
144
      //mObject = new DistortedBitmap( SIZE, SIZE, 10);
145 71d8ad03 Leszek Koltunski
      mCenter = new DistortedBitmap(SIZE, SIZE, 1);
146 d40cfeb2 Leszek Koltunski
147 261fe5bd Leszek Koltunski
      mObjWidth = mObject.getWidth();
148
      mObjHeight= mObject.getHeight();
149 d40cfeb2 Leszek Koltunski
150 e2d8ea1d Leszek Koltunski
      mCenterPoint = new Static2D(0,0);
151
      mDeformPoint = new Static3D(0,0,0);
152
      mDistortPoint= new Static3D(1,1,1);
153
      mSwirlPoint  = new Static1D(0);
154
      mSinkPoint   = new Static1D(1);
155
156 794e6c4f Leszek Koltunski
      mCenterInter  = new Dynamic2D();
157 e2d8ea1d Leszek Koltunski
      mDeformInter  = new Dynamic3D();
158
      mDistortInter = new Dynamic3D();
159
      mSwirlInter   = new Dynamic1D();
160
      mSinkInter    = new Dynamic1D();
161
162 794e6c4f Leszek Koltunski
      mCenterInter.add(mCenterPoint);
163 e2d8ea1d Leszek Koltunski
      mDeformInter.add(mDeformPoint);
164
      mDistortInter.add(mDistortPoint);
165
      mSwirlInter.add(mSwirlPoint);
166
      mSinkInter.add(mSinkPoint);
167 833685d0 Leszek Koltunski
168
      mQuat1 = new Static4D(0,0,0,1);  // unity
169
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
170
171
      mQuatInt1 = new DynamicQuat(0,0.5f);
172
      mQuatInt2 = new DynamicQuat(0,0.5f);
173
174
      mQuatInt1.add(mQuat1);
175
      mQuatInt2.add(mQuat2);
176 08f92d82 Leszek Koltunski
      }
177
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
   
180
    public void onDrawFrame(GL10 glUnused) 
181
      {
182
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
183
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
184 71d8ad03 Leszek Koltunski
185
      long time = System.currentTimeMillis();
186
187
      mObject.draw(time);
188
      mCenter.draw(time);
189 08f92d82 Leszek Koltunski
      }
190
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
    
193
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
194
      {
195 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
196
197 261fe5bd Leszek Koltunski
      mObject.abortEffects(EffectTypes.MATRIX);
198 71d8ad03 Leszek Koltunski
      mCenter.abortEffects(EffectTypes.MATRIX);
199
200 d40cfeb2 Leszek Koltunski
      float factor;
201
202 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
203 d40cfeb2 Leszek Koltunski
        {
204 794e6c4f Leszek Koltunski
        factor = (0.7f*height)/mObjHeight;
205 261fe5bd Leszek Koltunski
        }
206 d40cfeb2 Leszek Koltunski
      else
207
        {
208 794e6c4f Leszek Koltunski
        factor = (0.7f*width)/mObjWidth;
209 d40cfeb2 Leszek Koltunski
        }
210
211 261fe5bd Leszek Koltunski
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
212 833685d0 Leszek Koltunski
213 71d8ad03 Leszek Koltunski
      mObject.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
214
      mObject.scale(factor);
215 261fe5bd Leszek Koltunski
      mObject.quaternion(mQuatInt1, center);
216
      mObject.quaternion(mQuatInt2, center);
217 833685d0 Leszek Koltunski
218 71d8ad03 Leszek Koltunski
      factor *= 0.5f;
219
220
      mCenter.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 10) );
221
      mCenter.scale(factor);
222
      mCenter.quaternion(mQuatInt1, center);
223
      mCenter.quaternion(mQuatInt2, center);
224
225 e2d8ea1d Leszek Koltunski
      setVertexEffects();
226 08f92d82 Leszek Koltunski
227
      Distorted.onSurfaceChanged(width, height); 
228
      }
229
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
    
232
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
233
      {
234 71d8ad03 Leszek Koltunski
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.grid);
235
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.center);
236
237
      Bitmap bitmap1,bitmap2;
238 08f92d82 Leszek Koltunski
        
239
      try 
240
        {
241 71d8ad03 Leszek Koltunski
        bitmap1 = BitmapFactory.decodeStream(is1);
242
        bitmap2 = BitmapFactory.decodeStream(is2);
243
        }
244 08f92d82 Leszek Koltunski
      finally 
245
        {
246
        try 
247
          {
248 71d8ad03 Leszek Koltunski
          is1.close();
249
          is2.close();
250 08f92d82 Leszek Koltunski
          } 
251
        catch(IOException e) { }
252
        }  
253
      
254 71d8ad03 Leszek Koltunski
      mObject.setBitmap(bitmap1);
255
      mCenter.setBitmap(bitmap2);
256
257 08f92d82 Leszek Koltunski
      try
258
        {
259
        Distorted.onSurfaceCreated(mView.getContext());
260
        }
261
      catch(Exception ex)
262
        {
263
        android.util.Log.e("Vertex3D", ex.getMessage() );
264
        }
265
      }
266
}