Project

General

Profile

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

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

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.generic;
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

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
class GenericSurfaceView extends GLSurfaceView
32
  {
33
  private final static int DIRECTION_SENSITIVITY=  12;
34
  private int mX, mY;
35
  private GenericRenderer mRenderer;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
   
39
  public GenericSurfaceView(Context context, AttributeSet attrs)
40
    {
41
    super(context, attrs);
42
      
43
    if(!isInEditMode())
44
      {
45
      mRenderer = new GenericRenderer(this);
46
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
47
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
48
      setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
49
      setRenderer(mRenderer);
50
      }
51
    }
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  public GenericRenderer getRenderer()
56
    {
57
    return mRenderer;
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
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
                                    break;
125

    
126
      case MotionEvent.ACTION_UP  : mX = -1;
127
                                    mY = -1;
128
                                    resetQuats();
129
                                    break;
130
      }
131

    
132
    return true;
133
    }
134
}
135

    
(6-6/8)