Project

General

Profile

« Previous | Next » 

Revision 5b4a2d76

Added by Leszek Koltunski about 5 years ago

1) new 'Rubik' app (skeleton)
2) MeshCubes: add support for custom texture mappings on each side (Rubik needs that!)

View differences:

src/main/AndroidManifest.xml
52 52
        <activity android:name=".glow.GlowActivity"/>
53 53
        <activity android:name=".movingglow.MovingGlowActivity"/>
54 54
        <activity android:name=".earth.EarthActivity"/>
55
        <activity android:name=".earth.EarthActivity"/>
55
        <activity android:name=".rubik.RubikActivity"/>
56 56
    </application>
57 57
</manifest>
src/main/java/org/distorted/examples/TableOfContents.java
69 69
import org.distorted.examples.glow.GlowActivity;
70 70
import org.distorted.examples.movingglow.MovingGlowActivity;
71 71
import org.distorted.examples.earth.EarthActivity;
72
import org.distorted.examples.rubik.RubikActivity;
72 73

  
73 74
///////////////////////////////////////////////////////////////////////////////////////////////////
74 75

  
......
406 407
   activityMapping.put(i++, EarthActivity.class);
407 408
   }
408 409

  
410
   {
411
   final Map<String, Object> item = new HashMap<>();
412
   item.put(ITEM_IMAGE, R.drawable.icon_example_wip);
413
   item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_rubik));
414
   item.put(ITEM_SUBTITLE, getText(R.string.example_rubik_subtitle));
415
   data.add(item);
416
   activityMapping.put(i++, RubikActivity.class);
417
   }
418

  
409 419
   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});
410 420
   setListAdapter(dataAdapter);  
411 421
      
src/main/java/org/distorted/examples/flag/FlagRenderer.java
97 97
      effects.apply( new MatrixEffectQuaternion(mQuat1, mCenter) );
98 98
      effects.apply( new MatrixEffectQuaternion(mQuat2, mCenter) );
99 99

  
100
      final int GRIDX = 50;
101
      final int GRIDY = 30;
102

  
103
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f      ,1.0f      );
104
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRIDX,1.0f      );
105
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f      ,1.0f/GRIDY);
106

  
100 107
      mScreen = new DistortedScreen();
101
      mScreen.attach(mTexture, effects, new MeshCubes(50,30,1) );
108
      mScreen.attach(mTexture, effects, new MeshCubes(GRIDX,GRIDY,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB) );
102 109
      }
103 110

  
104 111
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/examples/objecttree/ObjectTreeRenderer.java
44 44
import org.distorted.library.type.Dynamic1D;
45 45
import org.distorted.library.type.Static1D;
46 46
import org.distorted.library.type.Static3D;
47
import org.distorted.library.type.Static4D;
47 48

  
48 49
import android.graphics.Bitmap;
49 50
import android.graphics.BitmapFactory;
......
181 182
      DistortedEffects gridEffects = new DistortedEffects();
182 183

  
183 184
      final int GRID = 20;
185
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f     ,1.0f     );
186
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRID,1.0f     );
187
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f     ,1.0f/GRID);
184 188

  
185 189
      if( mMeshFlat==null ) mMeshFlat = new MeshFlat(1,1);
186
      if( mMeshCubes==null) mMeshCubes= new MeshCubes(GRID,GRID,1);
190
      if( mMeshCubes==null) mMeshCubes= new MeshCubes(GRID,GRID,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
191

  
192
      int gridDepth = mGridTexture.getDepth(mMeshCubes);
187 193

  
188 194
      mRoot = new DistortedNode(mLisaTexture, mEffects, mMeshFlat);
189 195
      mRoot.attach(mGridTexture,gridEffects,mMeshCubes);
......
209 215
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
210 216
      sinkDyn.add(new Static1D(1.0f));
211 217
      sinkDyn.add(new Static1D(0.3f));
212
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
218
      VertexEffectSink sink = new VertexEffectSink(sinkDyn, new Static3D(gridWidth/2.0f, gridHeight/2.0f, gridDepth/2.0f) );
213 219
      gridEffects.apply(sink);
214 220

  
215 221
      VertexEffectSink.enable();
src/main/java/org/distorted/examples/rubik/RubikActivity.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.rubik;
21

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

  
25
import org.distorted.library.main.Distorted;
26

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

  
29
public class RubikActivity extends Activity
30
{
31
    private RubikSurfaceView mView;
32

  
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

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

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

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

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

  
26
import org.distorted.examples.R;
27
import org.distorted.library.effect.MatrixEffectMove;
28
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.effect.VertexEffect;
31
import org.distorted.library.effect.VertexEffectDistort;
32
import org.distorted.library.main.Distorted;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.mesh.MeshCubes;
37
import org.distorted.library.mesh.MeshFlat;
38
import org.distorted.library.type.Dynamic3D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

  
42
import java.io.IOException;
43
import java.io.InputStream;
44

  
45
import javax.microedition.khronos.egl.EGLConfig;
46
import javax.microedition.khronos.opengles.GL10;
47

  
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

  
50
class RubikRenderer implements GLSurfaceView.Renderer
51
{
52
    private static final int CUBE_SIZE = 3;
53
    private static final int VERTICES  = 5;
54
    private static final int SIZE      = 200;
55

  
56
    private GLSurfaceView mView;
57
    private DistortedTexture mTexture;
58
    private DistortedScreen mScreen;
59
    private Static3D mMove, mScale, mCenter;
60
    private MeshCubes[][][] mCubes;
61
    private DistortedEffects[][][] mEffects;
62

  
63
    Static4D mQuat1, mQuat2;
64
    int mScreenMin;
65

  
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

  
68
    RubikRenderer(GLSurfaceView v)
69
      {
70
      mView = v;
71

  
72
      mScreen = new DistortedScreen();
73

  
74
      mQuat1 = new Static4D(           0,         0,           0,          1);  // unity quaternion
75
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);  // something semi-random that looks good
76

  
77
      mCubes = new MeshCubes[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
78
      mEffects = new DistortedEffects[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
79
      Static3D[][][] cubeVectors = new Static3D[CUBE_SIZE][CUBE_SIZE][CUBE_SIZE];
80

  
81
      mMove  = new Static3D(0,0,0);
82
      mScale = new Static3D(1,1,1);
83
      mCenter= new Static3D(0,0,0);
84

  
85
      MatrixEffectMove       move  = new MatrixEffectMove(mMove);
86
      MatrixEffectScale      scale = new MatrixEffectScale(mScale);
87
      MatrixEffectQuaternion quat1 = new MatrixEffectQuaternion(mQuat1, mCenter);
88
      MatrixEffectQuaternion quat2 = new MatrixEffectQuaternion(mQuat2, mCenter);
89

  
90
      for(int x=0; x<CUBE_SIZE; x++)
91
        for(int y=0; y<CUBE_SIZE; y++)
92
          for(int z=0; z<CUBE_SIZE; z++)
93
            {
94
            mCubes[x][y][z] = new MeshCubes(VERTICES,VERTICES,VERTICES);
95

  
96
            cubeVectors[x][y][z] = new Static3D( SIZE*(x-0.5f*(CUBE_SIZE-1)), SIZE*(y-0.5f*(CUBE_SIZE-1)), SIZE*(z-0.5f*(CUBE_SIZE-1)) );
97

  
98
            mEffects[x][y][z] = new DistortedEffects();
99

  
100
            mEffects[x][y][z].apply(move);
101
            mEffects[x][y][z].apply(scale);
102
            mEffects[x][y][z].apply(quat1);
103
            mEffects[x][y][z].apply(quat2);
104
            mEffects[x][y][z].apply( new MatrixEffectMove(cubeVectors[x][y][z]) );
105
            }
106
      }
107

  
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
   
110
    public void onDrawFrame(GL10 glUnused) 
111
      {
112
      mScreen.render( System.currentTimeMillis() );
113
      }
114

  
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
    
117
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
118
      {
119
      mScreenMin = width<height ? width:height;
120

  
121
      float w = mTexture.getWidth();
122
      float h = mTexture.getHeight();
123
      float d = mTexture.getDepth(mCubes[0][0][0]);
124

  
125
      float factor = 0.6f*(width>height ? height/h:width/w)/CUBE_SIZE;
126

  
127
      mCenter.set(w/2,h/2,d/2);
128
      mMove.set( (width-factor*w)/2 , (height-factor*h)/2 , -factor*d/2 );
129
      mScale.set(factor,factor,factor);
130

  
131
      mScreen.resize(width, height);
132
      }
133

  
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
    
136
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
137
      {
138
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
139
      Bitmap bitmap;
140
        
141
      try 
142
        {
143
        bitmap = BitmapFactory.decodeStream(is);
144
        } 
145
      finally 
146
        {
147
        try 
148
          {
149
          is.close();
150
          } 
151
        catch(IOException e) { }
152
        }  
153

  
154
      if( mTexture==null ) mTexture = new DistortedTexture(SIZE,SIZE);
155
      mTexture.setTexture(bitmap);
156

  
157
      mScreen.detachAll();
158

  
159
      for(int x=0; x<CUBE_SIZE; x++)
160
        for(int y=0; y<CUBE_SIZE; y++)
161
          for(int z=0; z<CUBE_SIZE; z++)
162
             mScreen.attach(mTexture,mEffects[x][y][z],mCubes[x][y][z]);
163

  
164
      try
165
        {
166
        Distorted.onCreate(mView.getContext());
167
        }
168
      catch(Exception ex)
169
        {
170
        android.util.Log.e("Rubik", ex.getMessage() );
171
        }
172
      }
173
}
src/main/java/org/distorted/examples/rubik/RubikSurfaceView.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.rubik;
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

  
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

  
31
class RubikSurfaceView extends GLSurfaceView
32
{
33
    private int mX, mY;
34
    private RubikRenderer mRenderer;
35

  
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

  
38
    public RubikSurfaceView(Context context)
39
      {
40
      super(context);
41

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

  
45
      mRenderer = new RubikRenderer(this);
46
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
47
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
48
      setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
49
      setRenderer(mRenderer);
50
      }
51

  
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

  
54
    public RubikRenderer getRenderer()
55
      {
56
      return mRenderer;
57
      }
58

  
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

  
61
    @Override public boolean onTouchEvent(MotionEvent event)
62
      {
63
      int action = event.getAction();
64
      int x = (int)event.getX();
65
      int y = (int)event.getY();
66

  
67
      switch(action)
68
         {
69
         case MotionEvent.ACTION_DOWN: mX = x;
70
                                       mY = y;
71
                                       break;
72

  
73
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
74
                                         {
75
                                         float px = mY-y;
76
                                         float py = mX-x;
77
                                         float pz = 0;
78
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
79

  
80
                                         if( plen>0 )
81
                                           {
82
                                           px /= plen;
83
                                           py /= plen;
84
                                           pz /= plen;
85

  
86
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
87
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
88

  
89
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
90
                                           }
91
                                         }
92
                                       break;
93

  
94
         case MotionEvent.ACTION_UP  : mX = -1;
95
                                       mY = -1;
96

  
97
                                       float qx = mRenderer.mQuat1.get1();
98
                                       float qy = mRenderer.mQuat1.get2();
99
                                       float qz = mRenderer.mQuat1.get3();
100
                                       float qw = mRenderer.mQuat1.get4();
101

  
102
                                       float rx = mRenderer.mQuat2.get1();
103
                                       float ry = mRenderer.mQuat2.get2();
104
                                       float rz = mRenderer.mQuat2.get3();
105
                                       float rw = mRenderer.mQuat2.get4();
106

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

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

  
125
                                       break;
126
         }
127

  
128
      return true;
129
      }
130

  
131
}
132

  
src/main/res/values/strings.xml
182 182
    <string name="example_moving_glow_subtitle">See moving objects glowing with light.</string>
183 183
    <string name="example_earth">Earth</string>
184 184
    <string name="example_earth_subtitle">Test the sphere Mesh by showing the Earth in cosmos.</string>
185
    <string name="example_rubik">Rubik Cube</string>
186
    <string name="example_rubik_subtitle">Moveable Rubik Cube.</string>
185 187

  
186 188
    <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>
187 189
    <string name="example_rotate_toast">Rotate the scene by swiping the screen</string>

Also available in: Unified diff