Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DSurfaceView.java @ 26b28407

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.vertex3d;
21

    
22
import android.content.Context;
23
import android.opengl.GLSurfaceView;
24
import android.os.Build;
25
import android.util.AttributeSet;
26
import android.view.MotionEvent;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

    
30
class Vertex3DSurfaceView extends GLSurfaceView
31
  {
32
  private int mX, mY;
33
  private Vertex3DRenderer mRenderer;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
   
37
  public Vertex3DSurfaceView(Context c, AttributeSet attrs)
38
    {
39
    super(c, attrs);
40
      
41
    if(!isInEditMode())
42
      {
43
      setEGLContextClientVersion(2);
44
        
45
      if( Build.FINGERPRINT.startsWith("generic") )
46
        {
47
        setEGLConfigChooser(8, 8, 8, 8, 16, 0);
48
        }
49

    
50
      mRenderer = new Vertex3DRenderer(this);
51

    
52
      setRenderer(mRenderer);
53
      }
54
    }
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  @Override
59
  public boolean onTouchEvent(MotionEvent event)
60
    {
61
    int action = event.getAction();
62
    int x = (int)event.getX();
63
    int y = (int)event.getY();
64

    
65
    switch(action)
66
      {
67
      case MotionEvent.ACTION_DOWN: mX = x;
68
                                    mY = y;
69
                                    break;
70

    
71
      case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
72
                                      {
73
                                      float px = mY-y;
74
                                      float py = mX-x;
75
                                      float pz = 0;
76
                                      float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
77

    
78
                                      if( plen>0 )
79
                                        {
80
                                        px /= plen;
81
                                        py /= plen;
82
                                        pz /= plen;
83

    
84
                                        float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
85
                                        float sinA = (float)Math.sqrt(1-cosA*cosA);
86

    
87
                                        mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
88
                                        }
89
                                      }
90
                                    break;
91

    
92
      case MotionEvent.ACTION_UP  : mX = -1;
93
                                    mY = -1;
94

    
95
                                    float qx = mRenderer.mQuat1.getX();
96
                                    float qy = mRenderer.mQuat1.getY();
97
                                    float qz = mRenderer.mQuat1.getZ();
98
                                    float qw = mRenderer.mQuat1.getW();
99

    
100
                                    float rx = mRenderer.mQuat2.getX();
101
                                    float ry = mRenderer.mQuat2.getY();
102
                                    float rz = mRenderer.mQuat2.getZ();
103
                                    float rw = mRenderer.mQuat2.getW();
104

    
105
                                    float tx = rw*qx - rz*qy + ry*qz + rx*qw;
106
                                    float ty = rw*qy + rz*qx + ry*qw - rx*qz;
107
                                    float tz = rw*qz + rz*qw - ry*qx + rx*qy;
108
                                    float tw = rw*qw - rz*qz - ry*qy - rx*qx;
109

    
110
                                    mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
111
                                    mRenderer.mQuat2.set(tx, ty, tz, tw);
112

    
113
                                    break;
114
      }
115

    
116
    return true;
117
    }
118
}
119

    
(3-3/3)