Project

General

Profile

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

examples / src / main / java / org / distorted / examples / flag / FlagSurfaceView.java @ 76f9798b

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

    
22
import android.content.Context;
23
import android.opengl.GLSurfaceView;
24
import android.util.AttributeSet;
25
import android.view.MotionEvent;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
class FlagSurfaceView extends GLSurfaceView
30
{
31
    private int mX, mY;
32
    private FlagRenderer mRenderer;
33
	
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
   
36
    public FlagSurfaceView(Context c, AttributeSet attrs)
37
      {
38
      super(c,attrs);
39
    
40
      mX = -1;
41
      mY = -1;
42
      
43
      if(!isInEditMode())
44
        {
45
        setEGLContextClientVersion(2);
46
        mRenderer = new FlagRenderer(this);
47
        setRenderer(mRenderer);
48
        }
49
      }
50
    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
    public FlagRenderer getRenderer()
54
      {
55
      return mRenderer;
56
      }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
    @Override public boolean onTouchEvent(MotionEvent event) 
61
      {
62
      int action = event.getAction();
63
      int x = (int)event.getX();
64
      int y = (int)event.getY();
65
           
66
      switch(action)
67
         {
68
         case MotionEvent.ACTION_DOWN: mX = x;
69
                                       mY = y;
70
                                       break;
71
                                       
72
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
73
                                         {
74
                                         float px = mY-y;
75
                                         float py = mX-x;
76
                                         float pz = 0;
77
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
78
                                         
79
                                         if( plen>0 )
80
                                           {
81
                                           px /= plen;
82
                                           py /= plen;
83
                                           pz /= plen;
84

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

    
101
                                       float rx = mRenderer.mQuat2.getX();
102
                                       float ry = mRenderer.mQuat2.getY();
103
                                       float rz = mRenderer.mQuat2.getZ();
104
                                       float rw = mRenderer.mQuat2.getW();
105

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

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

    
(3-3/3)