Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesSurfaceView.java @ 41a81a14

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.app.ActivityManager;
25
import android.content.Context;
26
import android.content.pm.ConfigurationInfo;
27
import android.opengl.GLSurfaceView;
28
import android.util.AttributeSet;
29
import android.view.MotionEvent;
30
import android.widget.Toast;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
class CubesSurfaceView extends GLSurfaceView 
35
{
36
    private int mX, mY;
37
    private CubesRenderer mRenderer;
38
	
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
   
41
    public CubesSurfaceView(Context context, AttributeSet attrs)
42
      {
43
      super(context,attrs);
44
    
45
      mX = -1;
46
      mY = -1;
47
      
48
      if(!isInEditMode())
49
        {
50
        final ActivityManager activityManager     = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
51
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
52
        android.util.Log.e("View", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
53
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
54
        mRenderer = new CubesRenderer(this);
55
        setRenderer(mRenderer);
56
        Toast.makeText(context, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
57
        }
58
      }
59
    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
    
62
    @Override public boolean onTouchEvent(MotionEvent event) 
63
      {
64
      int action = event.getAction();
65
      int x = (int)event.getX();
66
      int y = (int)event.getY();
67
           
68
      switch(action)
69
         {
70
         case MotionEvent.ACTION_DOWN: mX = x;
71
                                       mY = y;
72
                                       break;
73
                                       
74
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
75
                                         {
76
                                         float px = mY-y;
77
                                         float py = mX-x;
78
                                         float pz = 0;
79
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
80
                                         
81
                                         if( plen>0 )
82
                                           {
83
                                           px /= plen;
84
                                           py /= plen;
85
                                           pz /= plen;
86

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

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

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

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

    
(3-3/3)