Project

General

Profile

« Previous | Next » 

Revision 58059374

Added by Leszek Koltunski almost 8 years ago

Beginnings of the 'Multiblur' app.

View differences:

src/main/AndroidManifest.xml
44 44
        <activity android:name=".aroundtheworld.AroundTheWorldActivity"/>
45 45
        <activity android:name=".mirror.MirrorActivity"/>
46 46
        <activity android:name=".blur.BlurActivity"/>
47
        <activity android:name=".multiblur.MultiblurActivity"/>
47 48
    </application>
48 49
</manifest>
src/main/java/org/distorted/examples/TableOfContents.java
62 62
import org.distorted.examples.wind.WindActivity;
63 63
import org.distorted.examples.mirror.MirrorActivity;
64 64
import org.distorted.examples.blur.BlurActivity;
65
import org.distorted.examples.multiblur.MultiblurActivity;
65 66

  
66 67
///////////////////////////////////////////////////////////////////////////////////////////////////
67 68

  
......
336 337
      activityMapping.put(i++, BlurActivity.class);
337 338
   }
338 339

  
340
   {
341
      final Map<String, Object> item = new HashMap<>();
342
      item.put(ITEM_IMAGE, R.drawable.icon_example_multiblur);
343
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_multiblur));
344
      item.put(ITEM_SUBTITLE, getText(R.string.example_multiblur_subtitle));
345
      data.add(item);
346
      activityMapping.put(i++, MultiblurActivity.class);
347
   }
348

  
339 349
   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});
340 350
   setListAdapter(dataAdapter);  
341 351
      
src/main/java/org/distorted/examples/multiblur/MultiblurActivity.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.multiblur;
21

  
22
import android.app.Activity;
23
import android.os.Bundle;
24
import org.distorted.library.Distorted;
25

  
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

  
28
public class MultiblurActivity extends Activity
29
{
30
    private MultiblurSurfaceView mView;
31

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

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

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

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

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

  
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedEffects;
30
import org.distorted.library.DistortedFramebuffer;
31
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.MeshCubes;
34
import org.distorted.library.MeshObject;
35
import org.distorted.library.type.DynamicQuat;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38

  
39
import java.io.IOException;
40
import java.io.InputStream;
41

  
42
import javax.microedition.khronos.egl.EGLConfig;
43
import javax.microedition.khronos.opengles.GL10;
44

  
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

  
47
class MultiblurRenderer implements GLSurfaceView.Renderer
48
{
49
    private static final int[] MOVE_VEC =
50
        {
51
        +1,+1,+1,
52
        +1,-1,+1,
53
        +1,+1,-1,
54
        +1,-1,-1,
55
        -1,+1,+1,
56
        -1,-1,+1,
57
        -1,+1,-1,
58
        -1,-1,-1
59
        };
60

  
61
    private static final int NUM_OBJECTS = MOVE_VEC.length/3;
62

  
63
    private GLSurfaceView mView;
64
    private DistortedTexture mTex1, mTex2;
65
    private DistortedEffects[] mEffects;
66
    private Static3D[] mMoveVector;
67
    private MeshObject mMesh;
68
    private DistortedFramebuffer mScreen;
69
    private DynamicQuat mQuatInt1, mQuatInt2;
70
    private int mObjWidth, mObjHeight;
71

  
72
    Static4D mQuat1, mQuat2;
73
    int mScreenMin;
74
    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

  
77
    MultiblurRenderer(GLSurfaceView v)
78
      {
79
      mView = v;
80

  
81
      mMoveVector = new Static3D[NUM_OBJECTS];
82
      for(int i=0; i<NUM_OBJECTS; i++) mMoveVector[i] = new Static3D(0,0,0);
83

  
84
      mEffects = new DistortedEffects[NUM_OBJECTS];
85
      for(int i=0; i<NUM_OBJECTS; i++) mEffects[i] = new DistortedEffects();
86

  
87
      mMesh = new MeshCubes(1,1,false);
88

  
89
      mTex1 = new DistortedTexture(100,100);
90
      mTex2 = new DistortedTexture(100,100);
91

  
92
      mObjWidth = mTex1.getWidth();
93
      mObjHeight= mTex2.getHeight();
94

  
95
      mQuat1 = new Static4D(0,0,0,1);  // unity
96
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
97
      
98
      mQuatInt1 = new DynamicQuat(0,0.5f);
99
      mQuatInt2 = new DynamicQuat(0,0.5f);
100

  
101
      mQuatInt1.add(mQuat1);
102
      mQuatInt2.add(mQuat2);
103

  
104
      mScreen = new DistortedFramebuffer(0);
105
      }
106

  
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
   
109
    public void onDrawFrame(GL10 glUnused) 
110
      {
111
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
112

  
113
      long time = System.currentTimeMillis();
114

  
115
      for(int i=0; i<NUM_OBJECTS; i++)
116
        mScreen.renderTo( i<NUM_OBJECTS/2 ? mTex1:mTex2, mMesh, mEffects[i], time );
117
      }
118

  
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
    
121
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
122
      {
123
      mScreenMin = width<height ? width:height;
124
    	
125
      Static3D center = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, 0.0f );
126
      float factor = Math.min((0.15f*height)/mObjHeight,(0.15f*width )/mObjWidth);
127
      float xMove  =(width -factor*mObjWidth )/2;
128
      float yMove  =(height-factor*mObjHeight)/2;
129
      float size   = 1.3f*mObjWidth;
130

  
131
      for(int i=0; i<NUM_OBJECTS; i++)
132
        {
133
        mEffects[i].abortEffects(EffectTypes.MATRIX);
134

  
135
        mMoveVector[i].set(xMove,yMove,0);
136
        mEffects[i].move(mMoveVector[i]);
137
        mEffects[i].scale(factor);
138
        mEffects[i].quaternion(mQuatInt1, center);
139
        mEffects[i].quaternion(mQuatInt2, center);
140
        mMoveVector[i].set(size*MOVE_VEC[3*i], size*MOVE_VEC[3*i+1], size*MOVE_VEC[3*i+2]);
141
        mEffects[i].move(mMoveVector[i]);
142
        }
143

  
144
      mScreen.resize(width, height);
145
      }
146

  
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
    
149
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
150
      {
151
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
152

  
153
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.cat);
154
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.dog);
155
      Bitmap bitmap1,bitmap2;
156
        
157
      try 
158
        {
159
        bitmap1 = BitmapFactory.decodeStream(is1);
160
        bitmap2 = BitmapFactory.decodeStream(is2);
161
        }
162
      finally 
163
        {
164
        try 
165
          {
166
          is1.close();
167
          is2.close();
168
          }
169
        catch(IOException e) { }
170
        }  
171

  
172
      mTex1.setTexture(bitmap1);
173
      mTex2.setTexture(bitmap2);
174

  
175
      try
176
        {
177
        Distorted.onCreate(mView.getContext());
178
        }
179
      catch(Exception ex)
180
        {
181
        android.util.Log.e("Multiblur", ex.getMessage() );
182
        }
183
      }
184
}
src/main/java/org/distorted/examples/multiblur/MultiblurSurfaceView.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.multiblur;
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 MultiblurSurfaceView extends GLSurfaceView
35
{
36
    private int mX, mY;
37
    private MultiblurRenderer mRenderer;
38
	
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
   
41
    public MultiblurSurfaceView(Context context)
42
      {
43
      super(context);
44
    
45
      mX = -1;
46
      mY = -1;
47
      
48
      if(!isInEditMode())
49
        {
50
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
51
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
52
        android.util.Log.e("View", "Using OpenGL ES "+configurationInfo.getGlEsVersion());
53
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
54
        mRenderer = new MultiblurRenderer(this);
55
        setRenderer(mRenderer);
56
        Toast.makeText(context, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
57
        }
58
      }
59
    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
    
62
    @Override public boolean onTouchEvent(MotionEvent event) 
63
      {
64
      int action = event.getAction();
65
      int x = (int)event.getX();
66
      int y = (int)event.getY();
67
           
68
      switch(action)
69
         {
70
         case MotionEvent.ACTION_DOWN: mX = x;
71
                                       mY = y;
72
                                       break;
73
                                       
74
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
75
                                         {
76
                                         float px = mY-y;
77
                                         float py = mX-x;
78
                                         float pz = 0;
79
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
80
                                         
81
                                         if( plen>0 )
82
                                           {
83
                                           px /= plen;
84
                                           py /= plen;
85
                                           pz /= plen;
86

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

  
103
                                       float rx = mRenderer.mQuat2.getX();
104
                                       float ry = mRenderer.mQuat2.getY();
105
                                       float rz = mRenderer.mQuat2.getZ();
106
                                       float rw = mRenderer.mQuat2.getW();
107

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

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

  
src/main/res/values/strings.xml
133 133
    <string name="example_mirror_subtitle">Ping-pong between offscreen buffers to achieve the \'infinite mirror\' effect.</string>
134 134
    <string name="example_blur">Blur</string>
135 135
    <string name="example_blur_subtitle">Postprocessing effect: Blur.</string>
136
    <string name="example_multiblur">Multiblur</string>
137
    <string name="example_multiblur_subtitle">Blur multiple objects which obstruct each other.</string>
136 138

  
137 139
    <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>
138 140
    <string name="example_rotate_toast">Rotate the scene by swiping the screen</string>

Also available in: Unified diff