Project

General

Profile

« Previous | Next » 

Revision 90940caf

Added by Leszek Koltunski almost 4 years ago

Skeleton of the SingleMesh app.

View differences:

src/main/AndroidManifest.xml
60 60
        <activity android:name=".predeform.PredeformActivity"/>
61 61
        <activity android:name=".predeform.PredeformActivity2"/>
62 62
        <activity android:name=".deferredjob.DeferredJobActivity"/>
63
        <activity android:name=".singlemesh.SingleMeshActivity"/>
63 64
    </application>
64 65
</manifest>
src/main/java/org/distorted/examples/TableOfContents.java
73 73
import org.distorted.examples.rubik.RubikActivity;
74 74
import org.distorted.examples.meshjoin.MeshJoinActivity;
75 75
import org.distorted.examples.predeform.PredeformActivity;
76
import org.distorted.examples.singlemesh.SingleMeshActivity;
76 77

  
77 78
///////////////////////////////////////////////////////////////////////////////////////////////////
78 79

  
......
123 124
    MESHJOIN          (R.drawable.icon_example_meshjoin        , R.string.example_meshjoin           , R.string.example_meshjoin_subtitle           ,            MeshJoinActivity.class),
124 125
    PREDEFORM         (R.drawable.icon_example_predeform       , R.string.example_predeform           , R.string.example_predeform_subtitle           ,            PredeformActivity.class),
125 126
    DEFERREDJOB       (R.drawable.icon_example_deferredjob     , R.string.example_deferredjob           , R.string.example_deferredjob_subtitle           ,            DeferredJobActivity.class),
127
    SINGLEMESH        (R.drawable.icon_example_wip             , R.string.example_singlemesh           , R.string.example_singlemesh_subtitle           ,            SingleMeshActivity.class),
126 128
    ;
127 129

  
128 130
    final int icon, title, subtitle;
src/main/java/org/distorted/examples/singlemesh/SingleMeshActivity.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.singlemesh;
21

  
22
import android.app.Activity;
23
import android.opengl.GLSurfaceView;
24
import android.os.Bundle;
25
import android.view.View;
26
import android.widget.Button;
27

  
28
import org.distorted.examples.R;
29
import org.distorted.library.main.DistortedLibrary;
30

  
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

  
33
public class SingleMeshActivity extends Activity
34
{
35
    @Override
36
    protected void onCreate(Bundle icicle) 
37
      {
38
      super.onCreate(icicle);
39
      setContentView(R.layout.singlemeshlayout);
40
      }
41

  
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
    
44
    @Override
45
    protected void onPause() 
46
      {
47
      GLSurfaceView view = this.findViewById(R.id.singlemeshSurfaceView);
48
      view.onPause();
49
      DistortedLibrary.onPause();
50
      super.onPause();
51
      }
52

  
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
    
55
    @Override
56
    protected void onResume() 
57
      {
58
      super.onResume();
59
      GLSurfaceView view = this.findViewById(R.id.singlemeshSurfaceView);
60
      view.onResume();
61
      }
62
    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
    
65
    @Override
66
    protected void onDestroy() 
67
      {
68
      DistortedLibrary.onDestroy();
69
      super.onDestroy();
70
      }
71

  
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

  
74
    public void onClick(View view)
75
      {
76
      Button butt = (Button)view;
77
      int id = butt.getId();
78
      SingleMeshSurfaceView sView = findViewById(R.id.singlemeshSurfaceView);
79

  
80
      switch(id)
81
        {
82
        case R.id.singlemeshButton0 : sView.getRenderer().apply(0); break;
83
        case R.id.singlemeshButton1 : sView.getRenderer().apply(1); break;
84
        }
85
      }
86
}
src/main/java/org/distorted/examples/singlemesh/SingleMeshRenderer.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.singlemesh;
21

  
22
import android.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25
import android.opengl.GLSurfaceView;
26

  
27
import org.distorted.library.effect.EffectType;
28
import org.distorted.library.effect.MatrixEffectQuaternion;
29
import org.distorted.library.effect.MatrixEffectScale;
30
import org.distorted.library.effect.VertexEffectMove;
31
import org.distorted.library.effect.VertexEffectRotate;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedLibrary;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.mesh.MeshBase;
37
import org.distorted.library.mesh.MeshJoined;
38
import org.distorted.library.mesh.MeshRectangles;
39
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.DynamicQuat;
41
import org.distorted.library.type.Static1D;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44

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

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

  
50
class SingleMeshRenderer implements GLSurfaceView.Renderer
51
{
52
    private GLSurfaceView mView;
53
    private DistortedTexture mTexture;
54
    private DistortedScreen mScreen;
55
    private DistortedEffects mEffects;
56
    private Static3D mScale;
57
    private MeshBase mMesh;
58
    private VertexEffectRotate mRotate;
59
    private Dynamic1D mAngleDyn;
60
    private Static1D mAngle;
61

  
62
    Static4D mQuat1, mQuat2;
63
    int mScreenMin;
64

  
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

  
67
    SingleMeshRenderer(GLSurfaceView v)
68
      {
69
      mView = v;
70
      mScreen = new DistortedScreen();
71
      mScale= new Static3D(1,1,1);
72
      Static3D center=new Static3D(0,0,0);
73

  
74
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
75
      sink.add( new Static1D(0.5f) );
76
      sink.add( new Static1D(2.0f) );
77

  
78
      mQuat1 = new Static4D(0,0,0,1);  // unity
79
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
80

  
81
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
82
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
83

  
84
      quatInt1.add(mQuat1);
85
      quatInt2.add(mQuat2);
86

  
87
      mAngle = new Static1D(0);
88

  
89
      mAngleDyn = new Dynamic1D(2000,0.5f);
90
      mAngleDyn.add(new Static1D(0));
91
      mAngleDyn.add( mAngle );
92

  
93
      mRotate = new VertexEffectRotate( mAngleDyn, new Static3D(1,0,0), new Static3D(0,0,0) );
94

  
95
      mEffects = new DistortedEffects();
96
      mEffects.apply( mRotate );
97
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
98
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
99
      mEffects.apply( new MatrixEffectScale(mScale));
100

  
101
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
102
      }
103

  
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
   
106
    public void onDrawFrame(GL10 glUnused) 
107
      {
108
      mScreen.render( System.currentTimeMillis() );
109
      }
110

  
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
    
113
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
114
      {
115
      final float SCALE = 0.4f;
116
      mScreenMin = Math.min(width, height);
117
      float factor = SCALE*mScreenMin;
118
      mScale.set(factor,factor,factor);
119
      mScreen.resize(width, height);
120
      }
121

  
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
    
124
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
125
      {
126
      if( mTexture==null ) mTexture = new DistortedTexture();
127
      mTexture.setTexture( createTexture() );
128

  
129
      if( mMesh==null ) mMesh = createMesh();
130

  
131
      mScreen.detachAll();
132
      mScreen.attach(mTexture,mEffects,mMesh);
133

  
134
      DistortedLibrary.setMax(EffectType.VERTEX, 11);
135
      VertexEffectRotate.enable();
136

  
137
      try
138
        {
139
        DistortedLibrary.onCreate(mView.getContext());
140
        }
141
      catch(Exception ex)
142
        {
143
        android.util.Log.e("DeferredJob", ex.getMessage() );
144
        }
145
      }
146

  
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

  
149
    void apply(int number)
150
      {
151
      mRotate.setMeshAssociation(0,number);
152
      mAngle.set(360);
153
      mAngleDyn.resetToBeginning();
154
      }
155

  
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

  
158
    private Bitmap createTexture()
159
      {
160
      final int[] FACE_COLORS = new int[] { 0xffff0000, 0xff00ff00 };
161
      final int FACES=FACE_COLORS.length;
162
      int SIZE = 200;
163

  
164
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
165
      Canvas canvas = new Canvas(result);
166
      Paint paint = new Paint();
167
      paint.setStyle(Paint.Style.FILL);
168

  
169
      for(int i=0; i<FACES; i++)
170
        {
171
        paint.setColor(FACE_COLORS[i]);
172
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
173
        }
174

  
175
      return result;
176
      }
177

  
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

  
180
    private MeshBase createMesh()
181
      {
182
      final int MESHES=2;
183

  
184
      MeshBase[] meshes = new MeshRectangles[MESHES];
185

  
186
      meshes[0] = new MeshRectangles(1,1);
187
      meshes[0].setEffectAssociation(0,1,0);
188

  
189
      for(int i=1; i<MESHES; i++)
190
        {
191
        meshes[i] = meshes[0].copy(true);
192
        meshes[i].setEffectAssociation(0,1,i);
193
        }
194

  
195
      Static4D[] lTextureMaps = new Static4D[MESHES];
196
      Static4D[] rTextureMaps = new Static4D[MESHES];
197

  
198
      for(int i=0; i<MESHES; i++)
199
        {
200
        lTextureMaps[i] = new Static4D(0.0f,0.0f,0.5f,1.0f);
201
        rTextureMaps[i] = new Static4D(0.5f,0.0f,0.5f,1.0f);
202
        }
203

  
204
      MeshBase[] tmp = new MeshBase[2];
205

  
206
      tmp[0] = new MeshJoined(meshes);
207
      tmp[0].setTextureMap(lTextureMaps);
208

  
209
      VertexEffectMove   effect0 = new VertexEffectMove  ( new Static3D(0,0,0.5f) );
210
      VertexEffectRotate effect1 = new VertexEffectRotate( new Static1D(180), new Static3D(1,0,0), new Static3D(0,0,0) );
211

  
212
      effect0.setMeshAssociation(1,0);  // apply only to Components whose andAssoc has the least significant bit set, i.e.
213
                                        // to both meshes[0] and meshes[1]
214
      effect1.setMeshAssociation(0,1);  // apply only to Components whose equAssoc is equal to 1, i.e. only to mesh[1]
215

  
216
      tmp[0].apply(effect0);
217
      tmp[0].apply(effect1);
218

  
219
      tmp[1] = tmp[0].copy(true);
220
      tmp[1].setTextureMap(rTextureMaps);
221

  
222
      tmp[0].mergeEffComponents();
223
      tmp[1].mergeEffComponents();
224

  
225
      tmp[0].setEffectAssociation(0,0,0); // set the equAssoc of the 0th (the only) component to 0
226
      tmp[1].setEffectAssociation(0,0,1); // set the equAssoc of the 0th (the only) component to 1
227

  
228
      MeshBase combined = new MeshJoined(tmp);
229

  
230
      VertexEffectMove moveL = new VertexEffectMove  ( new Static3D(-0.6f,0,0) );
231
      VertexEffectMove moveR = new VertexEffectMove  ( new Static3D(+0.6f,0,0) );
232

  
233
      moveL.setMeshAssociation(0,0);  // apply only to tmp[0]
234
      moveR.setMeshAssociation(0,1);  // apply only to tmp[1]
235

  
236
      combined.apply(moveL);
237
      combined.apply(moveR);
238

  
239
      return combined;
240
      }
241
}
src/main/java/org/distorted/examples/singlemesh/SingleMeshSurfaceView.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.singlemesh;
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 SingleMeshSurfaceView extends GLSurfaceView
32
{
33
    private final static int DIRECTION_SENSITIVITY=  12;
34
    private int mX, mY;
35
    private SingleMeshRenderer mRenderer;
36

  
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

  
39
    public SingleMeshSurfaceView(Context context, AttributeSet attrs)
40
      {
41
      super(context,attrs);
42

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

  
46
      if(!isInEditMode())
47
        {
48
        mRenderer = new SingleMeshRenderer(this);
49
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
50
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
51
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
52
        setRenderer(mRenderer);
53
        }
54
      }
55

  
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

  
58
    public SingleMeshRenderer getRenderer()
59
      {
60
      return mRenderer;
61
      }
62

  
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

  
65
    private void resetQuats()
66
      {
67
      float qx = mRenderer.mQuat1.get0();
68
      float qy = mRenderer.mQuat1.get1();
69
      float qz = mRenderer.mQuat1.get2();
70
      float qw = mRenderer.mQuat1.get3();
71

  
72
      float rx = mRenderer.mQuat2.get0();
73
      float ry = mRenderer.mQuat2.get1();
74
      float rz = mRenderer.mQuat2.get2();
75
      float rw = mRenderer.mQuat2.get3();
76

  
77
       // This is quaternion multiplication. (tx,ty,tz,tw)
78
       // is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw)
79
       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
80
       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
81
       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
82
       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
83

  
84
       // The point of this is so that there are always
85
       // exactly 2 quaternions: Quat1 representing the rotation
86
       // accumulating only since the last screen touch, and Quat2
87
       // which remembers the combined effect of all previous
88
       // swipes.
89
       // We cannot be accumulating an ever-growing list of quaternions
90
       // and add a new one every time user swipes the screen - there
91
       // is a limited number of slots in the EffectQueueMatrix!
92
       mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
93
       mRenderer.mQuat2.set(tx, ty, tz, tw);
94
       }
95

  
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

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

  
123
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
124
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
125
                                         
126
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
127
                                           }
128
                                         }
129

  
130
                                       if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mRenderer.mScreenMin*mRenderer.mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
131
                                         {
132
                                         mX = x;
133
                                         mY = y;
134
                                         resetQuats();
135
                                         }
136

  
137
                                       break;
138
                                       
139
         case MotionEvent.ACTION_UP  : mX = -1;
140
                                       mY = -1;
141
                                       resetQuats();
142
                                       break;
143
         }
144
             
145
      return true;
146
      }
147
         
148
}
149

  
src/main/res/layout/singlemeshlayout.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.singlemesh.SingleMeshSurfaceView
8
        android:id="@+id/singlemeshSurfaceView"
9
        android:layout_width="fill_parent"
10
        android:layout_height="0dp"
11
        android:layout_weight="1" />
12

  
13
    <LinearLayout
14
        android:orientation="horizontal"
15
        android:layout_width="match_parent"
16
        android:layout_height="wrap_content">
17

  
18
        <Button
19
            android:layout_width="wrap_content"
20
            android:layout_height="match_parent"
21
            android:id="@+id/singlemeshButton0"
22
            android:text="@string/color_red"
23
            android:onClick="onClick"
24
            android:layout_weight="1"/>
25
        <Button
26
            android:layout_width="wrap_content"
27
            android:layout_height="match_parent"
28
            android:id="@+id/singlemeshButton1"
29
            android:text="@string/color_green"
30
            android:onClick="onClick"
31
            android:layout_weight="1"/>
32

  
33
    </LinearLayout>
34

  
35
</LinearLayout>
src/main/res/values/strings.xml
200 200
    <string name="example_predeform_subtitle">Create a more advanced Mesh by taking one of the simple ones and pre-applying a queue of Vertex Effects to it.</string>
201 201
    <string name="example_deferredjob">Deferred Mesh Jobs</string>
202 202
    <string name="example_deferredjob_subtitle">Create an advanced mesh in steps, using deferred mesh jobs: apply vertex effects, copy meshes, join them, merge their components.</string>
203
    <string name="example_singlemesh">Single Mesh Rubik Cube</string>
204
    <string name="example_singlemesh_subtitle">Use the new MeshJoined + MeshBase.apply() to create a single, movable Mesh representing a 2x2x2 RubikCube.</string>
203 205

  
204 206
    <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>
205 207
    <string name="example_rotate_toast">Rotate the scene by swiping the screen</string>

Also available in: Unified diff