Project

General

Profile

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

examples / src / main / java / org / distorted / examples / matrix3d / Matrix3DRenderer.java @ 1d811f85

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.matrix3d;
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.EffectNames;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.type.Dynamic3D;
34
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36
import org.distorted.library.type.Dynamic4D;
37
import org.distorted.library.Distorted;
38

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

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
class Matrix3DRenderer implements GLSurfaceView.Renderer
47
{
48
    private static final int SIZE = 100;
49
	
50
    private GLSurfaceView mView;
51
    private DistortedCubes mCube;
52
    private EffectNames[] order;
53
    
54
    private Dynamic3D mMoveInter, mScaleInter, mShearInter;
55
    private Dynamic4D mDynamicRotate;
56
    private Static3D mZeroPoint, mMovePoint, mScalePoint, mShearPoint;
57
    private Static4D mRotatePoint;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

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

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

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

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
    void setOrder(EffectNames[] effects)
90
      {
91
      order = effects;
92
      setMatrixEffects();
93
      }
94
      
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

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

    
115
    Matrix3DRenderer(GLSurfaceView v)
116
      {
117
      mView = v;
118
      mCube = new DistortedCubes( 1, "1", SIZE);
119
      
120
      mZeroPoint   = new Static3D(0,0,0);
121
      mMovePoint   = new Static3D(0,0,0);
122
      mScalePoint  = new Static3D(1,1,1);
123
      mShearPoint  = new Static3D(0,0,0);
124
      mRotatePoint = new Static4D(0,1,0,0);
125

    
126
      mMoveInter    = new Dynamic3D();
127
      mScaleInter   = new Dynamic3D();
128
      mShearInter   = new Dynamic3D();
129
      mDynamicRotate= new Dynamic4D();
130

    
131
      mMoveInter.add(mMovePoint);
132
      mScaleInter.add(mScalePoint);
133
      mShearInter.add(mShearPoint);
134
      mDynamicRotate.add(mRotatePoint);
135
      }
136

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

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

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

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