Project

General

Profile

« Previous | Next » 

Revision 175f355d

Added by Leszek Koltunski about 8 years ago

Beginnings of a new example app - a waving flag.

No actual waving yet!

View differences:

src/main/AndroidManifest.xml
38 38
        <activity android:name=".effects3d.Effects3DActivity" />
39 39
        <activity android:name=".plainmonalisa.PlainMonaLisaActivity" />
40 40
        <activity android:name=".save.SaveActivity"/>
41
        <activity android:name=".flag.FlagActivity"/>
41 42
    </application>
42 43
</manifest>
src/main/java/org/distorted/examples/TableOfContents.java
57 57
import org.distorted.examples.effects3d.Effects3DActivity;
58 58
import org.distorted.examples.plainmonalisa.PlainMonaLisaActivity;
59 59
import org.distorted.examples.save.SaveActivity;
60
import org.distorted.examples.flag.FlagActivity;
60 61

  
61 62
///////////////////////////////////////////////////////////////////////////////////////////////////
62 63

  
......
286 287
      activityMapping.put(i++, SaveActivity.class);
287 288
   }
288 289

  
290
   {
291
      final Map<String, Object> item = new HashMap<>();
292
      item.put(ITEM_IMAGE, R.drawable.icon_example_flag);
293
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_flag));
294
      item.put(ITEM_SUBTITLE, getText(R.string.example_flag_subtitle));
295
      data.add(item);
296
      activityMapping.put(i++, FlagActivity.class);
297
   }
298

  
289 299
   final SimpleAdapter dataAdapter = new SimpleAdapter(this, data, R.layout.toc_item, new String[] {ITEM_IMAGE, ITEM_TITLE, ITEM_SUBTITLE}, new int[] {R.id.Image, R.id.Title, R.id.SubTitle});
290 300
   setListAdapter(dataAdapter);  
291 301
      
src/main/java/org/distorted/examples/cubes/CubesSurfaceView.java
57 57
        
58 58
        setRenderer(mRenderer);
59 59
        
60
        Toast.makeText(c, R.string.example_cubes_toast , Toast.LENGTH_SHORT).show();   
60
        Toast.makeText(c, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
61 61
        }
62 62
      }
63 63
    
src/main/java/org/distorted/examples/flag/FlagActivity.java
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.flag;
21

  
22
import android.app.Activity;
23
import android.os.Bundle;
24

  
25
import org.distorted.library.Distorted;
26

  
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

  
29
public class FlagActivity extends Activity
30
{
31
    private FlagSurfaceView mView;
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
    
35
    @Override
36
    protected void onCreate(Bundle savedState) 
37
      {
38
      super.onCreate(savedState);
39
      mView = new FlagSurfaceView(this);
40
      setContentView(mView);
41
      }
42

  
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
    
45
    @Override
46
    protected void onPause() 
47
      {
48
      mView.onPause();
49
      super.onPause();
50
      }
51

  
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
    
54
    @Override
55
    protected void onResume() 
56
      {
57
      super.onResume();
58
      mView.onResume();
59
      }
60
    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
    
63
    @Override
64
    protected void onDestroy() 
65
      {
66
      Distorted.onDestroy();  
67
      super.onDestroy();
68
      }
69
}
src/main/java/org/distorted/examples/flag/FlagRenderer.java
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.flag;
21

  
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLES20;
25
import android.opengl.GLSurfaceView;
26

  
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedBitmap;
30
import org.distorted.library.DistortedObject;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.DynamicQuat;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35

  
36
import java.io.IOException;
37
import java.io.InputStream;
38

  
39
import javax.microedition.khronos.egl.EGLConfig;
40
import javax.microedition.khronos.opengles.GL10;
41

  
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

  
44
class FlagRenderer implements GLSurfaceView.Renderer
45
{
46
    private GLSurfaceView mView;
47
    private DistortedObject mObject;
48
    private int mObjWidth, mObjHeight;
49
    private DynamicQuat mQuatInt1, mQuatInt2;
50

  
51
    Static4D mQuat1, mQuat2;
52

  
53
    int mScreenMin;
54
    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

  
57
    FlagRenderer(GLSurfaceView v)
58
      {
59
      mView = v;
60

  
61
      mObject = new DistortedBitmap(100,60,30);
62

  
63
      mObjWidth = mObject.getWidth();
64
      mObjHeight= mObject.getHeight();
65

  
66
      mQuat1 = new Static4D(0,0,0,1);  // unity
67
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
68

  
69
      mQuatInt1 = new DynamicQuat(0,0.5f);
70
      mQuatInt2 = new DynamicQuat(0,0.5f);
71

  
72
      mQuatInt1.add(mQuat1);
73
      mQuatInt2.add(mQuat2);
74
      }
75

  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
   
78
    public void onDrawFrame(GL10 glUnused) 
79
      {
80
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
81
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
82
      
83
      mObject.draw(System.currentTimeMillis());
84
      }
85

  
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
    
88
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
89
      {
90
      mScreenMin = width<height ? width:height;
91
    	
92
      mObject.abortEffects(EffectTypes.MATRIX);
93
      float factor;
94

  
95
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
96
        {
97
        factor = (0.8f*height)/mObjHeight;
98
        }
99
      else
100
        {
101
        factor = (0.8f*width)/mObjWidth;
102
        }
103

  
104
      mObject.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
105
      mObject.scale(factor);
106
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
107

  
108
      mObject.quaternion(mQuatInt1, center);
109
      mObject.quaternion(mQuatInt2, center);
110
       
111
      Distorted.onSurfaceChanged(width, height); 
112
      }
113

  
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
    
116
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
117
      {
118
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
119
      Bitmap bitmap;
120
        
121
      try 
122
        {
123
        bitmap = BitmapFactory.decodeStream(is);
124
        } 
125
      finally 
126
        {
127
        try 
128
          {
129
          is.close();
130
          } 
131
        catch(IOException e) { }
132
        }  
133
      
134
      mObject.setBitmap(bitmap);
135
      
136
      try
137
        {
138
        Distorted.onSurfaceCreated(mView.getContext());
139
        }
140
      catch(Exception ex)
141
        {
142
        android.util.Log.e("Cubes", ex.getMessage() );
143
        }
144
      }
145
}
src/main/java/org/distorted/examples/flag/FlagSurfaceView.java
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.flag;
21

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

  
28
import org.distorted.examples.R;
29

  
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

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

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

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

  
111
                                       // This is quaternion multiplication. (tx.ty.tz.tw)
112
                                       // is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw)
113
                                       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
114
                                       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
115
                                       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
116
                                       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
117

  
118
                                       // The point of this is so that there are always
119
                                       // exactly 2 quaternions: Quat1 representing the rotation
120
                                       // accumulating only since the last screen touch, and Quat2
121
                                       // which remembers the combined effect of all previous
122
                                       // swipes.
123
                                       // We cannot be accumulating an ever-growing list of quaternions
124
                                       // and add a new one every time user swipes the screen - there
125
                                       // is a limited number of slots in the EffectQueueMatrix!
126
                                       mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
127
                                       mRenderer.mQuat2.set(tx, ty, tz, tw);
128
                                       
129
                                       break;
130
         }
131
             
132
      return true;
133
      }
134
         
135
}
136

  
src/main/res/values/strings.xml
100 100
    <string name="example_plainmonalisa_subtitle">MonaLisa rendered on a plain SurfaceView</string>
101 101
    <string name="example_save">Save to PNG</string>
102 102
    <string name="example_save_subtitle">Saving the output to a PNG file.</string>
103
    <string name="example_flag">Waving flag</string>
104
    <string name="example_flag_subtitle">See the WAVE effect.</string>
103 105

  
104 106
    <string name="example_movingeffects_toast">Click on \'RESET\' and define your path by touching the screen. Then click on one of the effects and see it move along your path.</string>
105
    <string name="example_cubes_toast">Rotate the cubes by swiping the screen</string>
107
    <string name="example_rotate_toast">Rotate the scene by swiping the screen</string>
106 108
    <string name="example_effects2d_toast">Failed to add new effect</string>
107 109
</resources>

Also available in: Unified diff