Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DRenderer.java @ 261fe5bd

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.DistortedCubes;
30
import org.distorted.library.DistortedObject;
31
import org.distorted.library.EffectNames;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.type.Dynamic1D;
34
import org.distorted.library.type.Dynamic3D;
35
import org.distorted.library.type.DynamicQuat;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static2D;
38
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 GLSurfaceView mView;
52
    private static DistortedObject mObject;
53

    
54
    private int mObjWidth, mObjHeight;
55

    
56
    private DynamicQuat mQuatInt1, mQuatInt2;
57

    
58
    private static EffectNames[] order;
59
    
60
    private static Dynamic3D mDeformInter, mDistortInter;
61
    private static Dynamic1D mSinkInter, mSwirlInter;
62

    
63
    private static Static2D mCenterPoint;
64
    private static Static3D mDeformPoint, mDistortPoint;
65
    private static Static1D mSinkPoint, mSwirlPoint;
66

    
67
    Static4D mQuat1, mQuat2;
68
    static int mScreenMin;
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
    public static void setDeform(float x, float y, float z)
73
      {
74
      mDeformPoint.set(x, y, z);
75
      }
76
    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

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

    
86
    public static void setSink(float s)
87
      {
88
      mSinkPoint.set(s);
89
      }
90
    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
    public static void setSwirl(float s)
94
      {
95
      mSwirlPoint.set(s);
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
    public static void setOrder(EffectNames[] effects)
101
      {
102
      order = effects;
103
      setVertexEffects();
104
      }
105
      
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
    public static void setVertexEffects()
109
      {
110
      mObject.abortEffects(EffectTypes.VERTEX);
111
	
112
      for( int i=0; i<=order.length-1 ; i++ )
113
        {
114
        switch(order[i])
115
          {
116
          case DEFORM : mObject.deform( mDeformInter , mCenterPoint) ; break;
117
          case DISTORT: mObject.distort(mDistortInter, mCenterPoint) ; break;
118
          case SINK   : mObject.sink(   mSinkInter   , mCenterPoint) ; break;
119
          case SWIRL  : mObject.swirl(  mSwirlInter  , mCenterPoint) ; break;
120
          }
121
        }
122
      }
123
    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
    public Vertex3DRenderer(GLSurfaceView v)
127
      {
128
      mView = v;
129

    
130
      mObject = new DistortedCubes( Vertex3DActivity.getCols(), Vertex3DActivity.getShape(), 100);
131
      //mObject = new DistortedBitmap( 100, 100, 10);
132

    
133
      mObjWidth = mObject.getWidth();
134
      mObjHeight= mObject.getHeight();
135

    
136
      mCenterPoint = new Static2D(0,0);
137
      mDeformPoint = new Static3D(0,0,0);
138
      mDistortPoint= new Static3D(1,1,1);
139
      mSwirlPoint  = new Static1D(0);
140
      mSinkPoint   = new Static1D(1);
141

    
142
      mDeformInter  = new Dynamic3D();
143
      mDistortInter = new Dynamic3D();
144
      mSwirlInter   = new Dynamic1D();
145
      mSinkInter    = new Dynamic1D();
146

    
147
      mDeformInter.add(mDeformPoint);
148
      mDistortInter.add(mDistortPoint);
149
      mSwirlInter.add(mSwirlPoint);
150
      mSinkInter.add(mSinkPoint);
151

    
152
      mQuat1 = new Static4D(0,0,0,1);  // unity
153
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
154

    
155
      mQuatInt1 = new DynamicQuat(0,0.5f);
156
      mQuatInt2 = new DynamicQuat(0,0.5f);
157

    
158
      mQuatInt1.add(mQuat1);
159
      mQuatInt2.add(mQuat2);
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
   
164
    public void onDrawFrame(GL10 glUnused) 
165
      {
166
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
167
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
168
      
169
      mObject.draw(System.currentTimeMillis());
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
    
174
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
175
      {
176
      mScreenMin = width<height ? width:height;
177

    
178
      mObject.abortEffects(EffectTypes.MATRIX);
179
      float factor;
180

    
181
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
182
        {
183
        factor = (0.8f*height)/mObjHeight;
184
        }
185
      else
186
        {
187
        factor = (0.8f*width)/mObjWidth;
188
        }
189

    
190
      mObject.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
191
      mObject.scale(factor);
192

    
193
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
194

    
195
      mObject.quaternion(mQuatInt1, center);
196
      mObject.quaternion(mQuatInt2, center);
197

    
198
      setVertexEffects();
199

    
200
      Distorted.onSurfaceChanged(width, height); 
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
    
205
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
206
      {
207
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
208
      Bitmap bitmap;
209
        
210
      try 
211
        {
212
        bitmap = BitmapFactory.decodeStream(is);
213
        } 
214
      finally 
215
        {
216
        try 
217
          {
218
          is.close();
219
          } 
220
        catch(IOException e) { }
221
        }  
222
      
223
      mObject.setBitmap(bitmap);
224
      
225
      try
226
        {
227
        Distorted.onSurfaceCreated(mView.getContext());
228
        }
229
      catch(Exception ex)
230
        {
231
        android.util.Log.e("Vertex3D", ex.getMessage() );
232
        }
233
      }
234
}
(2-2/3)