Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ bc0a685b

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.effects3d;
21

    
22
import java.io.IOException;
23
import java.io.InputStream;
24

    
25
import javax.microedition.khronos.egl.EGLConfig;
26
import javax.microedition.khronos.opengles.GL10;
27

    
28
import org.distorted.examples.R;
29

    
30
import org.distorted.library.DistortedCubes;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.Float3D;
33
import org.distorted.library.Float4D;
34
import org.distorted.library.Interpolator3D;
35
import org.distorted.library.Interpolator4D;
36
import org.distorted.library.Distorted;
37

    
38
import android.graphics.Bitmap;
39
import android.graphics.BitmapFactory;
40
import android.opengl.GLES20;
41
import android.opengl.GLSurfaceView;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
class Effects3DRenderer implements GLSurfaceView.Renderer 
46
{
47
    private static final int SIZE = 100;
48
	
49
    private GLSurfaceView mView;
50
    private static DistortedCubes mCube;
51

    
52
    private static int[] order;
53
    
54
    private static Interpolator3D mMoveInter, mScaleInter, mShearInter;
55
    private static Interpolator4D mRotateInter;
56
    
57
    private static Float3D mZeroPoint, mMovePoint, mScalePoint, mShearPoint;
58
    private static Float4D mRotatePoint;
59
    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
    public static void setMove(float x, float y, float z)
63
      {
64
      mMovePoint.set(x, y, z);
65
      }
66
    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
    public static void setScale(float x, float y, float z)
70
      {
71
      mScalePoint.set(x, y, z);
72
      }
73
     
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
    public static void setRotate(float a, float x, float y, float z)
77
      {
78
      mRotatePoint.set(a, x, y, z);
79
      }
80
    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
    public static void setShear(float x, float y, float z)
84
      {
85
      mShearPoint.set(x, y, z);
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89

    
90
    public static void setOrder(int[] effects)
91
      {
92
      order = effects;
93
      setMatrixEffects();
94
      }
95
      
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
    public static void setMatrixEffects()
99
      {
100
      mCube.abortEffects(EffectTypes.MATRIX);
101
	
102
      for( int i=0; i<=order.length-1 ; i++ )
103
        {
104
        switch(order[i])
105
          {
106
          case Effects3DActivity.MOVE  : mCube.move(mMoveInter)               ; break; 
107
          case Effects3DActivity.SCALE : mCube.scale(mScaleInter)             ; break;
108
          case Effects3DActivity.ROTATE: mCube.rotate(mZeroPoint,mRotateInter); break;
109
          case Effects3DActivity.SHEAR : mCube.shear(mZeroPoint,mShearInter)  ; break;
110
          }
111
        }
112
      }
113
    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
    public Effects3DRenderer(GLSurfaceView v) 
117
      {
118
      mView = v;
119
      mCube = new DistortedCubes( 1, "1", SIZE);
120
      
121
      mZeroPoint  = new Float3D(0,0,0);
122
      mMovePoint  = new Float3D(0,0,0);
123
      mScalePoint = new Float3D(1,1,1);
124
      mShearPoint = new Float3D(0,0,0);
125
      mRotatePoint= new Float4D(1,0,0,0);
126
      
127
      mMoveInter  = new Interpolator3D();
128
      mScaleInter = new Interpolator3D();
129
      mShearInter = new Interpolator3D();
130
      mRotateInter= new Interpolator4D();
131
      
132
      mMoveInter.add(mMovePoint);
133
      mScaleInter.add(mScalePoint);
134
      mShearInter.add(mShearPoint);
135
      mRotateInter.add(mRotatePoint);
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
   
140
    public void onDrawFrame(GL10 glUnused) 
141
      {
142
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
143
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
144
      
145
      mCube.draw(System.currentTimeMillis());
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
151
      {
152
      setMatrixEffects();
153

    
154
      Distorted.onSurfaceChanged(width, height); 
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
    
159
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
160
      {
161
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
162
      Bitmap bitmap;
163
        
164
      try 
165
        {
166
        bitmap = BitmapFactory.decodeStream(is);
167
        } 
168
      finally 
169
        {
170
        try 
171
          {
172
          is.close();
173
          } 
174
        catch(IOException e) { }
175
        }  
176
      
177
      mCube.setBitmap(bitmap);
178
      
179
      try
180
        {
181
        Distorted.onSurfaceCreated(mView.getContext());
182
        }
183
      catch(Exception ex)
184
        {
185
        android.util.Log.e("Quaternion", ex.getMessage() );
186
        }
187
      }
188
}
(2-2/3)