Project

General

Profile

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

examples / src / main / java / org / distorted / examples / matrix3d / Matrix3DActivity.java @ db5d943e

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

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

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

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

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

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

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

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

    
186
    public void ButtonMove(View v)
187
      {
188
      moveUp(MOVE); 	
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    public void ButtonScale(View v)
194
      {
195
      moveUp(SCALE); 	
196
      }
197
    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
    public void ButtonRotate(View v)
201
      {
202
      moveUp(ROTATE); 	
203
      }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

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

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
    private void computeMove()
224
      {
225
      fmoveX = (moveX-50)*maxX/50.0f;
226
      fmoveY = (moveY-50)*maxY/50.0f;
227
      fmoveZ = (moveZ-50)*maxZ/50.0f;
228

    
229
      Matrix3DRenderer.setMove( fmoveX, fmoveY, fmoveZ);
230
      }
231
    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
    private void setMoveText()
235
      {
236
      fmoveX = ((int)(100*fmoveX))/100.0f;
237
      fmoveY = ((int)(100*fmoveY))/100.0f;
238
      fmoveZ = ((int)(100*fmoveZ))/100.0f;
239
      
240
      textMove.setText("move("+fmoveX+" , "+fmoveY+" , "+fmoveZ+")");
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    private void computeScale()
246
      {
247
      fscaleX = (scaleX>50 ? 0.18f : 0.018f)*(scaleX-50)+1;
248
      fscaleY = (scaleY>50 ? 0.18f : 0.018f)*(scaleY-50)+1;
249
      fscaleZ = (scaleZ>50 ? 0.18f : 0.018f)*(scaleZ-50)+1;
250

    
251
      Matrix3DRenderer.setScale(fscaleX, fscaleY, fscaleZ);
252
      }
253
    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
    private void setScaleText()
257
      {
258
      fscaleX = ((int)(100*fscaleX))/100.0f;
259
      fscaleY = ((int)(100*fscaleY))/100.0f;
260
      fscaleZ = ((int)(100*fscaleZ))/100.0f;
261
      
262
      textScale.setText("scale("+fscaleX+" , "+fscaleY+" , "+fscaleZ+")");
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

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

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

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
    private void computeShear()
292
      {
293
      fshearX = (shearX-50)/25.0f;
294
      fshearY = (shearY-50)/25.0f;
295
      fshearZ = (shearZ-50)/25.0f;
296
    		
297
      Matrix3DRenderer.setShear( fshearX, fshearY, fshearZ );
298
      }
299
    
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

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

    
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322
    
323
    @Override
324
    protected void onResume() 
325
      {
326
      super.onResume();
327
      GLSurfaceView mView = (GLSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
328
      mView.onResume();
329
      }
330

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

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

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380

    
381
    public void onStartTrackingTouch(SeekBar bar) { }
382
    
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384

    
385
    public void onStopTrackingTouch(SeekBar bar)  { }
386
    
387
}
(1-1/3)