Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericSurfaceView.java @ ea9b68db

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