Project

General

Profile

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

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

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

    
58
    private int mObjWidth, mObjHeight;
59

    
60
    private DynamicQuat mQuatInt1, mQuatInt2;
61

    
62
    private static EffectNames[] order;
63

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

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

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

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

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

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

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

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

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

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

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

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

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

    
142
      mObject = new DistortedCubes( Vertex3DActivity.getCols(), Vertex3DActivity.getShape(), SIZE);
143
      //mObject = new DistortedBitmap( SIZE, SIZE, 10);
144

    
145
      mObjWidth = mObject.getWidth();
146
      mObjHeight= mObject.getHeight();
147

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

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

    
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
      }
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
      mObject.draw(System.currentTimeMillis());
184
      }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
    
188
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
189
      {
190
      mScreenMin = width<height ? width:height;
191

    
192
      mObject.abortEffects(EffectTypes.MATRIX);
193
      float factor;
194

    
195
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
196
        {
197
        factor = (0.7f*height)/mObjHeight;
198
        }
199
      else
200
        {
201
        factor = (0.7f*width)/mObjWidth;
202
        }
203

    
204
      mObject.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
205
      mObject.scale(factor);
206

    
207
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
208

    
209
      mObject.quaternion(mQuatInt1, center);
210
      mObject.quaternion(mQuatInt2, center);
211

    
212
      setVertexEffects();
213

    
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
      mObject.setBitmap(bitmap);
238
      
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
}
(2-2/3)