Project

General

Profile

« Previous | Next » 

Revision b62eb334

Added by Leszek Koltunski about 5 years ago

Many things.

1) make the Dynamic.setDuration() able to be called AFTER the Dynamic has already been run. (and rename it to 'makeRunNowFor()' )
2) remove the automatic removal of zero Effects from EffectQueues.
3) adjust several Apps to cope with 2)
4) add post-rotation to Rubik (still not finished)

View differences:

src/main/java/org/distorted/examples/deform/DeformRenderer.java
34 34
import org.distorted.library.mesh.MeshBase;
35 35
import org.distorted.library.mesh.MeshFlat;
36 36

  
37
import org.distorted.library.message.EffectListener;
38
import org.distorted.library.message.EffectMessage;
37 39
import org.distorted.library.type.Dynamic3D;
38 40
import org.distorted.library.type.Static3D;
39 41
import org.distorted.library.type.Static4D;
......
46 48

  
47 49
///////////////////////////////////////////////////////////////////////////////////////////////////
48 50

  
49
class DeformRenderer implements GLSurfaceView.Renderer 
51
class DeformRenderer implements GLSurfaceView.Renderer , EffectListener
50 52
   {
51 53
   private static final int NUM_VECTORS =  8;
52 54
   private static final int NUM_LINES   = 10;
53 55

  
54 56
   private GLSurfaceView mView;
55
   private DistortedTexture stretchTexture;
56
   private DistortedEffects stretchEffects;
57
   private MeshBase stretchMesh;
57
   private DistortedTexture mTexture;
58
   private DistortedEffects mEffects;
59
   private MeshBase mMesh;
58 60
   private DistortedScreen mScreen;
59 61
   private Static3D mTouchPoint;
60 62

  
......
80 82
      { 
81 83
      mView = view;
82 84

  
83
      stretchEffects = new DistortedEffects();
85
      mEffects    = new DistortedEffects();
84 86
      mRegion     = new Static4D(0,0,0,0);
85 87
      mMove       = new Static3D(0,0,0);
86 88
      mTouchPoint = new Static3D(0,0,0);
87 89

  
90
      mEffects.registerForMessages(this);
91

  
88 92
      // DISTORT
89 93
      Dynamic3D releasedDistortDynamic = new Dynamic3D(NUM_VECTORS*500, 0.5f);
90 94
      releasedDistortDynamic.setMode(Dynamic3D.MODE_PATH);
......
143 147
      mReleasedDeform  = new VertexEffectDeform ( releasedDeformDynamic , mTouchPoint, mRegion);
144 148
      mReleasedShear   = new MatrixEffectShear  ( releasedShearDynamic  , mTouchPoint         );
145 149

  
146
      stretchEffects.apply(new MatrixEffectMove(mMove));
150
      mEffects.apply(new MatrixEffectMove(mMove));
147 151
      }
148 152

  
149 153
///////////////////////////////////////////////////////////////////////////////////////////////////
......
161 165
      mRegion.set4(mRadius*scrWidth);
162 166
      }
163 167

  
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
// keep aborting the 'released' effects, otherwise we are quickly going to run out of room in
170
// effect queues.
171

  
172
   public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
173
     {
174
     switch(em)
175
        {
176
        case EFFECT_FINISHED: mEffects.abortById(effectID); break;
177
        }
178
     }
179

  
164 180
///////////////////////////////////////////////////////////////////////////////////////////////////
165 181
   
166 182
   public void onDrawFrame(GL10 glUnused)
......
181 197
     int w=width/2;
182 198
     int h=height/2;
183 199

  
184
     if( stretchMesh!=null ) stretchMesh.markForDeletion();
200
     if( mMesh!=null ) mMesh.markForDeletion();
185 201

  
186
     stretchMesh = new MeshFlat(50,50*h/w);
202
     mMesh = new MeshFlat(50,50*h/w);
187 203
     Bitmap stretchBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
188 204
     stretchCanvas = new Canvas(stretchBitmap);
189 205

  
......
199 215
       stretchCanvas.drawRect(              0, h*i/NUM_LINES-1, w              , h*i/NUM_LINES+1, paint);
200 216
       }
201 217

  
202
     if( stretchTexture==null ) stretchTexture = new DistortedTexture(w,h);
203
     stretchTexture.setTexture(stretchBitmap);
218
     if( mTexture==null ) mTexture = new DistortedTexture(w,h);
219
     mTexture.setTexture(stretchBitmap);
204 220

  
205 221
     mMove.set(scrWidth/4,scrHeight/4,0);
206 222

  
207 223
     mScreen.detachAll();
208
     mScreen.attach(stretchTexture,stretchEffects,stretchMesh);
224
     mScreen.attach(mTexture,mEffects,mMesh);
209 225

  
210 226
     mScreen.resize(width, height);
211 227
     }
......
244 260
     switch(mMode)
245 261
       {
246 262
       case DISTORT: vDistort[0].set(0,0,0);
247
                     stretchEffects.apply(mMovingDistort);
263
                     mEffects.apply(mMovingDistort);
248 264
                     mLastEffect = mMovingDistort.getID();
249 265
                     break;
250 266
       case DEFORM : vDeform[0].set(0,0,0);
251
                     stretchEffects.apply(mMovingDeform);
267
                     mEffects.apply(mMovingDeform);
252 268
                     mLastEffect = mMovingDeform.getID();
253 269
                     break;
254 270
       case SHEAR  : vShear[0].set(0,0,0);
255
                     stretchEffects.apply(mMovingShear);
271
                     mEffects.apply(mMovingShear);
256 272
                     mLastEffect = mMovingShear.getID();
257 273
                     break;
258 274
       }
......
277 293

  
278 294
   void up()
279 295
     {
280
     stretchEffects.abortById(mLastEffect);
296
     mEffects.abortById(mLastEffect);
281 297

  
282 298
     float damp = -0.65f;
283 299

  
......
288 304
                       vDistort[i].set( vDistort[i-1].get1()*damp, vDistort[i-1].get2()*damp, 0 );
289 305
                       }
290 306
                     vDistort[NUM_VECTORS-1].set(0,0,0);
291
                     stretchEffects.apply(mReleasedDistort);
307
                     mEffects.apply(mReleasedDistort);
292 308
                     break;
293 309
       case DEFORM : for(int i=1; i<NUM_VECTORS-1; i++)
294 310
                       {
295 311
                       vDeform[i].set( vDeform[i-1].get1()*damp, vDeform[i-1].get2()*damp, 0 );
296 312
                       }
297 313
                     vDeform[NUM_VECTORS-1].set(0,0,0);
298
                     stretchEffects.apply(mReleasedDeform);
314
                     mEffects.apply(mReleasedDeform);
299 315
                     break;
300 316
       case SHEAR  : for(int i=1; i<NUM_VECTORS-1; i++)
301 317
                       {
302 318
                       vShear[i].set( vShear[i-1].get1()*damp, vShear[i-1].get2()*damp, 0 );
303 319
                       }
304 320
                     vShear[NUM_VECTORS-1].set(0,0,0);
305
                     stretchEffects.apply(mReleasedShear);
321
                     mEffects.apply(mReleasedShear);
306 322
                     break;
307 323
       }
308 324
     }
src/main/java/org/distorted/examples/dynamic/DynamicSurfaceView.java
148 148
      {
149 149
      mDuration = duration;
150 150
      
151
      di1D.setDuration(duration);
152
      di2D.setDuration(duration);
153
      di3D.setDuration(duration);
151
      di1D.makeRunNowFor(duration);
152
      di2D.makeRunNowFor(duration);
153
      di3D.makeRunNowFor(duration);
154 154
      }
155 155

  
156 156
///////////////////////////////////////////////////////////////////
src/main/java/org/distorted/examples/listener/ListenerRenderer.java
102 102
     {
103 103
     switch(em)
104 104
        {
105
        case EFFECT_REMOVED: if( !addNewBubble() )
106
                               {
107
                               android.util.Log.e("Listener", "failed to add new bubble - this should never happen!");
108
                               }
109
                             break;
105
        case EFFECT_FINISHED: mEffects.abortById(effectID);
106

  
107
                              if( !addNewBubble() )
108
                                {
109
                                android.util.Log.e("Listener", "failed to add new bubble - this should never happen!");
110
                                }
111
                              break;
110 112
        }
111 113
     }
112 114
   
src/main/java/org/distorted/examples/movingglow/MovingGlowRenderer.java
139 139

  
140 140
   private void makeGlow(int leaf)
141 141
     {
142
     //android.util.Log.e("glow", "glowing: "+leaf);
143

  
144 142
     mGlowing = leaf;
145 143
     mLeafEffects[leaf].apply(mGlow[leaf]);
146 144
     }
......
152 150
     {
153 151
     switch(em)
154 152
       {
155
       case EFFECT_FINISHED: //android.util.Log.e("glow", "effectMessage FINISHED");
153
       case EFFECT_FINISHED: mLeafEffects[mGlowing].abortById(effectID);
154

  
156 155
                             int glowing = mGlowing+1;
157 156
                             if( glowing>=NUM_LEAVES ) glowing = 0;
158 157
                             makeGlow(glowing);
159 158
                             break;
160
       default:              //android.util.Log.e("glow", "effectMessage REMOVED");
161
                             break;
162 159
       }
163 160
     }
164 161

  
src/main/java/org/distorted/examples/quaternion/QuaternionRenderer.java
85 85
      }
86 86
    
87 87
    rot.setCount(0);
88
    rot.setDuration(8000);
88
    rot.makeRunNowFor(8000);
89 89
    rot.setMode(Dynamic.MODE_LOOP);
90 90

  
91 91
    mMove   = new Static3D(0,0,0);
src/main/java/org/distorted/examples/rubik/RubikCube.java
32 32
import org.distorted.library.main.DistortedScreen;
33 33
import org.distorted.library.main.DistortedTexture;
34 34
import org.distorted.library.mesh.MeshCubes;
35
import org.distorted.library.message.EffectListener;
35 36
import org.distorted.library.type.Dynamic1D;
36 37
import org.distorted.library.type.Static1D;
37 38
import org.distorted.library.type.Static3D;
......
53 54
    private Static3D[][][] mRotationAxis;
54 55
    private Dynamic1D[][][] mRotationAngle;
55 56
    private Static3D[][][] mCurrentPosition;
56
    private Static1D mRotationAngleStatic;
57
    private Static1D mRotationAngleStatic, mRotationAngleNearest;
57 58
    private DistortedTexture mTexture;
59
    private DistortedEffects mEffectsListeningForNow;
58 60

  
59 61
    private int mRotAxis, mRotRow;
60 62
    private int mSize;
......
65 67
      {
66 68
      mSize = size;
67 69

  
68
      mRotationAngleStatic = new Static1D(0);
70
      mRotationAngleStatic  = new Static1D(0);
71
      mRotationAngleNearest = new Static1D(0);
72

  
69 73
      mRotAxis= RubikSurfaceView.VECTX;
70 74
      mTexture = new DistortedTexture(TEXTURE_SIZE,TEXTURE_SIZE);
71 75

  
......
192 196

  
193 197
///////////////////////////////////////////////////////////////////////////////////////////////////
194 198

  
195
    void continueRotation(float angle)
199
    void continueRotation(float angleInDegrees)
200
      {
201
      mRotationAngleStatic.set1(angleInDegrees);
202
      }
203

  
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

  
206
    private int computeNearestAngle(float angle)
196 207
      {
197
      mRotationAngleStatic.set1(angle);
208
      final int NEAREST = 90;
209

  
210
      int tmp = (int)((angle+NEAREST/2)/NEAREST);
211
      if( angle< -(NEAREST/2) ) tmp-=1;
212

  
213
      return NEAREST*tmp;
198 214
      }
199 215

  
200 216
///////////////////////////////////////////////////////////////////////////////////////////////////
201 217

  
202
    void finishRotationCalledOnNextRender()
218
    void finishRotationCalledOnNextRender(EffectListener listener)
203 219
      {
204
      float nearestAngle = (mRotationAngleStatic.get1()+45.0f)/90.0f;
205
      if( nearestAngle<0 ) nearestAngle-=1.0f;
206
      int nearestAngleInDegrees = 90*(4-((int)nearestAngle+4)%4);
220
      boolean first = true;
221
      int nearestAngleInDegrees = computeNearestAngle(mRotationAngleStatic.get1());
222

  
223

  
224
      android.util.Log.e("cube", "finish: angle="+((int)mRotationAngleStatic.get1())+" ret: "+nearestAngleInDegrees);
225

  
226

  
227
      mRotationAngleNearest.set1(nearestAngleInDegrees);
228

  
229
      for(int x=0; x<mSize; x++)
230
        for(int y=0; y<mSize; y++)
231
          for(int z=0; z<mSize; z++)
232
            if( x==0 || x==mSize-1 || y==0 || y==mSize-1 || z==0 || z==mSize-1 )
233
              {
234
              if( belongsToRotation(x,y,z,mRotAxis,mRotRow) )
235
                {
236
                mRotationAngle[x][y][z].makeRunNowFor(2000);
237
                mRotationAngle[x][y][z].add(mRotationAngleNearest);
238

  
239
                if( first )
240
                  {
241
                  first = false;
242
                  mEffectsListeningForNow = mEffects[x][y][z];
243
                  mEffectsListeningForNow.registerForMessages(listener);
244
                  }
245
                }
246
              }
247
      }
248

  
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250

  
251
    void removeRotationCalledOnNextRender(EffectListener listener)
252
      {
253
      mEffectsListeningForNow.deregisterForMessages(listener);
254

  
255
      int nearestAngleInDegrees = computeNearestAngle(mRotationAngleStatic.get1());
207 256
      double nearestAngleInRadians = nearestAngleInDegrees*Math.PI/180;
208
      float sinA = (float)Math.sin(nearestAngleInRadians*0.5);
257
      float sinA =-(float)Math.sin(nearestAngleInRadians*0.5);
209 258
      float cosA = (float)Math.cos(nearestAngleInRadians*0.5);
210 259

  
260
      android.util.Log.e("cube", "remove: angle="+((int)mRotationAngleStatic.get1())+" ret: "+nearestAngleInDegrees);
261

  
211 262
      mRotationAngleStatic.set1(0);
263
      mRotationAngleNearest.set1(0);
212 264

  
213 265
      float qx=0,qy=0,qz=0;
214 266

  
......
228 280
              {
229 281
              if( belongsToRotation(x,y,z,mRotAxis,mRotRow) )
230 282
                {
283
                mRotationAngle[x][y][z].makeRunNowFor(0);
231 284
                mRotationAngle[x][y][z].removeAll();
232 285
                mQuatScramble[x][y][z].set(RubikSurfaceView.quatMultiply(quat,mQuatScramble[x][y][z]));
233 286
                modifyCurrentPosition(x,y,z,quat);
src/main/java/org/distorted/examples/rubik/RubikRenderer.java
24 24
import org.distorted.library.effect.VertexEffectSink;
25 25
import org.distorted.library.main.Distorted;
26 26
import org.distorted.library.main.DistortedScreen;
27
import org.distorted.library.message.EffectListener;
28
import org.distorted.library.message.EffectMessage;
27 29
import org.distorted.library.type.Static3D;
28 30
import org.distorted.library.type.Static4D;
29 31

  
......
32 34

  
33 35
///////////////////////////////////////////////////////////////////////////////////////////////////
34 36

  
35
class RubikRenderer implements GLSurfaceView.Renderer
37
class RubikRenderer implements GLSurfaceView.Renderer, EffectListener
36 38
{
37 39
    private static final int NUM_CUBES = 4;
38 40
    private static final float CUBE_SCREEN_RATIO = 0.5f;
39
    private static final float CAMERA_DISTANCE = 0.5f;     // 0.5 of the length of max(scrHeight,scrWidth)
41
    private static final float CAMERA_DISTANCE   = 0.5f;  // 0.5 of the length of max(scrHeight,scrWidth)
40 42

  
41 43
    private RubikSurfaceView mView;
42 44
    private DistortedScreen mScreen;
......
44 46
    private Static4D mQuatCurrent, mQuatAccumulated;
45 47
    private Static4D mTempCurrent, mTempAccumulated;
46 48
    private float mCubeSizeInScreenSpace;
47
    private boolean mFinishRotation, mFinishDragCurrent, mFinishDragAccumulated;
49
    private boolean mFinishRotation, mRemoveRotation, mFinishDragCurrent, mFinishDragAccumulated;
48 50
    private RubikCube mCube;
49 51

  
50 52
///////////////////////////////////////////////////////////////////////////////////////////////////
......
64 66
      mScale = new Static3D(1,1,1);
65 67

  
66 68
      mFinishRotation        = false;
69
      mRemoveRotation        = false;
67 70
      mFinishDragCurrent     = false;
68 71
      mFinishDragAccumulated = false;
69 72

  
......
71 74
      }
72 75

  
73 76
///////////////////////////////////////////////////////////////////////////////////////////////////
74
   
77
// various things are done here delayed, 'after the next render' as not to be done mid-render and
78
// cause artifacts.
79

  
75 80
    public void onDrawFrame(GL10 glUnused) 
76 81
      {
77 82
      mScreen.render( System.currentTimeMillis() );
......
91 96
      if( mFinishRotation )
92 97
        {
93 98
        mFinishRotation=false;
94
        mCube.finishRotationCalledOnNextRender();
99
        mCube.finishRotationCalledOnNextRender(this);
100
        }
101

  
102
      if( mRemoveRotation )
103
        {
104
        mRemoveRotation=false;
105
        mCube.removeRotationCalledOnNextRender(this);
95 106
        }
96 107
      }
97 108

  
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
// EffectListener. The library sends a message to us when it's time to call 'removeRotation'
111

  
112
   public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
113
     {
114
     switch(em)
115
        {
116
        case EFFECT_FINISHED: mRemoveRotation = true; break;
117
        }
118
     }
119

  
98 120
///////////////////////////////////////////////////////////////////////////////////////////////////
99 121
    
100 122
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
......
141 163
    private float computeFOV(float cameraDistance, int screenHeight)
142 164
      {
143 165
      double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
144
      float fovInDegrees = (float)(2*halfFOVInRadians*(180/Math.PI));
145

  
146
      return fovInDegrees;
166
      return (float)(2*halfFOVInRadians*(180/Math.PI));
147 167
      }
148 168

  
149 169
///////////////////////////////////////////////////////////////////////////////////////////////////
src/main/java/org/distorted/examples/rubik/RubikSurfaceView.java
63 63
      {
64 64
      super(context);
65 65

  
66
      mDragging = false;
67
      mBeginRot = false;
68 66
      mRotationVect = VECT[0];
69 67

  
70 68
      mPoint = new float[3];
......
109 107
                                       mY = y;
110 108
                                       mLastTouchedFace = faceTouched(x,y);
111 109

  
112
                                       if( mLastTouchedFace != NONE ) mBeginRot = true;
113
                                       else                           mDragging = true;
114

  
110
                                       if( mLastTouchedFace != NONE ) { mBeginRot = true; mDragging = false; }
111
                                       else                           { mDragging = true; mBeginRot = false; }
115 112
                                       break;
116 113
         case MotionEvent.ACTION_MOVE: if( mDragging )
117 114
                                         {
......
120 117
                                         }
121 118
                                       else if( mBeginRot )
122 119
                                         {
123
                                         int minimumToRotate = (mScreenMin*mScreenMin)/100;
120
                                         int minimumDistToStartRotating = (mScreenMin*mScreenMin)/100;
124 121

  
125
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y)>minimumToRotate )
122
                                         if( (mX-x)*(mX-x)+(mY-y)*(mY-y) > minimumDistToStartRotating )
126 123
                                           {
127 124
                                           addNewRotation(x,y);
128 125
                                           mBeginRot = false;
......
134 131
                                         }
135 132
                                       break;
136 133
         case MotionEvent.ACTION_UP  : if( !mDragging ) finishRotation();
137

  
138
                                       mDragging = false;
139
                                       mBeginRot = false;
140

  
141 134
                                       mQuatAccumulated.set(quatMultiply(mQuatCurrent, mQuatAccumulated));
142 135
                                       mQuatCurrent.set(0f, 0f, 0f, 1f);
143 136
                                       mRenderer.setQuatCurrent(mQuatCurrent);
......
366 359
      }
367 360

  
368 361
///////////////////////////////////////////////////////////////////////////////////////////////////
369
// given precomputed mCam and mPoi, respectively camera and touch point positions in ScreenSpace,
370
// cast this touch point onto the 'face' and write the cast coords to 'output'.
371
// Center of the 'face' = (0,0)
362
// given precomputed mCamera and mPoint, respectively camera and touch point positions in ScreenSpace,
363
// cast this touch point onto the surface defined by the 'face' and write the cast coords to 'output'.
364
// Center of the 'face' = (0,0), third coord always +- cubeHalfSize.
372 365

  
373 366
    private void castTouchPointOntoFace(int face, float cubeHalfSize, float[] output)
374 367
      {
......
398 391
      }
399 392

  
400 393
///////////////////////////////////////////////////////////////////////////////////////////////////
394
// retFace{X,Y,Z}axis: 3 functions which return which real AXIS gets mapped to which when we look
395
// directly at a given face. For example, when we look at the RIGHT face of the cube (with TOP still
396
// in the top) then the 'real' X axis becomes the 'Z' axis, thus retFaceZaxis(RIGHT) = VECTX.
401 397

  
402 398
    private int retFaceXaxis(int face)
403 399
      {
src/main/java/org/distorted/examples/wind/WindEffectsManager.java
131 131
    shearFactor.set2(tanAngle);
132 132
    scaleFactor.set1(1/(float)Math.sqrt(1+tanAngle*tanAngle));
133 133

  
134
    windDynamic1.setDuration( wind > 0 ? 900 + 10000/wind : Long.MAX_VALUE);
135
    windDynamic2.setDuration( wind > 0 ? 720 +  8000/wind : Long.MAX_VALUE);
136
    windDynamic3.setDuration( wind > 0 ? 900 + 10000/wind : Long.MAX_VALUE);
134
    windDynamic1.makeRunNowFor( wind > 0 ? 900 + 10000/wind : Long.MAX_VALUE);
135
    windDynamic2.makeRunNowFor( wind > 0 ? 720 +  8000/wind : Long.MAX_VALUE);
136
    windDynamic3.makeRunNowFor( wind > 0 ? 900 + 10000/wind : Long.MAX_VALUE);
137 137

  
138 138
    float wave2 = mHeight*( 0.05f + 0.002f*wind);
139 139
    windFactor21.set1(wave2);

Also available in: Unified diff