Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesSurfaceView.java @ b01acdaf

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.cubes;
21

    
22
import org.distorted.examples.R;
23

    
24
import android.content.Context;
25
import android.opengl.GLSurfaceView;
26
import android.util.AttributeSet;
27
import android.view.MotionEvent;
28
import android.widget.Toast;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
class CubesSurfaceView extends GLSurfaceView 
33
{
34
    private int mX, mY;
35
    private CubesRenderer mRenderer;
36
	
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
   
39
    public CubesSurfaceView(Context c, AttributeSet attrs) 
40
      {
41
      super(c,attrs);
42
    
43
      mX = -1;
44
      mY = -1;
45
      
46
      if(!isInEditMode())
47
        {
48
        setEGLContextClientVersion(2);
49
        mRenderer = new CubesRenderer(this);
50
        setRenderer(mRenderer);
51
        Toast.makeText(c, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
52
        }
53
      }
54
    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
    
57
    @Override public boolean onTouchEvent(MotionEvent event) 
58
      {
59
      int action = event.getAction();
60
      int x = (int)event.getX();
61
      int y = (int)event.getY();
62
           
63
      switch(action)
64
         {
65
         case MotionEvent.ACTION_DOWN: mX = x;
66
                                       mY = y;
67
                                       break;
68
                                       
69
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
70
                                         {
71
                                         float px = mY-y;
72
                                         float py = mX-x;
73
                                         float pz = 0;
74
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
75
                                         
76
                                         if( plen>0 )
77
                                           {
78
                                           px /= plen;
79
                                           py /= plen;
80
                                           pz /= plen;
81

    
82
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
83
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
84
                                         
85
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
86
                                           }
87
                                         }                             
88
                                       break;
89
                                       
90
         case MotionEvent.ACTION_UP  : mX = -1;
91
                                       mY = -1;
92
        	                           
93
                                       float qx = mRenderer.mQuat1.getX();
94
                                       float qy = mRenderer.mQuat1.getY();
95
                                       float qz = mRenderer.mQuat1.getZ();
96
                                       float qw = mRenderer.mQuat1.getW();
97

    
98
                                       float rx = mRenderer.mQuat2.getX();
99
                                       float ry = mRenderer.mQuat2.getY();
100
                                       float rz = mRenderer.mQuat2.getZ();
101
                                       float rw = mRenderer.mQuat2.getW();
102

    
103
                                       // This is quaternion multiplication. (tx,ty,tz,tw)
104
                                       // is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw)
105
                                       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
106
                                       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
107
                                       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
108
                                       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
109

    
110
                                       // The point of this is so that there are always
111
                                       // exactly 2 quaternions: Quat1 representing the rotation
112
                                       // accumulating only since the last screen touch, and Quat2
113
                                       // which remembers the combined effect of all previous
114
                                       // swipes.
115
                                       // We cannot be accumulating an ever-growing list of quaternions
116
                                       // and add a new one every time user swipes the screen - there
117
                                       // is a limited number of slots in the EffectQueueMatrix!
118
                                       mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
119
                                       mRenderer.mQuat2.set(tx, ty, tz, tw);
120
                                       
121
                                       break;
122
         }
123
             
124
      return true;
125
      }
126
         
127
}
128

    
(3-3/3)