Project

General

Profile

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

examples / src / main / java / org / distorted / examples / matrix3d / Matrix3DActivity.java @ 08f92d82

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 org.distorted.library.Distorted;
23
import org.distorted.examples.R;
24
import org.distorted.library.EffectNames;
25

    
26
import android.app.Activity;
27
import android.os.Bundle;
28
import android.opengl.GLSurfaceView;
29
import android.view.View;
30
import android.widget.LinearLayout;
31
import android.widget.SeekBar;
32
import android.widget.TextView;
33
import android.widget.SeekBar.OnSeekBarChangeListener;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
public class Matrix3DActivity extends Activity implements OnSeekBarChangeListener
38
{
39
    private SeekBar bar;
40
    private TextView textMove, textScale, textRotate, textShear;
41
    private int moveX, moveY, moveZ;
42
    private int scaleX, scaleY, scaleZ;
43
    private int rotateX, rotateY, rotateZ, rotateA;
44
    private int shearX, shearY, shearZ;
45
    
46
    private int maxX, maxY, maxZ;
47
    
48
    private float fmoveX, fmoveY, fmoveZ;
49
    private float fscaleX, fscaleY, fscaleZ;
50
    private float frotateX, frotateY, frotateZ, frotateA;
51
    private float fshearX, fshearY, fshearZ;
52
    
53
    private EffectNames[] effects = new EffectNames[4];
54
    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
    
57
    @Override
58
    protected void onCreate(Bundle icicle) 
59
      {
60
      super.onCreate(icicle);
61
      setContentView(R.layout.matrix3dlayout);
62
      Default(null);
63
      }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
    public void Default(View view)
68
      {
69
      effects[0] = EffectNames.MOVE;
70
      effects[1] = EffectNames.SCALE;
71
      effects[2] = EffectNames.ROTATE;
72
      effects[3] = EffectNames.SHEAR;
73
    
74
      moveX = 50;
75
      moveY = 50;
76
      moveZ = 50;
77
      
78
      scaleX = 50;
79
      scaleY = 50;
80
      scaleZ = 50;
81
      
82
      rotateX = 100;
83
      rotateY =  50;
84
      rotateZ =  50;
85
      rotateA =  50;
86
      
87
      shearX = 50;
88
      shearY = 50;
89
      shearZ = 50;
90
      
91
      addViews();
92
      }
93
    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
    
96
    private void addViews()
97
      {
98
      LinearLayout layout = (LinearLayout)findViewById(R.id.matrix3dlayout);
99
    
100
      layout.removeAllViews();
101
      
102
      View move   = getLayoutInflater().inflate(R.layout.matrix3dmove, null);
103
      View scale  = getLayoutInflater().inflate(R.layout.matrix3dscale, null);
104
      View rotate = getLayoutInflater().inflate(R.layout.matrix3drotate, null);
105
      View shear  = getLayoutInflater().inflate(R.layout.matrix3dshear, null);
106
     
107
      for( int i=effects.length-1 ; i>=0 ; i-- )
108
        {
109
        switch(effects[i])
110
          {
111
          case MOVE  : layout.addView(move   , 0); break; 
112
          case SCALE : layout.addView(scale  , 0); break;
113
          case ROTATE: layout.addView(rotate , 0); break;
114
          case SHEAR : layout.addView(shear  , 0); break;
115
          }
116
        }
117
      
118
      textMove  = (TextView)findViewById(R.id.matrix3dmoveText);
119
      textScale = (TextView)findViewById(R.id.matrix3dscaleText);
120
      textRotate= (TextView)findViewById(R.id.matrix3drotateText);
121
      textShear = (TextView)findViewById(R.id.matrix3dshearText);
122
     
123
      setMoveText();
124
      setScaleText();
125
      setRotateText();
126
      setShearText();
127
      
128
      setBar(R.id.matrix3dmoveBar1, moveX);
129
      setBar(R.id.matrix3dmoveBar2, moveY);
130
      setBar(R.id.matrix3dmoveBar3, moveZ);
131
      
132
      setBar(R.id.matrix3dscaleBar1, scaleX);
133
      setBar(R.id.matrix3dscaleBar2, scaleY);
134
      setBar(R.id.matrix3dscaleBar3, scaleZ);
135
      
136
      setBar(R.id.matrix3drotateBar1, rotateX);
137
      setBar(R.id.matrix3drotateBar2, rotateY);
138
      setBar(R.id.matrix3drotateBar3, rotateZ);
139
      setBar(R.id.matrix3drotateBar4, rotateA);
140
      
141
      setBar(R.id.matrix3dshearBar1, shearX);
142
      setBar(R.id.matrix3dshearBar2, shearY);
143
      setBar(R.id.matrix3dshearBar3, shearZ);
144
      
145
      Matrix3DRenderer.setOrder(effects);
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
    private void moveUp(EffectNames name)
151
      {
152
      int len = effects.length-1;	
153
      int index = -1;
154
      
155
      for(int i=0; i<=len; i++)	
156
        {
157
        if( effects[i]==name )
158
          {
159
          index=i;
160
          break;
161
          }
162
        }
163
      
164
      if( index==0 )
165
        {
166
        for(int i=0; i<len; i++)
167
          effects[i] = effects[i+1];
168
    	
169
        effects[len] = name;
170
        }
171
      else if( index>0 )
172
        {
173
        effects[index]   = effects[index-1];
174
        effects[index-1] = name;
175
        }
176
      
177
      addViews();
178
      }
179
 
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
    public void ButtonMove(View v)
183
      {
184
      moveUp(EffectNames.MOVE);
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
    public void ButtonScale(View v)
190
      {
191
      moveUp(EffectNames.SCALE);
192
      }
193
    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    public void ButtonRotate(View v)
197
      {
198
      moveUp(EffectNames.ROTATE);
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    public void ButtonShear(View v)
204
      {
205
      moveUp(EffectNames.SHEAR);
206
      }
207
 
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
    
210
    private void setBar(int id, int value)
211
      {
212
      bar = (SeekBar)findViewById(id);
213
      bar.setOnSeekBarChangeListener(this); 
214
      bar.setProgress(value);
215
      }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
    private void computeMove()
220
      {
221
      fmoveX = (moveX-50)*maxX/50.0f;
222
      fmoveY = (moveY-50)*maxY/50.0f;
223
      fmoveZ = (moveZ-50)*maxZ/50.0f;
224

    
225
      Matrix3DRenderer.setMove( fmoveX, fmoveY, fmoveZ);
226
      }
227
    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
    private void setMoveText()
231
      {
232
      fmoveX = ((int)(100*fmoveX))/100.0f;
233
      fmoveY = ((int)(100*fmoveY))/100.0f;
234
      fmoveZ = ((int)(100*fmoveZ))/100.0f;
235
      
236
      textMove.setText("move("+fmoveX+" , "+fmoveY+" , "+fmoveZ+")");
237
      }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
    private void computeScale()
242
      {
243
      fscaleX = (scaleX>50 ? 0.18f : 0.018f)*(scaleX-50)+1;
244
      fscaleY = (scaleY>50 ? 0.18f : 0.018f)*(scaleY-50)+1;
245
      fscaleZ = (scaleZ>50 ? 0.18f : 0.018f)*(scaleZ-50)+1;
246

    
247
      Matrix3DRenderer.setScale(fscaleX, fscaleY, fscaleZ);
248
      }
249
    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
    private void setScaleText()
253
      {
254
      fscaleX = ((int)(100*fscaleX))/100.0f;
255
      fscaleY = ((int)(100*fscaleY))/100.0f;
256
      fscaleZ = ((int)(100*fscaleZ))/100.0f;
257
      
258
      textScale.setText("scale("+fscaleX+" , "+fscaleY+" , "+fscaleZ+")");
259
      }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
    private void computeRotate()
264
      {
265
      frotateX = (rotateX-50)/50.0f;
266
      frotateY = (rotateY-50)/50.0f;
267
      frotateZ = (rotateZ-50)/50.0f;
268
    
269
      Matrix3DRenderer.setRotate( frotateA, frotateX, frotateY, frotateZ );
270
      }
271
    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
    private void setRotateText()
275
      {
276
      frotateX = ((int)(100*frotateX))/100.0f;
277
      frotateY = ((int)(100*frotateY))/100.0f;
278
      frotateZ = ((int)(100*frotateZ))/100.0f;
279
      
280
      frotateA = ((rotateA-50)*180)/50;
281
      
282
      textRotate.setText("rotate( "+frotateA+" ("+frotateX+","+frotateY+","+frotateZ+") )");
283
      }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
    private void computeShear()
288
      {
289
      fshearX = (shearX-50)/25.0f;
290
      fshearY = (shearY-50)/25.0f;
291
      fshearZ = (shearZ-50)/25.0f;
292
    		
293
      Matrix3DRenderer.setShear( fshearX, fshearY, fshearZ );
294
      }
295
    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
    private void setShearText()
299
      {
300
      fshearX = ((int)(100*fshearX))/100.0f;
301
      fshearY = ((int)(100*fshearY))/100.0f;
302
      fshearZ = ((int)(100*fshearZ))/100.0f;
303
      
304
      textShear.setText("shear("+fshearX+" , "+fshearY+" , "+fshearZ+")");	
305
      }
306
   
307
///////////////////////////////////////////////////////////////////////////////////////////////////
308
    
309
    @Override
310
    protected void onPause() 
311
      {
312
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
313
      mView.onPause(); 
314
      super.onPause();
315
      }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318
    
319
    @Override
320
    protected void onResume() 
321
      {
322
      super.onResume();
323
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
324
      mView.onResume();
325
      }
326

    
327
///////////////////////////////////////////////////////////////////////////////////////////////////
328
    
329
    @Override
330
    public void onWindowFocusChanged(boolean hasFocus) 
331
      {
332
      super.onWindowFocusChanged(hasFocus);
333
  
334
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
335
      
336
      maxX = mView.getWidth();
337
      maxY = mView.getHeight();
338
      maxZ = (maxX+maxY)/2;
339
      }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342
    
343
    @Override
344
    protected void onDestroy() 
345
      {
346
      Distorted.onDestroy();  
347
      super.onDestroy();
348
      }
349
    
350
///////////////////////////////////////////////////////////////////////////////////////////////////
351
    
352
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) 
353
      {
354
      switch (bar.getId()) 
355
        {
356
        case R.id.matrix3dmoveBar1  : moveX = progress; computeMove()  ; setMoveText()  ; break;
357
        case R.id.matrix3dmoveBar2  : moveY = progress; computeMove()  ; setMoveText()  ; break;
358
        case R.id.matrix3dmoveBar3  : moveZ = progress; computeMove()  ; setMoveText()  ; break;
359
        
360
        case R.id.matrix3dscaleBar1 : scaleX= progress; computeScale() ; setScaleText() ; break;
361
        case R.id.matrix3dscaleBar2 : scaleY= progress; computeScale() ; setScaleText() ; break;
362
        case R.id.matrix3dscaleBar3 : scaleZ= progress; computeScale() ; setScaleText() ; break;
363
        
364
        case R.id.matrix3drotateBar1: rotateX=progress; computeRotate(); setRotateText(); break;
365
        case R.id.matrix3drotateBar2: rotateY=progress; computeRotate(); setRotateText(); break;
366
        case R.id.matrix3drotateBar3: rotateZ=progress; computeRotate(); setRotateText(); break;
367
        case R.id.matrix3drotateBar4: rotateA=progress; computeRotate(); setRotateText(); break;
368
        
369
        case R.id.matrix3dshearBar1 : shearX= progress; computeShear() ; setShearText() ; break;
370
        case R.id.matrix3dshearBar2 : shearY= progress; computeShear() ; setShearText() ; break;
371
        case R.id.matrix3dshearBar3 : shearZ= progress; computeShear() ; setShearText() ; break;
372
        }
373
      }
374

    
375
///////////////////////////////////////////////////////////////////////////////////////////////////
376

    
377
    public void onStartTrackingTouch(SeekBar bar) { }
378
    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
    public void onStopTrackingTouch(SeekBar bar)  { }
382
    
383
}
(1-1/3)