Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 89a0d841

1

    
2
package org.distorted.examples.effects3d;
3

    
4
import java.io.IOException;
5
import java.io.InputStream;
6

    
7
import javax.microedition.khronos.egl.EGLConfig;
8
import javax.microedition.khronos.opengles.GL10;
9

    
10
import org.distorted.examples.R;
11

    
12
import org.distorted.library.DistortedCubes;
13
import org.distorted.library.Float3D;
14
import org.distorted.library.Float4D;
15
import org.distorted.library.Interpolator3D;
16
import org.distorted.library.Interpolator4D;
17
import org.distorted.library.Distorted;
18

    
19
import android.graphics.Bitmap;
20
import android.graphics.BitmapFactory;
21
import android.opengl.GLES20;
22
import android.opengl.GLSurfaceView;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
class Effects3DRenderer implements GLSurfaceView.Renderer 
27
{
28
    private static final int SIZE = 100;
29
	
30
    private GLSurfaceView mView;
31
    private static DistortedCubes mCube;
32

    
33
    private static int[] order;
34
    
35
    private static Interpolator3D mMoveInter, mScaleInter, mShearInter;
36
    private static Interpolator4D mRotateInter;
37
    
38
    private static Float3D mZeroPoint, mMovePoint, mScalePoint, mShearPoint;
39
    private static Float4D mRotatePoint;
40
    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
    public static void setMove(float x, float y, float z)
44
      {
45
      mMovePoint.set(x, y, z);
46
      }
47
    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
    public static void setScale(float x, float y, float z)
51
      {
52
      mScalePoint.set(x, y, z);
53
      }
54
     
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
    public static void setRotate(float a, float x, float y, float z)
58
      {
59
      mRotatePoint.set(a, x, y, z);
60
      }
61
    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
    public static void setShear(float x, float y, float z)
65
      {
66
      mShearPoint.set(x, y, z);
67
      }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
    public static void setOrder(int[] effects)
72
      {
73
      order = effects;
74
      setMatrixEffects();
75
      }
76
      
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
    public static void setMatrixEffects()
80
      {
81
      mCube.abortAllEffects(Distorted.TYPE_PRE);
82
	
83
      for( int i=0; i<=order.length-1 ; i++ )
84
        {
85
        switch(order[i])
86
          {
87
          case Effects3DActivity.MOVE  : mCube.move(mMoveInter)               ; break; 
88
          case Effects3DActivity.SCALE : mCube.scale(mScaleInter)             ; break;
89
          case Effects3DActivity.ROTATE: mCube.rotate(mZeroPoint,mRotateInter); break;
90
          case Effects3DActivity.SHEAR : mCube.shear(mZeroPoint,mShearInter)  ; break;
91
          }
92
        }
93
      }
94
    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
    public Effects3DRenderer(GLSurfaceView v) 
98
      {
99
      mView = v;
100
      mCube = new DistortedCubes( 1, "1", SIZE);
101
      
102
      mZeroPoint  = new Float3D(0,0,0);
103
      mMovePoint  = new Float3D(0,0,0);
104
      mScalePoint = new Float3D(1,1,1);
105
      mShearPoint = new Float3D(0,0,0);
106
      mRotatePoint= new Float4D(1,0,0,0);
107
      
108
      mMoveInter  = new Interpolator3D();
109
      mScaleInter = new Interpolator3D();
110
      mShearInter = new Interpolator3D();
111
      mRotateInter= new Interpolator4D();
112
      
113
      mMoveInter.add(mMovePoint);
114
      mScaleInter.add(mScalePoint);
115
      mShearInter.add(mShearPoint);
116
      mRotateInter.add(mRotatePoint);
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
   
121
    public void onDrawFrame(GL10 glUnused) 
122
      {
123
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
124
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
125
      
126
      mCube.draw(System.currentTimeMillis());
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
    
131
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
132
      {
133
      setMatrixEffects();
134

    
135
      Distorted.onSurfaceChanged(width, height); 
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
141
      {
142
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
143
      Bitmap bitmap;
144
        
145
      try 
146
        {
147
        bitmap = BitmapFactory.decodeStream(is);
148
        } 
149
      finally 
150
        {
151
        try 
152
          {
153
          is.close();
154
          } 
155
        catch(IOException e) { }
156
        }  
157
      
158
      mCube.setBitmap(bitmap);
159
      
160
      try
161
        {
162
        Distorted.onSurfaceCreated(mView.getContext());
163
        }
164
      catch(Exception ex)
165
        {
166
        android.util.Log.e("Quaternion", ex.getMessage() );
167
        }
168
      }
169
}
(2-2/3)