Project

General

Profile

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

examples / src / main / java / org / distorted / examples / inflate / InflateSurfaceView.java @ 77a500b3

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.inflate;
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.util.AttributeSet;
27
import android.view.MotionEvent;
28
import android.widget.Toast;
29

    
30
import org.distorted.examples.R;
31

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

    
34
class InflateSurfaceView extends GLSurfaceView
35
{
36
    private int mX, mY;
37
    private InflateRenderer mRenderer;
38
	
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
   
41
    public InflateSurfaceView(Context context, AttributeSet attrs)
42
      {
43
      super(context,attrs);
44
    
45
      mX = -1;
46
      mY = -1;
47
      
48
      if(!isInEditMode())
49
        {
50
        mRenderer = new InflateRenderer(this);
51
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
52
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
53
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
54
        setRenderer(mRenderer);
55
        Toast.makeText(context, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
56
        }
57
      }
58

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

    
61
    public InflateRenderer getRenderer()
62
      {
63
      return mRenderer;
64
      }
65

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

    
93
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
94
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
95
                                         
96
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
97
                                           }
98
                                         }                             
99
                                       break;
100
                                       
101
         case MotionEvent.ACTION_UP  : mX = -1;
102
                                       mY = -1;
103
        	                           
104
                                       float qx = mRenderer.mQuat1.get1();
105
                                       float qy = mRenderer.mQuat1.get2();
106
                                       float qz = mRenderer.mQuat1.get3();
107
                                       float qw = mRenderer.mQuat1.get4();
108

    
109
                                       float rx = mRenderer.mQuat2.get1();
110
                                       float ry = mRenderer.mQuat2.get2();
111
                                       float rz = mRenderer.mQuat2.get3();
112
                                       float rw = mRenderer.mQuat2.get4();
113

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

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

    
(4-4/4)