Project

General

Profile

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

examples / src / main / java / org / distorted / examples / transparency / TransparencySurfaceView.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.transparency;
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
import android.widget.Toast;
29

    
30
import org.distorted.examples.R;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

    
34
class TransparencySurfaceView extends GLSurfaceView
35
{
36
    private final static int DIRECTION_SENSITIVITY=  12;
37
    private int mX, mY;
38
    private TransparencyRenderer mRenderer;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
    public TransparencySurfaceView(Context context, AttributeSet attrs)
43
      {
44
      super(context, attrs);
45

    
46
      mX = -1;
47
      mY = -1;
48

    
49
      if(!isInEditMode())
50
        {
51
        mRenderer = new TransparencyRenderer(this);
52
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
53
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
54
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
55
        setRenderer(mRenderer);
56
        Toast.makeText(context, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
57
        }
58
      }
59

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

    
62
    public TransparencyRenderer getRenderer()
63
      {
64
      return mRenderer;
65
      }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
    private void resetQuats()
70
      {
71
      float qx = mRenderer.mQuat1.get0();
72
      float qy = mRenderer.mQuat1.get1();
73
      float qz = mRenderer.mQuat1.get2();
74
      float qw = mRenderer.mQuat1.get3();
75

    
76
      float rx = mRenderer.mQuat2.get0();
77
      float ry = mRenderer.mQuat2.get1();
78
      float rz = mRenderer.mQuat2.get2();
79
      float rw = mRenderer.mQuat2.get3();
80

    
81
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
82
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
83
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
84
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
85

    
86
      mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
87
      mRenderer.mQuat2.set(tx, ty, tz, tw);
88
      }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
    
92
    @Override public boolean onTouchEvent(MotionEvent event) 
93
      {
94
      int action = event.getAction();
95
      int x = (int)event.getX();
96
      int y = (int)event.getY();
97
           
98
      switch(action)
99
         {
100
         case MotionEvent.ACTION_DOWN: mX = x;
101
                                       mY = y;
102
                                       break;
103
                                       
104
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
105
                                         {
106
                                         float px = mY-y;
107
                                         float py = mX-x;
108
                                         float pz = 0;
109
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
110
                                         
111
                                         if( plen>0 )
112
                                           {
113
                                           px /= plen;
114
                                           py /= plen;
115
                                           pz /= plen;
116

    
117
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
118
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
119
                                         
120
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
121
                                           }
122
                                         }
123
                                       if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mRenderer.mScreenMin*mRenderer.mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
124
                                         {
125
                                         mX = x;
126
                                         mY = y;
127
                                         resetQuats();
128
                                         }
129
                                       break;
130
                                       
131
         case MotionEvent.ACTION_UP  : mX = -1;
132
                                       mY = -1;
133
        	                             resetQuats();
134
                                       break;
135
         }
136
             
137
      return true;
138
      }
139
         
140
}
141

    
(3-3/3)