Project

General

Profile

« Previous | Next » 

Revision 815687bb

Added by Leszek Koltunski over 7 years ago

Progress with 'Wind'

View differences:

src/main/java/org/distorted/examples/wind/WindSurfaceView.java
29 29

  
30 30
class WindSurfaceView extends GLSurfaceView
31 31
{
32
    private int mX, mY;
33 32
    private WindRenderer mRenderer;
34 33

  
35 34
///////////////////////////////////////////////////////////////////////////////////////////////////
......
38 37
      {
39 38
      super(c, attrs);
40 39

  
41
      mX = -1;
42
      mY = -1;
43

  
44 40
      if(!isInEditMode())
45 41
        {
46 42
        setEGLContextClientVersion(2);
......
60 56
      {
61 57
      return mRenderer;
62 58
      }
63

  
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

  
66
    @Override public boolean onTouchEvent(MotionEvent event)
67
      {
68
      int action = event.getAction();
69
      int x = (int)event.getX();
70
      int y = (int)event.getY();
71

  
72
      switch(action)
73
         {
74
         case MotionEvent.ACTION_DOWN: mX = x;
75
                                       mY = y;
76
                                       break;
77

  
78
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
79
                                         {
80
                                         float px = mY-y;
81
                                         float py = mX-x;
82
                                         float pz = 0;
83
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
84

  
85
                                         if( plen>0 )
86
                                           {
87
                                           px /= plen;
88
                                           py /= plen;
89
                                           pz /= plen;
90

  
91
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
92
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
93

  
94
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
95
                                           }
96
                                         }
97
                                       break;
98

  
99
         case MotionEvent.ACTION_UP  : mX = -1;
100
                                       mY = -1;
101

  
102
                                       float qx = mRenderer.mQuat1.getX();
103
                                       float qy = mRenderer.mQuat1.getY();
104
                                       float qz = mRenderer.mQuat1.getZ();
105
                                       float qw = mRenderer.mQuat1.getW();
106

  
107
                                       float rx = mRenderer.mQuat2.getX();
108
                                       float ry = mRenderer.mQuat2.getY();
109
                                       float rz = mRenderer.mQuat2.getZ();
110
                                       float rw = mRenderer.mQuat2.getW();
111

  
112
                                       // This is quaternion multiplication. (tx.ty.tz.tw)
113
                                       // is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw)
114
                                       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
115
                                       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
116
                                       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
117
                                       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
118

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

  
130
                                       break;
131
         }
132

  
133
      return true;
134
      }
135

  
136 59
}
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138 60

  

Also available in: Unified diff