Project

General

Profile

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

examples / src / main / java / org / distorted / examples / matrix3d / Matrix3DActivity.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 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 TextView textMove, textScale, textRotate, textShear;
40
    private int moveX, moveY, moveZ;
41
    private int scaleX, scaleY, scaleZ;
42
    private int rotateX, rotateY, rotateZ, rotateA;
43
    private int shearX, shearY, shearZ;
44
    private int maxX, maxY, maxZ;
45
    private float fmoveX, fmoveY, fmoveZ;
46
    private float fscaleX, fscaleY, fscaleZ;
47
    private float frotateX, frotateY, frotateZ, frotateA;
48
    private float fshearX, fshearY, fshearZ;
49
    
50
    private EffectNames[] effects = new EffectNames[4];
51
    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
    
54
    @Override
55
    protected void onCreate(Bundle icicle) 
56
      {
57
      super.onCreate(icicle);
58
      setContentView(R.layout.matrix3dlayout);
59
      Default(null);
60
      }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

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

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

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

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

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

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

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

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

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

    
223
      Matrix3DSurfaceView view = (Matrix3DSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
224
      view.getRenderer().setMove( fmoveX, fmoveY, fmoveZ);
225
      }
226
    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

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

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

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

    
246
      Matrix3DSurfaceView view = (Matrix3DSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
247
      view.getRenderer().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
      Matrix3DSurfaceView view = (Matrix3DSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
270
      view.getRenderer().setRotate( frotateA, frotateX, frotateY, frotateZ );
271
      }
272
    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

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

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
    private void computeShear()
289
      {
290
      fshearX = (shearX-50)/25.0f;
291
      fshearY = (shearY-50)/25.0f;
292
      fshearZ = (shearZ-50)/25.0f;
293

    
294
    	Matrix3DSurfaceView view = (Matrix3DSurfaceView) this.findViewById(R.id.matrix3dSurfaceView);
295
      view.getRenderer().setShear( fshearX, fshearY, fshearZ );
296
      }
297
    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

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

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

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

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

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378

    
379
    public void onStartTrackingTouch(SeekBar bar) { }
380
    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382

    
383
    public void onStopTrackingTouch(SeekBar bar)  { }
384
    
385
}
(1-1/3)