Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DRenderer.java @ 26b28407

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