Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DRenderer.java @ 794e6c4f

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