Project

General

Profile

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

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

1 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 76a81b6a Leszek Koltunski
package org.distorted.examples.effects3d;
21 08f92d82 Leszek Koltunski
22
import android.content.Context;
23
import android.opengl.GLSurfaceView;
24
import android.util.AttributeSet;
25 833685d0 Leszek Koltunski
import android.view.MotionEvent;
26 08f92d82 Leszek Koltunski
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
29 76a81b6a Leszek Koltunski
class Effects3DSurfaceView extends GLSurfaceView
30 833685d0 Leszek Koltunski
  {
31
  private int mX, mY;
32 76a81b6a Leszek Koltunski
  private Effects3DRenderer mRenderer;
33 833685d0 Leszek Koltunski
34 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
35
   
36 76a81b6a Leszek Koltunski
  public Effects3DSurfaceView(Context c, AttributeSet attrs)
37 833685d0 Leszek Koltunski
    {
38
    super(c, attrs);
39 08f92d82 Leszek Koltunski
      
40 833685d0 Leszek Koltunski
    if(!isInEditMode())
41
      {
42
      setEGLContextClientVersion(2);
43 76a81b6a Leszek Koltunski
      mRenderer = new Effects3DRenderer(this);
44 833685d0 Leszek Koltunski
      setRenderer(mRenderer);
45 08f92d82 Leszek Koltunski
      }
46 833685d0 Leszek Koltunski
    }
47
48 d2337a3a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 76a81b6a Leszek Koltunski
  public Effects3DRenderer getRenderer()
51 d2337a3a Leszek Koltunski
    {
52
    return mRenderer;
53
    }
54
55 833685d0 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57
  @Override
58
  public boolean onTouchEvent(MotionEvent event)
59
    {
60
    int action = event.getAction();
61
    int x = (int)event.getX();
62
    int y = (int)event.getY();
63
64
    switch(action)
65
      {
66
      case MotionEvent.ACTION_DOWN: mX = x;
67
                                    mY = y;
68
                                    break;
69
70
      case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
71
                                      {
72
                                      float px = mY-y;
73
                                      float py = mX-x;
74
                                      float pz = 0;
75
                                      float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
76
77
                                      if( plen>0 )
78
                                        {
79
                                        px /= plen;
80
                                        py /= plen;
81
                                        pz /= plen;
82
83
                                        float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
84
                                        float sinA = (float)Math.sqrt(1-cosA*cosA);
85
86
                                        mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
87
                                        }
88
                                      }
89
                                    break;
90
91
      case MotionEvent.ACTION_UP  : mX = -1;
92
                                    mY = -1;
93
94
                                    float qx = mRenderer.mQuat1.getX();
95
                                    float qy = mRenderer.mQuat1.getY();
96
                                    float qz = mRenderer.mQuat1.getZ();
97
                                    float qw = mRenderer.mQuat1.getW();
98
99
                                    float rx = mRenderer.mQuat2.getX();
100
                                    float ry = mRenderer.mQuat2.getY();
101
                                    float rz = mRenderer.mQuat2.getZ();
102
                                    float rw = mRenderer.mQuat2.getW();
103
104
                                    float tx = rw*qx - rz*qy + ry*qz + rx*qw;
105
                                    float ty = rw*qy + rz*qx + ry*qw - rx*qz;
106
                                    float tz = rw*qz + rz*qw - ry*qx + rx*qy;
107
                                    float tw = rw*qw - rz*qz - ry*qy - rx*qx;
108
109
                                    mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
110
                                    mRenderer.mQuat2.set(tx, ty, tz, tw);
111
112
                                    break;
113
      }
114
115
    return true;
116
    }
117 08f92d82 Leszek Koltunski
}