Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshjoin / MeshJoinSurfaceView.java @ ea9b68db

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

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.opengl.GLSurfaceView;
26
import android.view.MotionEvent;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
class MeshJoinSurfaceView extends GLSurfaceView
31
{
32
    private final static int DIRECTION_SENSITIVITY=  12;
33
    private int mX, mY;
34
    private MeshJoinRenderer mRenderer;
35
	
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
   
38
    public MeshJoinSurfaceView(Context context)
39
      {
40
      super(context);
41
    
42
      mX = -1;
43
      mY = -1;
44

    
45
      mRenderer = new MeshJoinRenderer(this);
46
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
47
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
48
      setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
49
      setRenderer(mRenderer);
50
      }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
    public MeshJoinRenderer getRenderer()
55
      {
56
      return mRenderer;
57
      }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
    private void resetQuats()
62
      {
63
      float qx = mRenderer.mQuat1.get0();
64
      float qy = mRenderer.mQuat1.get1();
65
      float qz = mRenderer.mQuat1.get2();
66
      float qw = mRenderer.mQuat1.get3();
67

    
68
      float rx = mRenderer.mQuat2.get0();
69
      float ry = mRenderer.mQuat2.get1();
70
      float rz = mRenderer.mQuat2.get2();
71
      float rw = mRenderer.mQuat2.get3();
72

    
73
       // This is quaternion multiplication. (tx,ty,tz,tw)
74
       // is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw)
75
       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
76
       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
77
       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
78
       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
79

    
80
       // The point of this is so that there are always
81
       // exactly 2 quaternions: Quat1 representing the rotation
82
       // accumulating only since the last screen touch, and Quat2
83
       // which remembers the combined effect of all previous
84
       // swipes.
85
       // We cannot be accumulating an ever-growing list of quaternions
86
       // and add a new one every time user swipes the screen - there
87
       // is a limited number of slots in the EffectQueueMatrix!
88
       mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
89
       mRenderer.mQuat2.set(tx, ty, tz, tw);
90
       }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
    @Override public boolean onTouchEvent(MotionEvent event) 
95
      {
96
      int action = event.getAction();
97
      int x = (int)event.getX();
98
      int y = (int)event.getY();
99
           
100
      switch(action)
101
         {
102
         case MotionEvent.ACTION_DOWN: mX = x;
103
                                       mY = y;
104
                                       break;
105
                                       
106
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
107
                                         {
108
                                         float px = mY-y;
109
                                         float py = mX-x;
110
                                         float pz = 0;
111
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
112
                                         
113
                                         if( plen>0 )
114
                                           {
115
                                           px /= plen;
116
                                           py /= plen;
117
                                           pz /= plen;
118

    
119
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
120
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
121
                                         
122
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
123
                                           }
124
                                         }
125

    
126
                                       if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mRenderer.mScreenMin*mRenderer.mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
127
                                         {
128
                                         mX = x;
129
                                         mY = y;
130
                                         resetQuats();
131
                                         }
132

    
133
                                       break;
134
                                       
135
         case MotionEvent.ACTION_UP  : mX = -1;
136
                                       mY = -1;
137
                                       resetQuats();
138
                                       break;
139
         }
140
             
141
      return true;
142
      }
143
         
144
}
145

    
(3-3/3)