Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DRenderer.java @ 50ac40a6

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
    private static final int SIZE = 100;
54

    
55
    private GLSurfaceView mView;
56
    private DistortedObject mObject;
57
    private DistortedBitmap mCenter;
58
    private float mFactorCen, mFactorObj;
59

    
60
    private int mObjWidth, mObjHeight;
61

    
62
    private DynamicQuat mQuatInt1, mQuatInt2;
63

    
64
    private EffectNames[] order;
65

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

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

    
74
    Static4D mQuat1, mQuat2;
75
    int mScreenMin;
76

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

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

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

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

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

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

    
107
    public void setCenter(float x, float y)
108
      {
109
      x = (0.012f*x-0.1f)*mObjWidth;
110
      y = (0.012f*y-0.1f)*mObjHeight;
111

    
112
      mCenterPoint.set(x,y);
113
      mMovePoint.set(mFactorObj*x,mFactorObj*y,0);
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    public void setOrder(EffectNames[] effects)
119
      {
120
      order = effects;
121
      setVertexEffects();
122
      }
123
      
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
    public void setVertexEffects()
127
      {
128
      mObject.abortEffects(EffectTypes.VERTEX);
129

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

    
144
    public Vertex3DRenderer(GLSurfaceView v)
145
      {
146
      mView = v;
147

    
148
      mObject = ((Vertex3DActivity)v.getContext()).getObject();
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
      int centerSize = mCenter.getWidth();
208

    
209
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
210
        {
211
        mFactorObj = (0.70f*height)/mObjHeight;
212
        mFactorCen = (0.15f*height)/centerSize;
213
        }
214
      else
215
        {
216
        mFactorObj = (0.70f*width)/mObjWidth;
217
        mFactorCen = (0.15f*width)/centerSize;
218
        }
219

    
220
      Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
221
      float cX = act.getCenterX();
222
      float cY = act.getCenterY();
223

    
224
      cX = (0.012f*cX-0.1f)*mObjWidth;
225
      cY = (0.012f*cY-0.1f)*mObjHeight;
226

    
227
      mMovePoint.set(cX*mFactorObj,cY*mFactorObj,0);
228

    
229
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
230

    
231
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
232
      mObject.scale(mFactorObj);
233
      mObject.quaternion(mQuatInt1, rotateObj);
234
      mObject.quaternion(mQuatInt2, rotateObj);
235

    
236
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
237

    
238
      mCenter.quaternion(mQuatInt1, rotateCen);
239
      mCenter.quaternion(mQuatInt2, rotateCen);
240
      mCenter.move( new Static3D( (width -mFactorCen*centerSize-mFactorObj*mObjWidth )/2 ,
241
                                  (height-mFactorCen*centerSize-mFactorObj*mObjHeight)/2 , 10) );
242
      mCenter.move(mMoveInter);
243
      mCenter.scale(mFactorCen);
244

    
245
      setVertexEffects();
246

    
247
      Distorted.onSurfaceChanged(width, height); 
248
      }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251
    
252
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
253
      {
254
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.grid);
255
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.center);
256

    
257
      Bitmap bitmap1,bitmap2;
258
        
259
      try 
260
        {
261
        bitmap1 = BitmapFactory.decodeStream(is1);
262
        bitmap2 = BitmapFactory.decodeStream(is2);
263
        }
264
      finally 
265
        {
266
        try 
267
          {
268
          is1.close();
269
          is2.close();
270
          } 
271
        catch(IOException e) { }
272
        }  
273
      
274
      mObject.setBitmap(bitmap1);
275
      mCenter.setBitmap(bitmap2);
276

    
277
      try
278
        {
279
        Distorted.onSurfaceCreated(mView.getContext());
280
        }
281
      catch(Exception ex)
282
        {
283
        android.util.Log.e("Vertex3D", ex.getMessage() );
284
        }
285
      }
286
}
(2-2/3)