Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DRenderer.java @ 2bf60c29

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import org.distorted.library.DistortedBitmap;
30
import org.distorted.library.DistortedCubes;
31
import org.distorted.library.DistortedObject;
32
import org.distorted.library.EffectNames;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Dynamic2D;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.DynamicQuat;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static2D;
40
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
    public static final int SIZE = 100;
54

    
55
    private GLSurfaceView mView;
56
    private static DistortedObject mObject;
57
    private DistortedBitmap mCenter;
58
    private int mCols;
59

    
60
    private int mObjWidth, mObjHeight;
61

    
62
    private DynamicQuat mQuatInt1, mQuatInt2;
63

    
64
    private static EffectNames[] order;
65

    
66
    private static Dynamic2D mCenterInter;
67
    private static Dynamic3D mDeformInter, mDistortInter, mMoveInter;
68
    private static Dynamic1D mSinkInter, mSwirlInter;
69

    
70
    private static Static2D mCenterPoint;
71
    private static Static3D mDeformPoint, mDistortPoint, mMovePoint;
72
    private static Static1D mSinkPoint, mSwirlPoint;
73

    
74
    Static4D mQuat1, mQuat2;
75
    static int mScreenMin;
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
    public static void setDeform(float x, float y, float z)
80
      {
81
      mDeformPoint.set(x, y, z);
82
      }
83
    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
    public static void setDistort(float x, float y, float z)
87
      {
88
      mDistortPoint.set(x, y, z);
89
      }
90
     
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
    public static void setSink(float s)
94
      {
95
      mSinkPoint.set(s);
96
      }
97
    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
    public static void setSwirl(float s)
101
      {
102
      mSwirlPoint.set(s);
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
    public static void setCenter(float x, float y)
108
      {
109
      mCenterPoint.set(x,y);
110
      mMovePoint.set(x,y,0);
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
    public static void setOrder(EffectNames[] effects)
116
      {
117
      order = effects;
118
      setVertexEffects();
119
      }
120
      
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
    public static void setVertexEffects()
124
      {
125
      mObject.abortEffects(EffectTypes.VERTEX);
126

    
127
      for( int i=0; i<=order.length-1 ; i++ )
128
        {
129
        switch(order[i])
130
          {
131
          case DEFORM : mObject.deform( mDeformInter , mCenterInter) ; break;
132
          case DISTORT: mObject.distort(mDistortInter, mCenterInter) ; break;
133
          case SINK   : mObject.sink(   mSinkInter   , mCenterInter) ; break;
134
          case SWIRL  : mObject.swirl(  mSwirlInter  , mCenterInter) ; break;
135
          }
136
        }
137
      }
138
    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
    public Vertex3DRenderer(GLSurfaceView v)
142
      {
143
      mView = v;
144

    
145
      mCols = Vertex3DActivity.getCols();
146

    
147
      mObject = new DistortedCubes( mCols, Vertex3DActivity.getShape(), SIZE);
148
      //mObject = new DistortedBitmap( SIZE, SIZE, 10);
149
      mCenter = new DistortedBitmap(SIZE, SIZE, 1);
150

    
151
      mObjWidth = mObject.getWidth();
152
      mObjHeight= mObject.getHeight();
153

    
154
      mCenterPoint = new Static2D(0,0);
155
      mMovePoint   = new Static3D(0,0,0);
156
      mDeformPoint = new Static3D(0,0,0);
157
      mDistortPoint= new Static3D(1,1,1);
158
      mSwirlPoint  = new Static1D(0);
159
      mSinkPoint   = new Static1D(1);
160

    
161
      mCenterInter  = new Dynamic2D();
162
      mDeformInter  = new Dynamic3D();
163
      mDistortInter = new Dynamic3D();
164
      mMoveInter    = new Dynamic3D();
165
      mSwirlInter   = new Dynamic1D();
166
      mSinkInter    = new Dynamic1D();
167

    
168
      mCenterInter.add(mCenterPoint);
169
      mMoveInter.add(mMovePoint);
170
      mDeformInter.add(mDeformPoint);
171
      mDistortInter.add(mDistortPoint);
172
      mSwirlInter.add(mSwirlPoint);
173
      mSinkInter.add(mSinkPoint);
174

    
175
      mQuat1 = new Static4D(0,0,0,1);  // unity
176
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
177

    
178
      mQuatInt1 = new DynamicQuat(0,0.5f);
179
      mQuatInt2 = new DynamicQuat(0,0.5f);
180

    
181
      mQuatInt1.add(mQuat1);
182
      mQuatInt2.add(mQuat2);
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
   
187
    public void onDrawFrame(GL10 glUnused) 
188
      {
189
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
190
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
191

    
192
      long time = System.currentTimeMillis();
193

    
194
      mObject.draw(time);
195
      mCenter.draw(time);
196
      }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199
    
200
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
201
      {
202
      mScreenMin = width<height ? width:height;
203

    
204
      mObject.abortEffects(EffectTypes.MATRIX);
205
      mCenter.abortEffects(EffectTypes.MATRIX);
206

    
207
      float factorObj;
208

    
209
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
210
        {
211
        factorObj = (0.7f*height)/mObjHeight;
212
        }
213
      else
214
        {
215
        factorObj = (0.7f*width)/mObjWidth;
216
        }
217

    
218
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
219

    
220
      mObject.move( new Static3D( (width-factorObj*mObjWidth)/2 , (height-factorObj*mObjHeight)/2 , 0) );
221
      mObject.scale(factorObj);
222
      mObject.quaternion(mQuatInt1, rotateObj);
223
      mObject.quaternion(mQuatInt2, rotateObj);
224

    
225
      int centerSize = mCenter.getWidth();
226
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
227

    
228
      float factorCen = (float)(mCols*SIZE) / (5*centerSize);  // make the 'center' bitmap 1/5 of the Object's width
229

    
230
      mCenter.quaternion(mQuatInt1, rotateCen);
231
      mCenter.quaternion(mQuatInt2, rotateCen);
232
      mCenter.move( new Static3D( (width -factorCen*centerSize-factorObj*mObjWidth )/2 ,
233
                                  (height-factorCen*centerSize-factorObj*mObjHeight)/2 , 10) );
234
      mCenter.move(mMoveInter);
235
      mCenter.scale(factorCen);
236

    
237
      setVertexEffects();
238

    
239
      Distorted.onSurfaceChanged(width, height); 
240
      }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243
    
244
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
245
      {
246
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.grid);
247
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.center);
248

    
249
      Bitmap bitmap1,bitmap2;
250
        
251
      try 
252
        {
253
        bitmap1 = BitmapFactory.decodeStream(is1);
254
        bitmap2 = BitmapFactory.decodeStream(is2);
255
        }
256
      finally 
257
        {
258
        try 
259
          {
260
          is1.close();
261
          is2.close();
262
          } 
263
        catch(IOException e) { }
264
        }  
265
      
266
      mObject.setBitmap(bitmap1);
267
      mCenter.setBitmap(bitmap2);
268

    
269
      try
270
        {
271
        Distorted.onSurfaceCreated(mView.getContext());
272
        }
273
      catch(Exception ex)
274
        {
275
        android.util.Log.e("Vertex3D", ex.getMessage() );
276
        }
277
      }
278
}
(2-2/3)