Project

General

Profile

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

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

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

    
59
    private int mObjWidth, mObjHeight;
60

    
61
    private DynamicQuat mQuatInt1, mQuatInt2;
62

    
63
    private static EffectNames[] order;
64

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

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

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

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

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

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

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

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

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

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

    
121
    public static void setVertexEffects()
122
      {
123
      mObject.abortEffects(EffectTypes.VERTEX);
124

    
125
      for( int i=0; i<=order.length-1 ; i++ )
126
        {
127
        switch(order[i])
128
          {
129
          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
          }
134
        }
135
      }
136
    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
    public Vertex3DRenderer(GLSurfaceView v)
140
      {
141
      mView = v;
142

    
143
      mObject = new DistortedCubes( Vertex3DActivity.getCols(), Vertex3DActivity.getShape(), SIZE);
144
      //mObject = new DistortedBitmap( SIZE, SIZE, 10);
145
      mCenter = new DistortedBitmap(SIZE, SIZE, 1);
146

    
147
      mObjWidth = mObject.getWidth();
148
      mObjHeight= mObject.getHeight();
149

    
150
      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
      mCenterInter  = new Dynamic2D();
157
      mDeformInter  = new Dynamic3D();
158
      mDistortInter = new Dynamic3D();
159
      mSwirlInter   = new Dynamic1D();
160
      mSinkInter    = new Dynamic1D();
161

    
162
      mCenterInter.add(mCenterPoint);
163
      mDeformInter.add(mDeformPoint);
164
      mDistortInter.add(mDistortPoint);
165
      mSwirlInter.add(mSwirlPoint);
166
      mSinkInter.add(mSinkPoint);
167

    
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
      }
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

    
185
      long time = System.currentTimeMillis();
186

    
187
      mObject.draw(time);
188
      mCenter.draw(time);
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
    
193
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
194
      {
195
      mScreenMin = width<height ? width:height;
196

    
197
      mObject.abortEffects(EffectTypes.MATRIX);
198
      mCenter.abortEffects(EffectTypes.MATRIX);
199

    
200
      float factor;
201

    
202
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
203
        {
204
        factor = (0.7f*height)/mObjHeight;
205
        }
206
      else
207
        {
208
        factor = (0.7f*width)/mObjWidth;
209
        }
210

    
211
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
212

    
213
      mObject.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
214
      mObject.scale(factor);
215
      mObject.quaternion(mQuatInt1, center);
216
      mObject.quaternion(mQuatInt2, center);
217

    
218
      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
      setVertexEffects();
226

    
227
      Distorted.onSurfaceChanged(width, height); 
228
      }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
    
232
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
233
      {
234
      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
        
239
      try 
240
        {
241
        bitmap1 = BitmapFactory.decodeStream(is1);
242
        bitmap2 = BitmapFactory.decodeStream(is2);
243
        }
244
      finally 
245
        {
246
        try 
247
          {
248
          is1.close();
249
          is2.close();
250
          } 
251
        catch(IOException e) { }
252
        }  
253
      
254
      mObject.setBitmap(bitmap1);
255
      mCenter.setBitmap(bitmap2);
256

    
257
      try
258
        {
259
        Distorted.onSurfaceCreated(mView.getContext());
260
        }
261
      catch(Exception ex)
262
        {
263
        android.util.Log.e("Vertex3D", ex.getMessage() );
264
        }
265
      }
266
}
(2-2/3)