Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesSurfaceView.java @ bc0a685b

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

    
22
import org.distorted.examples.R;
23

    
24
import android.content.Context;
25
import android.opengl.GLSurfaceView;
26
import android.os.Build;
27
import android.util.AttributeSet;
28
import android.view.MotionEvent;
29
import android.widget.Toast;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

    
33
class CubesSurfaceView extends GLSurfaceView 
34
{
35
    int mX, mY;
36
    CubesRenderer mRenderer;
37
	
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39
   
40
    public CubesSurfaceView(Context c, AttributeSet attrs) 
41
      {
42
      super(c,attrs);
43
    
44
      mX = -1;
45
      mY = -1;
46
      
47
      if(!isInEditMode())
48
        {
49
        setEGLContextClientVersion(2);
50
        
51
        if( Build.FINGERPRINT.startsWith("generic") )
52
          { 
53
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
54
          }
55
    
56
        mRenderer = new CubesRenderer(this);
57
        
58
        setRenderer(mRenderer);
59
        
60
        Toast.makeText(c, R.string.example_cubes_toast , Toast.LENGTH_SHORT).show();   
61
        }
62
      }
63
    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
    
66
    @Override public boolean onTouchEvent(MotionEvent event) 
67
      {
68
      int action = event.getAction();
69
      int x = (int)event.getX();
70
      int y = (int)event.getY();
71
           
72
      switch(action)
73
         {
74
         case MotionEvent.ACTION_DOWN: mX = x;
75
                                       mY = y;
76
                                       break;
77
                                       
78
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
79
                                         {
80
                                         float px = mY-y;
81
                                         float py = mX-x;
82
                                         float pz = 0;
83
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
84
                                         
85
                                         if( plen>0 )
86
                                           {
87
                                           px /= plen;
88
                                           py /= plen;
89
                                           pz /= plen;
90

    
91
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
92
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
93
                                         
94
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
95
                                           }
96
                                         }                             
97
                                       break;
98
                                       
99
         case MotionEvent.ACTION_UP  : mX = -1;
100
                                       mY = -1;
101
        	                           
102
                                       float qx = mRenderer.mQuat1.getX();
103
                                       float qy = mRenderer.mQuat1.getY();
104
                                       float qz = mRenderer.mQuat1.getZ();
105
                                       float qw = mRenderer.mQuat1.getW();
106

    
107
                                       float rx = mRenderer.mQuat2.getX();
108
                                       float ry = mRenderer.mQuat2.getY();
109
                                       float rz = mRenderer.mQuat2.getZ();
110
                                       float rw = mRenderer.mQuat2.getW();
111

    
112
                                       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
113
                                       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
114
                                       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
115
                                       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
116
                                       
117
                                       mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
118
                                       mRenderer.mQuat2.set(tx, ty, tz, tw);
119
                                       
120
                                       break;
121
         }
122
             
123
      return true;
124
      }
125
         
126
}
127

    
(3-3/3)