Project

General

Profile

« Previous | Next » 

Revision 14122c52

Added by Leszek Koltunski over 7 years ago

Beginnings of the 'Wind' app.

View differences:

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

  
62 63
///////////////////////////////////////////////////////////////////////////////////////////////////
63 64

  
......
296 297
      activityMapping.put(i++, FlagActivity.class);
297 298
   }
298 299

  
300
   {
301
      final Map<String, Object> item = new HashMap<>();
302
      item.put(ITEM_IMAGE, R.drawable.icon_example_wind);
303
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_wind));
304
      item.put(ITEM_SUBTITLE, getText(R.string.example_wind_subtitle));
305
      data.add(item);
306
      activityMapping.put(i++, WindActivity.class);
307
   }
308

  
299 309
   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});
300 310
   setListAdapter(dataAdapter);  
301 311
      
src/main/java/org/distorted/examples/wind/WindActivity.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.wind;
21

  
22
import android.app.Activity;
23
import android.opengl.GLSurfaceView;
24
import android.os.Bundle;
25
import android.widget.SeekBar;
26
import android.widget.SeekBar.OnSeekBarChangeListener;
27
import android.widget.TextView;
28

  
29
import org.distorted.examples.R;
30
import org.distorted.library.Distorted;
31

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

  
34
public class WindActivity extends Activity implements OnSeekBarChangeListener
35
{
36
    private TextView windText;
37
    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39
    
40
    @Override
41
    protected void onCreate(Bundle icicle) 
42
      {
43
      super.onCreate(icicle);
44
      setContentView(R.layout.windlayout);
45
       
46
      SeekBar bar = (SeekBar)findViewById(R.id.windSeek);
47
      bar.setOnSeekBarChangeListener(this);
48
        
49
      windText = (TextView)findViewById(R.id.windText);
50
     
51
      bar.setProgress(50);
52
      
53
      windText.setText("Wind: 50");
54
      }
55

  
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
    
58
    @Override
59
    protected void onPause() 
60
      {
61
      GLSurfaceView view = (GLSurfaceView) this.findViewById(R.id.windSurfaceView);
62
      view.onPause();
63
      super.onPause();
64
      }
65

  
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
    
68
    @Override
69
    protected void onResume() 
70
      {
71
      super.onResume();
72
      GLSurfaceView view = (GLSurfaceView) this.findViewById(R.id.windSurfaceView);
73
      view.onResume();
74
      }
75
 
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
    
78
    @Override
79
    protected void onDestroy() 
80
      {
81
      Distorted.onDestroy();  
82
      super.onDestroy();
83
      }
84

  
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
    
87
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser) 
88
      {
89
      switch (bar.getId()) 
90
        {
91
        case R.id.windSeek:WindSurfaceView view = (WindSurfaceView) this.findViewById(R.id.windSurfaceView);
92
                           view.getRenderer().setWind(progress);
93
                           windText.setText("Wind: "+progress);
94
                           break;
95
        }
96
      }
97

  
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

  
100
    public void onStartTrackingTouch(SeekBar bar) { }
101
    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

  
104
    public void onStopTrackingTouch(SeekBar bar)  { }
105

  
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
}
src/main/java/org/distorted/examples/wind/WindRenderer.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.wind;
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.DistortedCubes;
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 WindRenderer implements GLSurfaceView.Renderer
45
{
46
   private GLSurfaceView mView;
47
   private DistortedObject mObject;
48
   private DynamicQuat mQuatInt1, mQuatInt2;
49
   private int mWind;
50
   private int mObjWidth, mObjHeight;
51

  
52
   Static4D mQuat1, mQuat2;
53
   int mScreenMin;
54

  
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

  
57
   WindRenderer(GLSurfaceView view)
58
      { 
59
      mView = view;
60

  
61
      mObject = new DistortedCubes(50,30,10,false);
62

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

  
66
      mQuat1 = new Static4D(           0,         0,           0,          1);  // unity quaternion
67
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);  // something semi-random that looks good
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
   void setWind(int wind)
79
      {
80
      mWind = wind;
81
      }
82
   
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
   
85
   public void onDrawFrame(GL10 glUnused) 
86
      {
87
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
88
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
89
     
90
      mObject.draw(System.currentTimeMillis());
91
      }
92

  
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
    
95
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
96
      {
97
      mScreenMin = width<height ? width:height;
98

  
99
      mObject.abortEffects(EffectTypes.MATRIX);
100
      float factor;
101

  
102
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
103
        {
104
        factor = (0.8f*height)/mObjHeight;
105
        }
106
      else
107
        {
108
        factor = (0.8f*width)/mObjWidth;
109
        }
110

  
111
      mObject.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
112
      mObject.scale(factor);
113
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
114

  
115
      mObject.quaternion(mQuatInt1, center);
116
      mObject.quaternion(mQuatInt2, center);
117

  
118
      Distorted.onSurfaceChanged(width, height);
119
      }
120

  
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
    
123
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
124
      {  
125
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
126
      Bitmap bitmap;
127

  
128
      try
129
        {
130
        bitmap = BitmapFactory.decodeStream(is);
131
        }
132
      finally
133
        {
134
        try
135
          {
136
          is.close();
137
          }
138
        catch(IOException e) { }
139
        }
140

  
141
      mObject.setBitmap(bitmap);
142

  
143
      try
144
        {
145
        Distorted.onSurfaceCreated(mView.getContext());
146
        }
147
      catch(Exception ex)
148
        {
149
        android.util.Log.e("Wind", ex.getMessage() );
150
        }
151
      }
152
}
src/main/java/org/distorted/examples/wind/WindSurfaceView.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.wind;
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 WindSurfaceView extends GLSurfaceView
31
{
32
    private int mX, mY;
33
    private WindRenderer mRenderer;
34

  
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
   
37
    public WindSurfaceView(Context c, AttributeSet attrs)
38
      {
39
      super(c, attrs);
40

  
41
      mX = -1;
42
      mY = -1;
43

  
44
      if(!isInEditMode())
45
        {
46
        setEGLContextClientVersion(2);
47
        
48
        if( Build.FINGERPRINT.startsWith("generic") )
49
          { 
50
          setEGLConfigChooser(8, 8, 8, 8, 16, 0);   
51
          }
52
    
53
        mRenderer = new WindRenderer(this);
54
        setRenderer(mRenderer);
55
        }
56
      }
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

  
59
    public WindRenderer getRenderer()
60
      {
61
      return mRenderer;
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
                                       // This is quaternion multiplication. (tx.ty.tz.tw)
113
                                       // is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw)
114
                                       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
115
                                       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
116
                                       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
117
                                       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
118

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

  
130
                                       break;
131
         }
132

  
133
      return true;
134
      }
135

  
136
}
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

  
src/main/res/layout/windlayout.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
    android:layout_width="fill_parent"
4
    android:layout_height="fill_parent"
5
    android:orientation="vertical" >
6

  
7
    <org.distorted.examples.wind.WindSurfaceView
8
        android:id="@+id/windSurfaceView"
9
        android:layout_width="fill_parent"
10
        android:layout_height="0dp"
11
        android:layout_weight="1" />
12

  
13
    <LinearLayout
14
        android:id="@+id/linearLayout1"
15
        android:layout_width="fill_parent"
16
        android:layout_height="wrap_content"
17
        android:gravity="center|fill_horizontal"
18
        android:orientation="horizontal"
19
        android:paddingBottom="10dp"
20
        android:paddingTop="10dp" >
21

  
22
        <TextView
23
            android:id="@+id/windText"
24
            android:layout_width="wrap_content"
25
            android:layout_height="wrap_content"
26
            android:layout_weight="1"
27
            android:paddingLeft="15dp"
28
            android:textAppearance="?android:attr/textAppearanceMedium" />
29

  
30
        <SeekBar
31
            android:id="@+id/windSeek"
32
            android:layout_width="106dp"
33
            android:layout_height="wrap_content"
34
            android:layout_weight="0.94"
35
            android:paddingLeft="15dp"
36
            android:paddingRight="10dp" />
37

  
38
    </LinearLayout>
39

  
40
</LinearLayout>
src/main/res/values/strings.xml
106 106
    <string name="example_save_subtitle">Saving the output to a PNG file.</string>
107 107
    <string name="example_flag">Waving flag</string>
108 108
    <string name="example_flag_subtitle">See the WAVE effect.</string>
109
    <string name="example_wind">Variable wind</string>
110
    <string name="example_wind_subtitle">A couple of effects put together to create an effect of a waving flag.</string>
109 111

  
110 112
    <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>
111 113
    <string name="example_rotate_toast">Rotate the scene by swiping the screen</string>

Also available in: Unified diff