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