Project

General

Profile

« Previous | Next » 

Revision da56b12f

Added by Leszek Koltunski over 2 years ago

Beginnings of support for display of a collection of bandaged cubits in the CreatorView.

View differences:

src/main/java/org/distorted/bandaged/BandagedCreatorActivity.java
1 1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
2
// Copyright 2022 Leszek Koltunski                                                               //
3 3
//                                                                                               //
4 4
// This file is part of Magic Cube.                                                              //
5 5
//                                                                                               //
src/main/java/org/distorted/bandaged/BandagedCreatorRenderer.java
1 1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
2
// Copyright 2022 Leszek Koltunski                                                               //
3 3
//                                                                                               //
4 4
// This file is part of Magic Cube.                                                              //
5 5
//                                                                                               //
......
21 21

  
22 22
import android.opengl.GLSurfaceView;
23 23

  
24
import org.distorted.library.effect.EffectType;
25
import org.distorted.library.effect.VertexEffectQuaternion;
26
import org.distorted.library.effect.VertexEffectRotate;
27 24
import org.distorted.library.main.DistortedLibrary;
25
import org.distorted.library.main.DistortedNode;
28 26
import org.distorted.library.main.DistortedScreen;
27
import org.distorted.library.type.Static4D;
29 28

  
30 29
import javax.microedition.khronos.egl.EGLConfig;
31 30
import javax.microedition.khronos.opengles.GL10;
......
37 36
   private final BandagedCreatorView mView;
38 37
   private final DistortedScreen mScreen;
39 38

  
39
   private final BandagedCubit[] mCubits;
40

  
41
   private float[][] POSITIONS = new float[][]
42
        {
43
          {-1.0f, +1.0f, +1.0f},
44
          {-1.0f, +1.0f, +0.0f},
45
          {-1.0f, +1.0f, -1.0f},
46
          {-1.0f,  0.0f, +1.0f},
47
          {-1.0f,  0.0f, +0.0f},
48
          {-1.0f,  0.0f, -1.0f},
49
          {-1.0f, -1.0f, +1.0f},
50
          {-1.0f, -1.0f, +0.0f},
51
          {-1.0f, -1.0f, -1.0f},
52
          { 0.0f, -1.0f, +1.0f},
53
          { 0.0f, -1.0f, +0.0f},
54
          { 0.0f, +1.0f, +1.0f},
55
          { 0.0f, +1.0f, +0.0f},
56
          { 0.0f, +1.0f, -1.0f},
57
          { 0.0f,  0.0f, +1.0f},
58
          { 0.0f,  0.0f, -1.0f},
59
          { 1.0f, +1.0f, +1.0f},
60
          { 1.0f, +1.0f, +0.0f},
61
          { 1.0f, +1.0f, -1.0f},
62
          { 1.0f,  0.0f, +1.0f},
63
          { 1.0f,  0.0f, +0.0f},
64
          { 1.0f, -1.0f, +1.0f},
65
          { 1.0f,  0.0f, -1.0f },
66
          { 1.0f, -1.0f, -1.0f },
67
          { 1.0f, -1.0f, +0.0f },
68
          { 0.0f, -1.0f, -1.0f },
69
        };
70

  
71
   Static4D mQuat1, mQuat2;
72
   int mScreenMin;
73

  
40 74
///////////////////////////////////////////////////////////////////////////////////////////////////
41 75

  
42 76
   BandagedCreatorRenderer(BandagedCreatorView v)
43 77
     {
44 78
     final float BRIGHTNESS = 0.333f;
45 79

  
80
     mQuat1 = new Static4D(0,0,0,1);
81
     mQuat2 = new Static4D(0,0,0,1);
82

  
46 83
     mView = v;
47 84
     mScreen = new DistortedScreen();
48 85
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
86

  
87
     mCubits = createCubits();
88
     }
89

  
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

  
92
   private BandagedCubit[] createCubits()
93
     {
94
     int len = POSITIONS.length;
95
     BandagedCubit[] cubits = new BandagedCubit[len];
96

  
97
     for(int c=0; c<len; c++)
98
       {
99
       cubits[c] = new BandagedCubit(POSITIONS[c],mQuat1,mQuat2,1.0f);
100
       DistortedNode node = cubits[c].getNode();
101
       mScreen.attach(node);
102
       }
103

  
104
     return cubits;
49 105
     }
50 106

  
51 107
///////////////////////////////////////////////////////////////////////////////////////////////////
......
62 118
   @Override
63 119
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
64 120
      {
121
      mScreenMin = Math.min(width, height);
65 122
      mScreen.resize(width,height);
66 123
      }
67 124

  
......
70 127
   @Override
71 128
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
72 129
      {
73
      DistortedLibrary.setMax(EffectType.VERTEX,25);
74
      VertexEffectRotate.enable();
75
      VertexEffectQuaternion.enable();
76

  
77 130
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
78 131
      }
79 132

  
......
83 136
     {
84 137
     android.util.Log.e("CREATOR", "unexpected exception: "+ex.getMessage() );
85 138
     }
86

  
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

  
89
   DistortedScreen getScreen()
90
     {
91
     return mScreen;
92
     }
93 139
}
src/main/java/org/distorted/bandaged/BandagedCreatorScreen.java
1 1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
2
// Copyright 2022 Leszek Koltunski                                                               //
3 3
//                                                                                               //
4 4
// This file is part of Magic Cube.                                                              //
5 5
//                                                                                               //
src/main/java/org/distorted/bandaged/BandagedCreatorView.java
1 1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
2
// Copyright 2022 Leszek Koltunski                                                               //
3 3
//                                                                                               //
4 4
// This file is part of Magic Cube.                                                              //
5 5
//                                                                                               //
......
25 25
import android.opengl.GLES30;
26 26
import android.opengl.GLSurfaceView;
27 27
import android.util.AttributeSet;
28
import android.view.MotionEvent;
28 29

  
29 30
import com.google.firebase.crashlytics.FirebaseCrashlytics;
30 31

  
......
32 33

  
33 34
public class BandagedCreatorView extends GLSurfaceView
34 35
{
36
    private final static int DIRECTION_SENSITIVITY=  12;
37
    private int mX, mY;
35 38
    private BandagedCreatorRenderer mRenderer;
36 39

  
37 40
///////////////////////////////////////////////////////////////////////////////////////////////////
......
42 45
      {
43 46
      super(context,attrs);
44 47

  
48
      mX = -1;
49
      mY = -1;
50

  
45 51
      if(!isInEditMode())
46 52
        {
47 53
        BandagedCreatorActivity act = (BandagedCreatorActivity)context;
......
77 83

  
78 84
///////////////////////////////////////////////////////////////////////////////////////////////////
79 85

  
80
    @Override
81
    public void onPause()
86
    public BandagedCreatorRenderer getRenderer()
82 87
      {
83
      super.onPause();
88
      return mRenderer;
84 89
      }
85 90

  
86 91
///////////////////////////////////////////////////////////////////////////////////////////////////
87 92

  
88
    @Override
89
    public void onResume()
93
    private void resetQuats()
90 94
      {
91
      super.onResume();
95
      float qx = mRenderer.mQuat1.get0();
96
      float qy = mRenderer.mQuat1.get1();
97
      float qz = mRenderer.mQuat1.get2();
98
      float qw = mRenderer.mQuat1.get3();
99

  
100
      float rx = mRenderer.mQuat2.get0();
101
      float ry = mRenderer.mQuat2.get1();
102
      float rz = mRenderer.mQuat2.get2();
103
      float rw = mRenderer.mQuat2.get3();
104

  
105
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
106
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
107
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
108
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
109

  
110
      mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
111
      mRenderer.mQuat2.set(tx, ty, tz, tw);
92 112
      }
113

  
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

  
116
    @Override public boolean onTouchEvent(MotionEvent event)
117
      {
118
      int action = event.getAction();
119
      int x = (int)event.getX();
120
      int y = (int)event.getY();
121

  
122
      switch(action)
123
         {
124
         case MotionEvent.ACTION_DOWN: mX = x;
125
                                       mY = y;
126
                                       break;
127

  
128
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
129
                                         {
130
                                         float px = mY-y;
131
                                         float py = mX-x;
132
                                         float pz = 0;
133
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
134

  
135
                                         if( plen>0 )
136
                                           {
137
                                           px /= plen;
138
                                           py /= plen;
139
                                           pz /= plen;
140

  
141
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
142
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
143

  
144
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
145
                                           }
146
                                         }
147
                                       if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mRenderer.mScreenMin*mRenderer.mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
148
                                         {
149
                                         mX = x;
150
                                         mY = y;
151
                                         resetQuats();
152
                                         }
153
                                       break;
154

  
155
         case MotionEvent.ACTION_UP  : mX = -1;
156
                                       mY = -1;
157
        	                             resetQuats();
158
                                       break;
159
         }
160

  
161
      return true;
162
      }
163

  
93 164
}
94 165

  
src/main/java/org/distorted/bandaged/BandagedCubit.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2022 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube 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
// Magic Cube 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 Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

  
20
package org.distorted.bandaged;
21

  
22
import org.distorted.library.effect.MatrixEffectMove;
23
import org.distorted.library.effect.MatrixEffectQuaternion;
24
import org.distorted.library.effect.MatrixEffectScale;
25
import org.distorted.library.main.DistortedEffects;
26
import org.distorted.library.main.DistortedNode;
27
import org.distorted.library.main.DistortedTexture;
28
import org.distorted.library.mesh.MeshBase;
29
import org.distorted.library.type.Static3D;
30
import org.distorted.library.type.Static4D;
31
import org.distorted.objectlib.helpers.FactoryBandaged3x3Cubit;
32

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

  
35
public class BandagedCubit
36
{
37
    private static final Static3D CENTER = new Static3D(0,0,0);
38

  
39
    private final DistortedNode mNode;
40
    private final DistortedTexture mTexture;
41
    private final DistortedEffects mEffects;
42
    private final MeshBase mMesh;
43
    private final float[] mPosition;
44

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

  
47
    private Static3D computeMove(float[] position)
48
      {
49
      int numCenters = position.length/3;
50
      float totalX=0.0f, totalY=0.0f, totalZ=0.0f;
51

  
52
      for(int center=0; center<numCenters; center++)
53
        {
54
        totalX += position[3*center  ];
55
        totalY += position[3*center+1];
56
        totalZ += position[3*center+2];
57
        }
58

  
59
      totalX /= numCenters;
60
      totalY /= numCenters;
61
      totalZ /= numCenters;
62

  
63
      return new Static3D(totalX,totalY,totalZ);
64
      }
65

  
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
// PUBLIC API
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

  
70
    public BandagedCubit(float[] position, Static4D quat1, Static4D quat2, float scale)
71
      {
72
      mPosition = position;
73

  
74
      FactoryBandaged3x3Cubit factory = FactoryBandaged3x3Cubit.getInstance();
75
      mMesh = factory.createMesh(position);
76

  
77
      mTexture = new DistortedTexture();
78
      mTexture.setColorARGB(0xffffffff);
79

  
80
      Static3D move = computeMove(position);
81
      Static3D objectScale = new Static3D(scale,scale,scale);
82

  
83
      MatrixEffectScale scaleEffect = new MatrixEffectScale(objectScale);
84
      MatrixEffectQuaternion quat1Effect = new MatrixEffectQuaternion(quat1, CENTER);
85
      MatrixEffectQuaternion quat2Effect = new MatrixEffectQuaternion(quat2, CENTER);
86
      MatrixEffectMove moveEffect = new MatrixEffectMove(move);
87

  
88
      mEffects = new DistortedEffects();
89
      mEffects.apply(quat1Effect);
90
      mEffects.apply(quat2Effect);
91
      mEffects.apply(scaleEffect);
92
      mEffects.apply(moveEffect);
93

  
94
      mNode = new DistortedNode(mTexture,mEffects,mMesh);
95
      }
96

  
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

  
99
    public DistortedNode getNode()
100
      {
101
      return mNode;
102
      }
103
}
104

  
src/main/java/org/distorted/bandaged/BandagedPlayActivity.java
1 1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
2
// Copyright 2022 Leszek Koltunski                                                               //
3 3
//                                                                                               //
4 4
// This file is part of Magic Cube.                                                              //
5 5
//                                                                                               //
src/main/java/org/distorted/bandaged/BandagedPlayLibInterface.java
1 1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 Leszek Koltunski                                                               //
2
// Copyright 2022 Leszek Koltunski                                                               //
3 3
//                                                                                               //
4 4
// This file is part of Magic Cube.                                                              //
5 5
//                                                                                               //
src/main/java/org/distorted/bandaged/BandagedPlayRenderer.java
1 1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
2
// Copyright 2022 Leszek Koltunski                                                               //
3 3
//                                                                                               //
4 4
// This file is part of Magic Cube.                                                              //
5 5
//                                                                                               //
src/main/java/org/distorted/bandaged/BandagedPlayScreen.java
1 1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
2
// Copyright 2022 Leszek Koltunski                                                               //
3 3
//                                                                                               //
4 4
// This file is part of Magic Cube.                                                              //
5 5
//                                                                                               //
src/main/java/org/distorted/bandaged/BandagedPlayView.java
1 1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
2
// Copyright 2022 Leszek Koltunski                                                               //
3 3
//                                                                                               //
4 4
// This file is part of Magic Cube.                                                              //
5 5
//                                                                                               //

Also available in: Unified diff