Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DSurfaceView.java @ fe59d375

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.effects3d;
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 Effects3DSurfaceView extends GLSurfaceView
32
  {
33
  private int mX, mY;
34
  private Effects3DRenderer mRenderer;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
   
38
  public Effects3DSurfaceView(Context context, AttributeSet attrs)
39
    {
40
    super(context, attrs);
41
      
42
    if(!isInEditMode())
43
      {
44
      final ActivityManager activityManager     = (android.app.ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
45
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
46
      android.util.Log.e("View", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
47
      setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
48
      mRenderer = new Effects3DRenderer(this);
49
      setRenderer(mRenderer);
50
      }
51
    }
52

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

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  @Override
63
  public boolean onTouchEvent(MotionEvent event)
64
    {
65
    int action = event.getAction();
66
    int x = (int)event.getX();
67
    int y = (int)event.getY();
68

    
69
    switch(action)
70
      {
71
      case MotionEvent.ACTION_DOWN: mX = x;
72
                                    mY = y;
73
                                    break;
74

    
75
      case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
76
                                      {
77
                                      float px = mY-y;
78
                                      float py = mX-x;
79
                                      float pz = 0;
80
                                      float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
81

    
82
                                      if( plen>0 )
83
                                        {
84
                                        px /= plen;
85
                                        py /= plen;
86
                                        pz /= plen;
87

    
88
                                        float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
89
                                        float sinA = (float)Math.sqrt(1-cosA*cosA);
90

    
91
                                        mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
92
                                        }
93
                                      }
94
                                    break;
95

    
96
      case MotionEvent.ACTION_UP  : mX = -1;
97
                                    mY = -1;
98

    
99
                                    float qx = mRenderer.mQuat1.getX();
100
                                    float qy = mRenderer.mQuat1.getY();
101
                                    float qz = mRenderer.mQuat1.getZ();
102
                                    float qw = mRenderer.mQuat1.getW();
103

    
104
                                    float rx = mRenderer.mQuat2.getX();
105
                                    float ry = mRenderer.mQuat2.getY();
106
                                    float rz = mRenderer.mQuat2.getZ();
107
                                    float rw = mRenderer.mQuat2.getW();
108

    
109
                                    float tx = rw*qx - rz*qy + ry*qz + rx*qw;
110
                                    float ty = rw*qy + rz*qx + ry*qw - rx*qz;
111
                                    float tz = rw*qz + rz*qw - ry*qx + rx*qy;
112
                                    float tw = rw*qw - rz*qz - ry*qy - rx*qx;
113

    
114
                                    mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
115
                                    mRenderer.mQuat2.set(tx, ty, tz, tw);
116

    
117
                                    break;
118
      }
119

    
120
    return true;
121
    }
122
}
123

    
(4-4/4)