1 |
77a500b3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2016 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
71c8884f
|
Leszek Koltunski
|
// This file is part of Distorted. //
|
5 |
77a500b3
|
Leszek Koltunski
|
// //
|
6 |
71c8884f
|
Leszek Koltunski
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
77a500b3
|
Leszek Koltunski
|
// 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 |
71c8884f
|
Leszek Koltunski
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
77a500b3
|
Leszek Koltunski
|
// 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 |
71c8884f
|
Leszek Koltunski
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
77a500b3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.examples.inflate;
|
21 |
|
|
|
22 |
64558e4e
|
Leszek Koltunski
|
import org.distorted.examples.R;
|
23 |
|
|
|
24 |
77a500b3
|
Leszek Koltunski
|
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 InflateSurfaceView extends GLSurfaceView
|
35 |
|
|
{
|
36 |
ea9b68db
|
Leszek Koltunski
|
private final static int DIRECTION_SENSITIVITY= 12;
|
37 |
77a500b3
|
Leszek Koltunski
|
private int mX, mY;
|
38 |
|
|
private InflateRenderer mRenderer;
|
39 |
|
|
|
40 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41 |
|
|
|
42 |
|
|
public InflateSurfaceView(Context context, AttributeSet attrs)
|
43 |
|
|
{
|
44 |
|
|
super(context,attrs);
|
45 |
|
|
|
46 |
|
|
mX = -1;
|
47 |
|
|
mY = -1;
|
48 |
|
|
|
49 |
|
|
if(!isInEditMode())
|
50 |
|
|
{
|
51 |
|
|
mRenderer = new InflateRenderer(this);
|
52 |
|
|
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
53 |
|
|
final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
|
54 |
|
|
setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
|
55 |
|
|
setRenderer(mRenderer);
|
56 |
|
|
Toast.makeText(context, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
|
57 |
|
|
}
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61 |
|
|
|
62 |
|
|
public InflateRenderer getRenderer()
|
63 |
|
|
{
|
64 |
|
|
return mRenderer;
|
65 |
|
|
}
|
66 |
|
|
|
67 |
ea9b68db
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
68 |
|
|
|
69 |
|
|
private void resetQuats()
|
70 |
|
|
{
|
71 |
|
|
float qx = mRenderer.mQuat1.get0();
|
72 |
|
|
float qy = mRenderer.mQuat1.get1();
|
73 |
|
|
float qz = mRenderer.mQuat1.get2();
|
74 |
|
|
float qw = mRenderer.mQuat1.get3();
|
75 |
|
|
|
76 |
|
|
float rx = mRenderer.mQuat2.get0();
|
77 |
|
|
float ry = mRenderer.mQuat2.get1();
|
78 |
|
|
float rz = mRenderer.mQuat2.get2();
|
79 |
|
|
float rw = mRenderer.mQuat2.get3();
|
80 |
|
|
|
81 |
|
|
float tx = rw*qx - rz*qy + ry*qz + rx*qw;
|
82 |
|
|
float ty = rw*qy + rz*qx + ry*qw - rx*qz;
|
83 |
|
|
float tz = rw*qz + rz*qw - ry*qx + rx*qy;
|
84 |
|
|
float tw = rw*qw - rz*qz - ry*qy - rx*qx;
|
85 |
|
|
|
86 |
|
|
mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
|
87 |
|
|
mRenderer.mQuat2.set(tx, ty, tz, tw);
|
88 |
|
|
}
|
89 |
|
|
|
90 |
77a500b3
|
Leszek Koltunski
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
91 |
|
|
|
92 |
|
|
@Override public boolean onTouchEvent(MotionEvent event)
|
93 |
|
|
{
|
94 |
|
|
int action = event.getAction();
|
95 |
|
|
int x = (int)event.getX();
|
96 |
|
|
int y = (int)event.getY();
|
97 |
|
|
|
98 |
|
|
switch(action)
|
99 |
|
|
{
|
100 |
|
|
case MotionEvent.ACTION_DOWN: mX = x;
|
101 |
|
|
mY = y;
|
102 |
|
|
break;
|
103 |
|
|
|
104 |
|
|
case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
|
105 |
|
|
{
|
106 |
|
|
float px = mY-y;
|
107 |
|
|
float py = mX-x;
|
108 |
|
|
float pz = 0;
|
109 |
|
|
float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
|
110 |
|
|
|
111 |
|
|
if( plen>0 )
|
112 |
|
|
{
|
113 |
|
|
px /= plen;
|
114 |
|
|
py /= plen;
|
115 |
|
|
pz /= plen;
|
116 |
|
|
|
117 |
|
|
float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
|
118 |
|
|
float sinA = (float)Math.sqrt(1-cosA*cosA);
|
119 |
|
|
|
120 |
|
|
mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
|
121 |
|
|
}
|
122 |
ea9b68db
|
Leszek Koltunski
|
}
|
123 |
|
|
if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mRenderer.mScreenMin*mRenderer.mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
|
124 |
|
|
{
|
125 |
|
|
mX = x;
|
126 |
|
|
mY = y;
|
127 |
|
|
resetQuats();
|
128 |
|
|
}
|
129 |
77a500b3
|
Leszek Koltunski
|
break;
|
130 |
|
|
|
131 |
|
|
case MotionEvent.ACTION_UP : mX = -1;
|
132 |
|
|
mY = -1;
|
133 |
ea9b68db
|
Leszek Koltunski
|
resetQuats();
|
134 |
77a500b3
|
Leszek Koltunski
|
break;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
return true;
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
}
|