Project

General

Profile

« Previous | Next » 

Revision 56cbe1cf

Added by Leszek Koltunski almost 8 years ago

Re-write Vertex3D to provide similar functionality to Fragment3D (displaying the center and region bitmaps still missing)

View differences:

src/main/java/org/distorted/examples/vertex3d/Vertex3DRenderer.java
27 27
import org.distorted.examples.R;
28 28
import org.distorted.library.Distorted;
29 29
import org.distorted.library.DistortedBitmap;
30
import org.distorted.library.DistortedCubes;
31 30
import org.distorted.library.DistortedObject;
32
import org.distorted.library.EffectNames;
33 31
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Dynamic2D;
36
import org.distorted.library.type.Dynamic3D;
37 32
import org.distorted.library.type.DynamicQuat;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static2D;
40 33
import org.distorted.library.type.Static3D;
41 34
import org.distorted.library.type.Static4D;
42 35

  
......
50 43

  
51 44
class Vertex3DRenderer implements GLSurfaceView.Renderer
52 45
{
53
    private static final int SIZE = 100;
54

  
55 46
    private GLSurfaceView mView;
56 47
    private DistortedObject mObject;
57
    private DistortedBitmap mCenter;
58
    private float mFactorCen, mFactorObj;
59

  
48
    private DistortedBitmap mBackground;
60 49
    private int mObjWidth, mObjHeight;
61

  
62 50
    private DynamicQuat mQuatInt1, mQuatInt2;
63

  
64
    private EffectNames[] order;
65

  
66
    private Dynamic2D mCenterInter;
67
    private Dynamic3D mDeformInter, mDistortInter, mMoveInter;
68
    private Dynamic1D mSinkInter, mSwirlInter;
69

  
70
    private Static2D mCenterPoint;
71
    private Static3D mDeformPoint, mDistortPoint, mMovePoint;
72
    private Static1D mSinkPoint, mSwirlPoint;
73

  
74 51
    Static4D mQuat1, mQuat2;
75 52
    int mScreenMin;
76 53

  
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

  
79
    public void setDeform(float x, float y, float z)
80
      {
81
      mDeformPoint.set(x, y, z);
82
      }
83
    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

  
86
    public void setDistort(float x, float y, float z)
87
      {
88
      mDistortPoint.set(x, y, z);
89
      }
90
     
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

  
93
    public void setSink(float s)
94
      {
95
      mSinkPoint.set(s);
96
      }
97
    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

  
100
    public void setSwirl(float s)
101
      {
102
      mSwirlPoint.set(s);
103
      }
104

  
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

  
107
    public void setCenter(float x, float y)
108
      {
109
      x = (0.012f*x-0.1f)*mObjWidth;
110
      y = (0.012f*y-0.1f)*mObjHeight;
111

  
112
      mCenterPoint.set(x,y);
113
      mMovePoint.set(mFactorObj*x,mFactorObj*y,0);
114
      }
115

  
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

  
118
    public void setOrder(EffectNames[] effects)
119
      {
120
      order = effects;
121
      setVertexEffects();
122
      }
123
      
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

  
126
    public void setVertexEffects()
127
      {
128
      mObject.abortEffects(EffectTypes.VERTEX);
129

  
130
      for( int i=0; i<=order.length-1 ; i++ )
131
        {
132
        switch(order[i])
133
          {
134
          case DEFORM : mObject.deform( mDeformInter , mCenterInter) ; break;
135
          case DISTORT: mObject.distort(mDistortInter, mCenterInter) ; break;
136
          case SINK   : mObject.sink(   mSinkInter   , mCenterInter) ; break;
137
          case SWIRL  : mObject.swirl(  mSwirlInter  , mCenterInter) ; break;
138
          }
139
        }
140
      }
141
    
142 54
///////////////////////////////////////////////////////////////////////////////////////////////////
143 55

  
144 56
    public Vertex3DRenderer(GLSurfaceView v)
145 57
      {
146 58
      mView = v;
147 59

  
148
      mObject = ((Vertex3DActivity)v.getContext()).getObject();
149
      mCenter = new DistortedBitmap(SIZE, SIZE, 1);
60
      mObject     = ((Vertex3DActivity)v.getContext()).getObject();
61
      mBackground = new DistortedBitmap(100, 100, 1);
150 62

  
151 63
      mObjWidth = mObject.getWidth();
152 64
      mObjHeight= mObject.getHeight();
153 65

  
154
      mCenterPoint = new Static2D(0,0);
155
      mMovePoint   = new Static3D(0,0,0);
156
      mDeformPoint = new Static3D(0,0,0);
157
      mDistortPoint= new Static3D(1,1,1);
158
      mSwirlPoint  = new Static1D(0);
159
      mSinkPoint   = new Static1D(1);
160

  
161
      mCenterInter  = new Dynamic2D();
162
      mDeformInter  = new Dynamic3D();
163
      mDistortInter = new Dynamic3D();
164
      mMoveInter    = new Dynamic3D();
165
      mSwirlInter   = new Dynamic1D();
166
      mSinkInter    = new Dynamic1D();
167

  
168
      mCenterInter.add(mCenterPoint);
169
      mMoveInter.add(mMovePoint);
170
      mDeformInter.add(mDeformPoint);
171
      mDistortInter.add(mDistortPoint);
172
      mSwirlInter.add(mSwirlPoint);
173
      mSinkInter.add(mSinkPoint);
174

  
175 66
      mQuat1 = new Static4D(0,0,0,1);  // unity
176 67
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
177 68

  
......
183 74
      }
184 75

  
185 76
///////////////////////////////////////////////////////////////////////////////////////////////////
186
   
187
    public void onDrawFrame(GL10 glUnused) 
77

  
78
    public void onDrawFrame(GL10 glUnused)
188 79
      {
189 80
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
190 81
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
191 82

  
192 83
      long time = System.currentTimeMillis();
193 84

  
85
      mBackground.draw(time);
194 86
      mObject.draw(time);
195
      mCenter.draw(time);
196 87
      }
197 88

  
198 89
///////////////////////////////////////////////////////////////////////////////////////////////////
199
    
200
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
90

  
91
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
201 92
      {
202 93
      mScreenMin = width<height ? width:height;
203 94

  
204 95
      mObject.abortEffects(EffectTypes.MATRIX);
205
      mCenter.abortEffects(EffectTypes.MATRIX);
206

  
207
      int centerSize = mCenter.getWidth();
96
      mBackground.abortEffects(EffectTypes.MATRIX);
97
      float factorObj;
208 98

  
209 99
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
210 100
        {
211
        mFactorObj = (0.70f*height)/mObjHeight;
212
        mFactorCen = (0.15f*height)/centerSize;
101
        factorObj = (0.80f*height)/mObjHeight;
213 102
        }
214 103
      else
215 104
        {
216
        mFactorObj = (0.70f*width)/mObjWidth;
217
        mFactorCen = (0.15f*width)/centerSize;
105
        factorObj = (0.80f*width)/mObjWidth;
218 106
        }
219 107

  
220
      Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
221
      float cX = act.getCenterX();
222
      float cY = act.getCenterY();
223

  
224
      cX = (0.012f*cX-0.1f)*mObjWidth;
225
      cY = (0.012f*cY-0.1f)*mObjHeight;
226

  
227
      mMovePoint.set(cX*mFactorObj,cY*mFactorObj,0);
228

  
229 108
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
230 109

  
231
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
232
      mObject.scale(mFactorObj);
110
      mObject.move( new Static3D( (width-factorObj*mObjWidth)/2 , (height-factorObj*mObjHeight)/2 , 0) );
111
      mObject.scale(factorObj);
233 112
      mObject.quaternion(mQuatInt1, rotateObj);
234 113
      mObject.quaternion(mQuatInt2, rotateObj);
235 114

  
236
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
115
      int backgroundSize = mBackground.getWidth();
116
      float factorBackX = ((float)width)/backgroundSize;
117
      float factorBackY = ((float)height)/backgroundSize;
237 118

  
238
      mCenter.quaternion(mQuatInt1, rotateCen);
239
      mCenter.quaternion(mQuatInt2, rotateCen);
240
      mCenter.move( new Static3D( (width -mFactorCen*centerSize-mFactorObj*mObjWidth )/2 ,
241
                                  (height-mFactorCen*centerSize-mFactorObj*mObjHeight)/2 , 10) );
242
      mCenter.move(mMoveInter);
243
      mCenter.scale(mFactorCen);
119
      mBackground.move(new Static3D( -width/2, -height/2,-factorObj*(mObjWidth+mObjHeight)/2) );
120
      mBackground.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
244 121

  
245
      setVertexEffects();
246

  
247
      Distorted.onSurfaceChanged(width, height); 
122
      Distorted.onSurfaceChanged(width, height);
248 123
      }
249 124

  
250 125
///////////////////////////////////////////////////////////////////////////////////////////////////
251
    
252
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
126

  
127
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
253 128
      {
254 129
      Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
255 130

  
256 131
      InputStream is1 = act.getResources().openRawResource(act.getBitmap());
257
      InputStream is2 = act.getResources().openRawResource(R.raw.center);
132
      InputStream is2 = act.getResources().openRawResource(R.raw.water);
258 133

  
259 134
      Bitmap bitmap1,bitmap2;
260 135
        
......
274 149
        }  
275 150
      
276 151
      mObject.setBitmap(bitmap1);
277
      mCenter.setBitmap(bitmap2);
278

  
152
      mBackground.setBitmap(bitmap2)
153
      ;
279 154
      try
280 155
        {
281 156
        Distorted.onSurfaceCreated(mView.getContext());
......
285 160
        android.util.Log.e("Vertex3D", ex.getMessage() );
286 161
        }
287 162
      }
288
}
163
}

Also available in: Unified diff