Project

General

Profile

« Previous | Next » 

Revision 296d2e73

Added by Leszek Koltunski almost 6 years ago

Advances with the Earth app - prepare it to checking if any FRAGMENT or VERTEX effect correctly works on a Sphere mesh.

View differences:

src/main/java/org/distorted/examples/aroundtheworld/AroundTheWorldRendererPicker.java
103 103

  
104 104
///////////////////////////////////////////////////////////////////////////////////////////////////
105 105

  
106
	@Override public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
106
	@Override
107
	public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
107 108
	  {
108 109
		GLES31.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
109 110

  
......
220 221

  
221 222
///////////////////////////////////////////////////////////////////////////////////////////////////
222 223

  
223
	@Override public void onSurfaceChanged(GL10 glUnused, int width, int height)
224
	@Override
225
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
224 226
	  {
225 227
	  mHeight = height;
226 228
	  mWidth  = width;
......
241 243

  
242 244
///////////////////////////////////////////////////////////////////////////////////////////////////
243 245

  
244
	@Override public void onDrawFrame(GL10 glUnused)
246
	@Override
247
  public void onDrawFrame(GL10 glUnused)
245 248
	  {
246 249
		GLES31.glClear(GLES31.GL_DEPTH_BUFFER_BIT | GLES31.GL_COLOR_BUFFER_BIT);
247 250
    GLES31.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPTriangleMatrix, 0);
src/main/java/org/distorted/examples/earth/EarthActivity.java
22 22
import android.app.Activity;
23 23
import android.opengl.GLSurfaceView;
24 24
import android.os.Bundle;
25
import android.view.View;
26
import android.widget.AdapterView;
27
import android.widget.ArrayAdapter;
25 28
import android.widget.SeekBar;
29
import android.widget.Spinner;
26 30

  
27 31
import org.distorted.examples.R;
32
import org.distorted.library.effect.EffectName;
33
import org.distorted.library.effect.EffectType;
28 34
import org.distorted.library.main.Distorted;
29
import org.distorted.library.main.DistortedTexture;
30
import org.distorted.library.mesh.MeshBase;
31
import org.distorted.library.mesh.MeshSphere;
32 35

  
33 36
///////////////////////////////////////////////////////////////////////////////////////////////////
34 37

  
35
public class EarthActivity extends Activity implements SeekBar.OnSeekBarChangeListener
38
public class EarthActivity extends Activity implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
36 39
{
37
    private static final int SIZE = 100;
38
    private static final int LEVEL=  32;
39

  
40
    private DistortedTexture mTexture;
41
    private MeshBase mMesh;
40
    private EffectName[] mEffectNames;
42 41

  
43 42
///////////////////////////////////////////////////////////////////////////////////////////////////
44
    
43

  
45 44
    @Override
46 45
    protected void onPause() 
47 46
      {
48
      GLSurfaceView mView = this.findViewById(R.id.earthSurfaceView);
49
      if( mView!=null ) mView.onPause();
47
      GLSurfaceView view = this.findViewById(R.id.earthSurfaceView);
48
      if( view!=null ) view.onPause();
49

  
50
      EarthSurfaceViewPicker pick = this.findViewById(R.id.earthSurfaceViewPicker);
51
      if( pick!=null ) pick.onPause();
50 52

  
51 53
      Distorted.onPause();
52 54
      super.onPause();
......
59 61
      {
60 62
      super.onResume();
61 63
      
62
      GLSurfaceView mView = this.findViewById(R.id.earthSurfaceView);
63
      if( mView!=null ) mView.onResume();  
64
      GLSurfaceView view = this.findViewById(R.id.earthSurfaceView);
65
      if( view!=null ) view.onResume();
66

  
67
      EarthSurfaceViewPicker pick = this.findViewById(R.id.earthSurfaceViewPicker);
68
      if( pick!=null ) pick.onResume();
64 69
      }
65 70
    
66 71
///////////////////////////////////////////////////////////////////////////////////////////////////
......
79 84
      {
80 85
      super.onCreate(savedState);
81 86

  
82
      mMesh = new MeshSphere(LEVEL);
83
      mTexture= new DistortedTexture(SIZE,SIZE);
84

  
85 87
      setContentView(R.layout.earthlayout);
86 88

  
87 89
      SeekBar levelBar = findViewById(R.id.earthInflateLevel);
88 90
      levelBar.setOnSeekBarChangeListener(this);
89 91
      levelBar.setProgress(50);
92

  
93
      Spinner renderSpinner  = findViewById(R.id.earthSpinnerEffect);
94
      renderSpinner.setOnItemSelectedListener(this);
95

  
96
      ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, createEffectNames() );
97
      adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
98
      renderSpinner.setAdapter(adapterBitmap);
99
      }
100

  
101

  
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

  
104
    @Override
105
    public void onSaveInstanceState(Bundle savedInstanceState)
106
      {
107
      super.onSaveInstanceState(savedInstanceState);
108

  
109
      EarthSurfaceViewPicker pick = this.findViewById(R.id.earthSurfaceViewPicker);
110
      pick.save(savedInstanceState);
111
      }
112

  
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

  
115
    @Override
116
    public void onRestoreInstanceState(Bundle savedInstanceState)
117
      {
118
      super.onRestoreInstanceState(savedInstanceState);
119

  
120
      EarthSurfaceViewPicker pick = this.findViewById(R.id.earthSurfaceViewPicker);
121
      pick.restore(savedInstanceState);
90 122
      }
91 123

  
92 124
///////////////////////////////////////////////////////////////////////////////////////////////////
......
111 143

  
112 144
///////////////////////////////////////////////////////////////////////////////////////////////////
113 145

  
114
    public DistortedTexture getTexture()
146
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
115 147
      {
116
      return mTexture;
148
      EarthSurfaceViewPicker mPick = this.findViewById(R.id.earthSurfaceViewPicker);
149
      mPick.setEffectMode(mEffectNames[pos]);
117 150
      }
118 151

  
119 152
///////////////////////////////////////////////////////////////////////////////////////////////////
120 153

  
121
    public MeshBase getMesh()
154
    public void onNothingSelected(AdapterView<?> parent) { }
155

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

  
158
    public void Remove(View v)
122 159
      {
123
      return mMesh;
160
      EarthSurfaceViewPicker mPick = this.findViewById(R.id.earthSurfaceViewPicker);
161
      mPick.clearPoints();
162

  
163
      EarthSurfaceView view = this.findViewById(R.id.earthSurfaceView);
164
      EarthRenderer renderer = view.getRenderer();
165

  
166
      renderer.removeAll();
167
      }
168

  
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

  
171
    void addNewPoint(float x, float y, EffectName effect)
172
      {
173
      EarthSurfaceView view = this.findViewById(R.id.earthSurfaceView);
174
      EarthRenderer renderer = view.getRenderer();
175

  
176
      renderer.addNewPoint(x,y,effect);
177
      }
178

  
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

  
181
    private String[] createEffectNames()
182
      {
183
      EffectType type;
184
      EffectName[] names = EffectName.values();
185

  
186
      int numEffects=0;
187

  
188
      for(int i=0; i<names.length; i++)
189
        {
190
        type = names[i].getType();
191
        if (type == EffectType.FRAGMENT || type == EffectType.VERTEX) numEffects++;
192
        }
193

  
194
      mEffectNames = new EffectName[numEffects];
195
      String[] effectStrings = new String[numEffects];
196

  
197
      numEffects=0;
198

  
199
      for(int i=0; i<names.length; i++)
200
        {
201
        type = names[i].getType();
202

  
203
        if (type == EffectType.FRAGMENT || type == EffectType.VERTEX)
204
          {
205
          mEffectNames[numEffects]  = names[i];
206
          effectStrings[numEffects] = names[i].name();
207

  
208
          numEffects++;
209
          }
210
        }
211

  
212
      return effectStrings;
124 213
      }
125 214
}
src/main/java/org/distorted/examples/earth/EarthRenderer.java
24 24
import android.opengl.GLSurfaceView;
25 25

  
26 26
import org.distorted.examples.R;
27
import org.distorted.library.effect.EffectName;
28
import org.distorted.library.effect.EffectType;
27 29
import org.distorted.library.effect.MatrixEffectMove;
28 30
import org.distorted.library.effect.MatrixEffectQuaternion;
29 31
import org.distorted.library.effect.MatrixEffectScale;
......
32 34
import org.distorted.library.main.DistortedScreen;
33 35
import org.distorted.library.main.DistortedTexture;
34 36
import org.distorted.library.mesh.MeshBase;
37
import org.distorted.library.mesh.MeshSphere;
35 38
import org.distorted.library.type.DynamicQuat;
36 39
import org.distorted.library.type.Static3D;
37 40
import org.distorted.library.type.Static4D;
......
46 49

  
47 50
class EarthRenderer implements GLSurfaceView.Renderer
48 51
{
49
    private static final float FOV = 30.0f;
50
    private static final float NEAR = 0.1f;
52
    private static final int   SIZE =   100;
53
    private static final int   LEVEL=    32;
54
    private static final float FOV  = 30.0f;
55
    private static final float NEAR =  0.1f;
51 56

  
52 57
    private GLSurfaceView mView;
53 58
    private DistortedTexture mTexture;
......
70 75
      mScale= new Static3D(1,1,1);
71 76
      mCenter=new Static3D(0,0,0);
72 77

  
73
      EarthActivity act = (EarthActivity)v.getContext();
74

  
75
      mTexture = act.getTexture();
76
      mMesh    = act.getMesh();
78
      mMesh   = new MeshSphere(LEVEL);
79
      mTexture= new DistortedTexture(SIZE,SIZE);
77 80

  
78 81
      mObjWidth = mTexture.getWidth();
79 82
      mObjHeight= mTexture.getHeight();
......
127 130
      mMesh.setInflate(inflateLevel);
128 131
      }
129 132

  
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

  
135
    void addNewPoint(float x, float y, EffectName effect)
136
      {
137

  
138
      }
139

  
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

  
142
    void removeAll()
143
      {
144
      mEffects.abortByType(EffectType.VERTEX);
145
      mEffects.abortByType(EffectType.FRAGMENT);
146
      }
147

  
130 148
///////////////////////////////////////////////////////////////////////////////////////////////////
131 149
    
132 150
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
src/main/java/org/distorted/examples/earth/EarthSurfaceViewPicker.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.earth;
21

  
22
import android.content.Context;
23
import android.graphics.Bitmap;
24
import android.graphics.BitmapFactory;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.graphics.Rect;
28
import android.os.Bundle;
29
import android.util.AttributeSet;
30
import android.view.MotionEvent;
31
import android.view.SurfaceHolder;
32
import android.view.SurfaceView;
33

  
34
import org.distorted.examples.R;
35
import org.distorted.library.effect.EffectName;
36

  
37
import java.io.IOException;
38
import java.io.InputStream;
39
import java.lang.ref.WeakReference;
40
import java.util.ArrayList;
41

  
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

  
44
class EarthSurfaceViewPicker extends SurfaceView implements SurfaceHolder.Callback
45
{
46
    private static final int FRAME_INTERVAL = 70;
47
    private static final int MULTIPLIER   = 1000;
48

  
49
    private static boolean refreshScreen = true;
50
    private GraphicsThread mThread;
51
    private EffectName mCurrentEffect;
52
    private ArrayList<Integer> mPointsX, mPointsY, mPointsE;
53
    private WeakReference<EarthActivity> mAct;
54
    private int mRadius;
55
    private Bitmap mBitmap;
56
    private Paint mPointPaint;
57
    private Rect mDst;
58
    private int mWidth, mHeight;
59

  
60
/////////////////////////////////////////////////////////////////////////////////////////
61

  
62
    private class GraphicsThread extends Thread
63
      {
64
      private final SurfaceHolder mSurfaceHolder;
65
      private final EarthSurfaceViewPicker mPicker;
66
      private boolean mRun = false;
67

  
68
      ///////////////////////////////////////////////////////////////////////////////////////
69

  
70
      GraphicsThread(SurfaceHolder surfaceHolder, EarthSurfaceViewPicker p)
71
        {
72
        mSurfaceHolder = surfaceHolder;
73
        mPicker = p;
74
        }
75

  
76
      ///////////////////////////////////////////////////////////////////////////////////////
77

  
78
      void setRunning(boolean run)
79
        {
80
        mRun = run;
81
        }
82

  
83
      ///////////////////////////////////////////////////////////////////////////////////////
84

  
85
      public void run()
86
        {
87
        Canvas c;
88
        long time;
89

  
90
        while (mRun)
91
          {
92
          c = null;
93
          time = 0;
94

  
95
          if( refreshScreen )
96
            {
97
            refreshScreen=false;
98
            time = System.currentTimeMillis();
99

  
100
            try
101
              {
102
              c = mSurfaceHolder.lockCanvas(null);
103
              synchronized (mSurfaceHolder) { mPicker.render(c); }
104
              }
105
            finally
106
              {
107
              if (c != null)  mSurfaceHolder.unlockCanvasAndPost(c);
108
              }
109

  
110
            time = System.currentTimeMillis() -time;
111
            }
112

  
113
          if( time<FRAME_INTERVAL )
114
            {
115
            try { Thread.sleep(FRAME_INTERVAL-time); }
116
            catch(InterruptedException ex) {}
117
            }
118
          }
119
        }
120
      }
121

  
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

  
124
    public EarthSurfaceViewPicker(Context context, AttributeSet attrs)
125
      {
126
      super(context,attrs);
127

  
128
      InputStream is = context.getResources().openRawResource(R.raw.world);
129

  
130
      try
131
        {
132
        mBitmap = BitmapFactory.decodeStream(is);
133
        }
134
      finally
135
        {
136
        try
137
          {
138
          is.close();
139
          }
140
        catch(IOException e) { }
141
        }
142

  
143
      EarthActivity act = (EarthActivity)context;
144
      mAct = new WeakReference<>(act);
145

  
146
      mDst = new Rect();
147
      mPointsX = new ArrayList<>();
148
      mPointsY = new ArrayList<>();
149
      mPointsE = new ArrayList<>();
150

  
151
      mPointPaint = new Paint();
152
      mPointPaint.setARGB(255,0,0,0);
153

  
154
      getHolder().addCallback(this);
155
      }
156

  
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

  
159
    private void stopThread()
160
      {
161
      if( mThread!=null )
162
        {
163
        boolean retry = true;
164
        mThread.setRunning(false);
165

  
166
        while (retry)
167
          {
168
          try
169
            {
170
            mThread.join();
171
            retry = false;
172
            }
173
          catch (InterruptedException e) { android.util.Log.e( "EarthPicker", "Joining thread interrupted!"); }
174
          }
175

  
176
        mThread=null;
177
        }
178
      }
179

  
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

  
182
    void setEffectMode(EffectName effect)
183
      {
184
      mCurrentEffect = effect;
185
      }
186

  
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

  
189
    private void addNewPoint(int x, int y)
190
      {
191
      float xf = (float)x/mWidth;
192
      float yf = (float)y/mHeight;
193

  
194
      mPointsX.add( (int)(MULTIPLIER*xf) );
195
      mPointsY.add( (int)(MULTIPLIER*yf) );
196
      mPointsE.add(mCurrentEffect.ordinal());
197

  
198
      mAct.get().addNewPoint(xf,yf,mCurrentEffect);
199
      }
200

  
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

  
203
    void save(Bundle bundle)
204
      {
205
      bundle.putIntegerArrayList("x", mPointsX);
206
      bundle.putIntegerArrayList("y", mPointsY);
207
      bundle.putIntegerArrayList("e", mPointsE);
208
      }
209

  
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

  
212
    void restore(Bundle bundle)
213
      {
214
      mPointsX = bundle.getIntegerArrayList("x");
215
      mPointsY = bundle.getIntegerArrayList("y");
216
      mPointsE = bundle.getIntegerArrayList("e");
217
      }
218

  
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

  
221
    void clearPoints()
222
      {
223
      mPointsX.clear();
224
      mPointsY.clear();
225
      mPointsE.clear();
226

  
227
      refreshScreen = true;
228
      }
229

  
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231

  
232
    private void render(Canvas c)
233
      {
234
      if( c!=null )
235
        {
236
        c.drawBitmap(mBitmap, null, mDst, null);
237

  
238
        int x,y,size = mPointsX.size();
239

  
240
        for(int point=0; point<size; point++)
241
          {
242
          x = (int)(((float)mPointsX.get(point)/MULTIPLIER)*mWidth );
243
          y = (int)(((float)mPointsY.get(point)/MULTIPLIER)*mHeight);
244

  
245
          c.drawCircle( x, y, mRadius, mPointPaint);
246
          }
247
        }
248
      }
249

  
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

  
252
    void onPause()
253
      {
254
      if( mThread!=null ) mThread.setRunning(false);
255
      }
256

  
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

  
259
    void onResume()
260
      {
261
      if( mThread!=null ) mThread.setRunning(true);
262
      }
263

  
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265

  
266
    @Override
267
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
268
      {
269
      mWidth = width;
270
      mHeight= height;
271

  
272
      mDst.bottom = height;
273
      mDst.right  = width;
274
      mDst.left   = 0;
275
      mDst.top    = 0;
276

  
277
      mRadius = (width+height)/60;
278

  
279
      refreshScreen = true;
280
      }
281

  
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

  
284
    @Override
285
    public void surfaceCreated(SurfaceHolder holder)
286
      {
287
      mThread = new GraphicsThread(getHolder(), this);
288
      mThread.setRunning(true);
289
      mThread.start();
290
      }
291

  
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293

  
294
    @Override
295
    public void surfaceDestroyed(SurfaceHolder holder)
296
      {
297
      stopThread();
298
      }
299

  
300
///////////////////////////////////////////////////////////////////////////////////////////////////
301

  
302
    @Override
303
    public boolean onTouchEvent(MotionEvent event)
304
      {
305
      int action = event.getAction();
306
      int x = (int)event.getX();
307
      int y = (int)event.getY();
308
           
309
      switch(action)
310
         {
311
         case MotionEvent.ACTION_DOWN: addNewPoint(x,y);
312
                                       refreshScreen = true;
313
                                       break;
314
         }
315
             
316
      return true;
317
      }
318
}
319

  
src/main/java/org/distorted/examples/inflate/InflateActivity2.java
108 108

  
109 109
      switch(objectType)
110 110
        {
111
        case 0: mMesh = new MeshCubes(mNumCols, str, mNumSlic);
112
          break;
113
        case 1: mMesh = new MeshFlat(mNumCols,mNumRows);
114
          break;
115
        case 2: mMesh = new MeshSphere(mNumRows);
116
          break;
117
        case 3: mMesh = new MeshQuad();
118
          break;
111
        case 0: mMesh = new MeshCubes(mNumCols, str, mNumSlic); break;
112
        case 1: mMesh = new MeshFlat(mNumCols,mNumRows)       ; break;
113
        case 2: mMesh = new MeshSphere(mNumRows)              ; break;
114
        case 3: mMesh = new MeshQuad()                        ; break;
119 115
        }
120 116

  
121 117
      mTexture = new DistortedTexture(mNumCols,mNumRows);
src/main/res/layout/earthlayout.xml
9 9
        android:id="@+id/earthSurfaceView"
10 10
        android:layout_width="match_parent"
11 11
        android:layout_height="0dp"
12
        android:layout_weight="1.00" />
12
        android:layout_weight="0.60" />
13 13

  
14 14
    <LinearLayout
15 15
        android:id="@+id/linearLayout"
......
28 28

  
29 29
    </LinearLayout>
30 30

  
31
    <org.distorted.examples.earth.EarthSurfaceViewPicker
32
        android:id="@+id/earthSurfaceViewPicker"
33
        android:layout_width="match_parent"
34
        android:layout_height="0dp"
35
        android:layout_weight="0.40" />
36

  
37
    <LinearLayout
38
        android:id="@+id/linearLayout2"
39
        android:layout_width="fill_parent"
40
        android:layout_height="wrap_content"
41
        android:gravity="center|fill_horizontal"
42
        android:orientation="horizontal"
43
        android:background="@color/blue">
44

  
45
        <Button
46
            android:id="@+id/button"
47
            android:layout_width="0dp"
48
            android:layout_height="wrap_content"
49
            android:layout_weight="0.3"
50
            android:onClick="Remove"
51
            android:text="@string/removebut" />
52

  
53
        <Spinner
54
            android:layout_width="0dp"
55
            android:layout_height="50dp"
56
            android:layout_weight="0.7"
57
            android:id="@+id/earthSpinnerEffect" />
58

  
59
    </LinearLayout>
60

  
31 61
</LinearLayout>

Also available in: Unified diff