Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 db5d943e Leszek Koltunski
package org.distorted.examples.matrix3d;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 5068fa06 Leszek Koltunski
import org.distorted.library.DistortedCubes;
31 08f92d82 Leszek Koltunski
import org.distorted.library.EffectNames;
32 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
33 7589635e Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
38 427ab7bf Leszek Koltunski
39
import android.graphics.Bitmap;
40
import android.graphics.BitmapFactory;
41
import android.opengl.GLES20;
42
import android.opengl.GLSurfaceView;
43
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 db5d943e Leszek Koltunski
class Matrix3DRenderer implements GLSurfaceView.Renderer
47 427ab7bf Leszek Koltunski
{
48
    private static final int SIZE = 100;
49
	
50
    private GLSurfaceView mView;
51 1d811f85 Leszek Koltunski
    private DistortedCubes mCube;
52
    private EffectNames[] order;
53 427ab7bf Leszek Koltunski
    
54 1d811f85 Leszek Koltunski
    private Dynamic3D mMoveInter, mScaleInter, mShearInter;
55
    private Dynamic4D mDynamicRotate;
56
    private Static3D mZeroPoint, mMovePoint, mScalePoint, mShearPoint;
57
    private Static4D mRotatePoint;
58 7589635e Leszek Koltunski
59 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 1d811f85 Leszek Koltunski
    void setMove(float x, float y, float z)
62 427ab7bf Leszek Koltunski
      {
63
      mMovePoint.set(x, y, z);
64
      }
65
    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68 1d811f85 Leszek Koltunski
    void setScale(float x, float y, float z)
69 427ab7bf Leszek Koltunski
      {
70
      mScalePoint.set(x, y, z);
71
      }
72
     
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75 1d811f85 Leszek Koltunski
    void setRotate(float a, float x, float y, float z)
76 427ab7bf Leszek Koltunski
      {
77 7589635e Leszek Koltunski
      mRotatePoint.set(a,x,y,z);
78 427ab7bf Leszek Koltunski
      }
79
    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
82 1d811f85 Leszek Koltunski
    void setShear(float x, float y, float z)
83 427ab7bf Leszek Koltunski
      {
84
      mShearPoint.set(x, y, z);
85
      }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89 1d811f85 Leszek Koltunski
    void setOrder(EffectNames[] effects)
90 427ab7bf Leszek Koltunski
      {
91
      order = effects;
92
      setMatrixEffects();
93
      }
94
      
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
97 1d811f85 Leszek Koltunski
    private void setMatrixEffects()
98 427ab7bf Leszek Koltunski
      {
99 a8c3ada7 Leszek Koltunski
      mCube.abortEffects(EffectTypes.MATRIX);
100 427ab7bf Leszek Koltunski
	
101
      for( int i=0; i<=order.length-1 ; i++ )
102
        {
103
        switch(order[i])
104
          {
105 08f92d82 Leszek Koltunski
          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 427ab7bf Leszek Koltunski
          }
110
        }
111
      }
112
    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115 1d811f85 Leszek Koltunski
    Matrix3DRenderer(GLSurfaceView v)
116 427ab7bf Leszek Koltunski
      {
117
      mView = v;
118
      mCube = new DistortedCubes( 1, "1", SIZE);
119
      
120 7589635e Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      mMoveInter.add(mMovePoint);
132
      mScaleInter.add(mScalePoint);
133
      mShearInter.add(mShearPoint);
134 7589635e Leszek Koltunski
      mDynamicRotate.add(mRotatePoint);
135 427ab7bf Leszek Koltunski
      }
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 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
181 427ab7bf Leszek Koltunski
        }
182
      catch(Exception ex)
183
        {
184 08f92d82 Leszek Koltunski
        android.util.Log.e("Matrix3D", ex.getMessage() );
185 427ab7bf Leszek Koltunski
        }
186
      }
187
}