Project

General

Profile

« Previous | Next » 

Revision 64558e4e

Added by Leszek Koltunski over 5 years ago

Rename the old 'Cubes' app to 'Inflate' - this now combines the testing functionality of former 'Cubes' and 'Inflate' apps.
The old 'Inflate' in turn renamed to 'Earth' - here we are going to show an Earth rotating in space.

View differences:

src/main/AndroidManifest.xml
31 31
        <activity android:name=".check.CheckActivity" />    
32 32
        <activity android:name=".objecttree.ObjectTreeActivity" />
33 33
        <activity android:name=".starwars.StarWarsActivity" />      
34
        <activity android:name=".olimpic.OlimpicActivity" />  
35
        <activity android:name=".cubes.CubesActivity" />       
34
        <activity android:name=".olimpic.OlimpicActivity" />
35
        <activity android:name=".inflate.InflateActivity" />
36
        <activity android:name=".inflate.InflateActivity2" />
36 37
        <activity android:name=".quaternion.QuaternionActivity" />          
37 38
        <activity android:name=".effects3d.Effects3DActivity" />
38 39
        <activity android:name=".effects3d.Effects3DActivity2"/>
......
50 51
        <activity android:name=".stencil.StencilActivity"/>
51 52
        <activity android:name=".glow.GlowActivity"/>
52 53
        <activity android:name=".movingglow.MovingGlowActivity"/>
53
        <activity android:name=".inflate.InflateActivity"/>
54
        <activity android:name=".inflate.InflateActivity2"/>
54
        <activity android:name=".earth.EarthActivity"/>
55
        <activity android:name=".earth.EarthActivity2"/>
55 56
    </application>
56 57
</manifest>
src/main/java/org/distorted/examples/TableOfContents.java
51 51
import org.distorted.examples.bean.BeanActivity;
52 52
import org.distorted.examples.objecttree.ObjectTreeActivity;
53 53
import org.distorted.examples.starwars.StarWarsActivity;
54
import org.distorted.examples.cubes.CubesActivity;
54
import org.distorted.examples.inflate.InflateActivity;
55 55
import org.distorted.examples.quaternion.QuaternionActivity;
56 56
import org.distorted.examples.effects3d.Effects3DActivity;
57 57
import org.distorted.examples.plainmonalisa.PlainMonaLisaActivity;
......
68 68
import org.distorted.examples.stencil.StencilActivity;
69 69
import org.distorted.examples.glow.GlowActivity;
70 70
import org.distorted.examples.movingglow.MovingGlowActivity;
71
import org.distorted.examples.inflate.InflateActivity;
71
import org.distorted.examples.earth.EarthActivity;
72 72

  
73 73
///////////////////////////////////////////////////////////////////////////////////////////////////
74 74

  
......
256 256
   {
257 257
      final Map<String, Object> item = new HashMap<>();
258 258
      item.put(ITEM_IMAGE, R.drawable.icon_example_cubes);
259
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_cubes));
260
      item.put(ITEM_SUBTITLE, getText(R.string.example_cubes_subtitle));
259
      item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_inflate));
260
      item.put(ITEM_SUBTITLE, getText(R.string.example_inflate_subtitle));
261 261
      data.add(item);
262
      activityMapping.put(i++, CubesActivity.class);
262
      activityMapping.put(i++, InflateActivity.class);
263 263
   }
264 264

  
265 265
   {
......
400 400
   {
401 401
   final Map<String, Object> item = new HashMap<>();
402 402
   item.put(ITEM_IMAGE, R.drawable.icon_example_wip);
403
   item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_inflate));
404
   item.put(ITEM_SUBTITLE, getText(R.string.example_inflate_subtitle));
403
   item.put(ITEM_TITLE, (i+1)+". "+getText(R.string.example_earth));
404
   item.put(ITEM_SUBTITLE, getText(R.string.example_earth_subtitle));
405 405
   data.add(item);
406
   activityMapping.put(i++, InflateActivity.class);
406
   activityMapping.put(i++, EarthActivity.class);
407 407
   }
408 408

  
409 409
   final SimpleAdapter dataAdapter = new SimpleAdapter(this, data, R.layout.toc_item, new String[] {ITEM_IMAGE, ITEM_TITLE, ITEM_SUBTITLE}, new int[] {R.id.Image, R.id.Title, R.id.SubTitle});
src/main/java/org/distorted/examples/cubes/CubesActivity.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.cubes;
21

  
22
import org.distorted.library.main.Distorted;
23
import org.distorted.examples.R;
24
import org.distorted.library.main.DistortedTexture;
25
import org.distorted.library.mesh.MeshBase;
26
import org.distorted.library.mesh.MeshCubes;
27

  
28
import android.app.Activity;
29
import android.opengl.GLSurfaceView;
30
import android.os.Bundle;
31
import android.view.Gravity;
32
import android.view.View;
33
import android.widget.AdapterView;
34
import android.widget.ArrayAdapter;
35
import android.widget.Button;
36
import android.widget.LinearLayout;
37
import android.widget.NumberPicker;
38
import android.widget.NumberPicker.OnValueChangeListener;
39
import android.widget.SeekBar;
40
import android.widget.Spinner;
41
import android.widget.TableRow;
42

  
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

  
45
public class CubesActivity extends Activity implements View.OnClickListener, SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
46
{
47
    private static final int COLOR_OFF = 0xffffe81f;
48
    private static final int COLOR_ON  = 0xff0000ff;
49

  
50
    private int mNumCols = 3;
51
    private int mNumRows = 3;
52
    private int mNumSlic = 1;
53
    private NumberPicker mColsPicker, mRowsPicker, mSlicPicker;
54
    private LinearLayout mLay;
55
    private boolean[] mShape;
56
    private DistortedTexture mTexture;
57
    private MeshBase mMesh;
58

  
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
    
61
    @Override
62
    protected void onCreate(Bundle savedState) 
63
      {
64
      super.onCreate(savedState);
65

  
66
      setContentView(R.layout.cubespickerlayout);
67
      
68
      mLay = findViewById(R.id.cubespicker_buttongrid);
69
      
70
      mColsPicker = findViewById(R.id.cubespicker_cols);
71
      mRowsPicker = findViewById(R.id.cubespicker_rows);
72
      mSlicPicker = findViewById(R.id.cubespicker_slices);
73

  
74
      mColsPicker.setMaxValue(40);
75
      mColsPicker.setMinValue( 0);
76
      mRowsPicker.setMaxValue(40);
77
      mRowsPicker.setMinValue( 0);
78
      mSlicPicker.setMaxValue(40);
79
      mSlicPicker.setMinValue( 0);
80

  
81
      mColsPicker.setOnValueChangedListener(new OnValueChangeListener() 
82
         {
83
         @Override
84
         public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
85
           { 
86
           setGrid();
87
           }
88
         });
89
      
90
      mRowsPicker.setOnValueChangedListener(new OnValueChangeListener() 
91
         {
92
         @Override
93
         public void onValueChange(NumberPicker picker, int oldVal, int newVal) 
94
           { 
95
           setGrid();
96
           }
97
         });
98

  
99
      mSlicPicker.setOnValueChangedListener(new OnValueChangeListener()
100
         {
101
         @Override
102
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
103
           {
104
           mNumSlic = mSlicPicker.getValue();
105
           }
106
         });
107
      }
108

  
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
    
111
    private void setGrid()
112
      {
113
      mNumCols = mColsPicker.getValue();
114
      mNumRows = mRowsPicker.getValue();
115
      
116
      int width = mLay.getWidth();
117
      int height= mLay.getHeight();
118
      int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0;
119
      int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0;
120
      int size= w<h ? w:h;
121
      int pad = size<20 ? 1 : size/20;
122
     
123
      mLay.removeAllViews();
124
      
125
      mShape = new boolean[mNumRows*mNumCols];
126

  
127
      TableRow.LayoutParams p = new android.widget.TableRow.LayoutParams();
128

  
129
      p.rightMargin  = pad;
130
      p.leftMargin   = pad;
131
      p.topMargin    = pad;
132
      p.bottomMargin = pad;
133
      p.height       = size;
134
      p.width        = size;
135
      
136
      for (int rows=0; rows<mNumRows; rows++) 
137
        {
138
        TableRow tr = new TableRow(this);
139
        tr.setGravity(Gravity.CENTER);
140
        
141
        for(int cols=0; cols<mNumCols; cols++) 
142
          {
143
          Button b = new Button(this);
144
          b.setOnClickListener(this);
145
          b.setId(rows*mNumCols+cols);
146
          b.setLayoutParams(p);          
147
          b.setBackgroundColor(COLOR_ON);
148
          tr.addView(b, p);
149
          mShape[rows*mNumCols+cols] = true;
150
          }
151
        
152
        mLay.addView(tr);
153
        }
154
      }
155

  
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
    
158
    public void onClick(View view) 
159
      {
160
      Button tmp = (Button)view;  
161
      int id = tmp.getId();
162
      mShape[id] = !mShape[id];
163
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
164
      }
165

  
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
    
168
    @Override
169
    public void onWindowFocusChanged(boolean hasFocus) 
170
      {
171
      super.onWindowFocusChanged(hasFocus);
172

  
173
      mColsPicker.setValue(mNumCols);
174
      mRowsPicker.setValue(mNumRows);
175
      mSlicPicker.setValue(mNumSlic);
176

  
177
      if( hasFocus ) setGrid();
178
      }
179
    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
    
182
    @Override
183
    protected void onPause() 
184
      {
185
      GLSurfaceView mView = this.findViewById(R.id.cubesSurfaceView);
186
      if( mView!=null ) mView.onPause();
187

  
188
      Distorted.onPause();
189
      super.onPause();
190
      }
191

  
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
    
194
    @Override
195
    protected void onResume() 
196
      {
197
      super.onResume();
198
      
199
      GLSurfaceView mView = this.findViewById(R.id.cubesSurfaceView);
200
      if( mView!=null ) mView.onResume();  
201
      }
202
    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
    
205
    @Override
206
    protected void onDestroy() 
207
      {
208
      Distorted.onDestroy();  
209
      super.onDestroy();
210
      }
211
 
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
    
214
    public void Create(View v)
215
      {
216
      String str = "";
217

  
218
      for(int i=0; i<mNumRows*mNumCols; i++)
219
        str += mShape[i] ? "1" : "0";
220

  
221
      mMesh = new MeshCubes(mNumCols, str, mNumSlic);
222
      mTexture = new DistortedTexture(mNumCols,mNumRows);
223

  
224
      setContentView(R.layout.cubeslayout);
225

  
226
      Spinner renderSpinner  = findViewById(R.id.cubes_spinnerMode);
227
      renderSpinner.setOnItemSelectedListener(this);
228

  
229
      String[] objectBitmap = new String[] { "Render: Normal", "Render: OIT" };
230

  
231
      ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
232
      adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
233
      renderSpinner.setAdapter(adapterBitmap);
234

  
235
      SeekBar transparencyBar = findViewById(R.id.cubesTransparency);
236

  
237
      transparencyBar.setOnSeekBarChangeListener(this);
238

  
239
      CubesSurfaceView view = this.findViewById(R.id.cubesSurfaceView);
240
      view.getRenderer().setTransparency(50);
241
      transparencyBar.setProgress(50);
242
      }
243

  
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245

  
246
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
247
      {
248
      CubesSurfaceView v = this.findViewById(R.id.cubesSurfaceView);
249
      CubesRenderer renderer = v.getRenderer();
250

  
251
      switch(parent.getId())
252
        {
253
        case R.id.cubes_spinnerMode: renderer.setRenderModeToOIT(pos==1);
254
                                     break;
255
        }
256
      }
257

  
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

  
260
    public void onNothingSelected(AdapterView<?> parent)
261
      {
262
      }
263

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

  
266
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
267
      {
268
      switch (bar.getId())
269
        {
270
        case R.id.cubesTransparency: CubesSurfaceView view = this.findViewById(R.id.cubesSurfaceView);
271
                                     view.getRenderer().setTransparency(progress);
272
                                     break;
273
        }
274
      }
275

  
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

  
278
    public void onStartTrackingTouch(SeekBar bar) { }
279

  
280
///////////////////////////////////////////////////////////////////////////////////////////////////
281

  
282
    public void onStopTrackingTouch(SeekBar bar)  { }
283

  
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

  
286
    public DistortedTexture getTexture()
287
      {
288
      return mTexture;
289
      }
290

  
291
///////////////////////////////////////////////////////////////////////////////////////////////////
292

  
293
    public MeshBase getMesh()
294
      {
295
      return mMesh;
296
      }
297
}
src/main/java/org/distorted/examples/cubes/CubesRenderer.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.cubes;
21

  
22
import java.io.IOException;
23
import java.io.InputStream;
24

  
25
import javax.microedition.khronos.egl.EGLConfig;
26
import javax.microedition.khronos.opengles.GL10;
27

  
28
import org.distorted.examples.R;
29

  
30
import org.distorted.library.effect.FragmentEffectAlpha;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.mesh.MeshBase;
38
import org.distorted.library.type.DynamicQuat;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static4D;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.main.Distorted;
43

  
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46
import android.opengl.GLSurfaceView;
47

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

  
50
class CubesRenderer implements GLSurfaceView.Renderer 
51
{
52
    private GLSurfaceView mView;
53
    private DistortedTexture mTexture;
54
    private DistortedEffects mEffects;
55
    private MeshBase mMesh;
56
    private DistortedScreen mScreen;
57
    private int mObjWidth, mObjHeight, mObjDepth;
58
    private Static3D mMove, mScale, mCenter;
59
    private Static1D mAlpha;
60

  
61
    Static4D mQuat1, mQuat2;
62
    int mScreenMin;
63

  
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

  
66
    CubesRenderer(GLSurfaceView v)
67
      {
68
      mView = v;
69

  
70
      mAlpha = new Static1D(1.0f);
71

  
72
      mMove = new Static3D(0,0,0);
73
      mScale= new Static3D(1,1,1);
74
      mCenter=new Static3D(0,0,0);
75

  
76
      CubesActivity act = (CubesActivity)v.getContext();
77

  
78
      mTexture = act.getTexture();
79
      mMesh = act.getMesh();
80

  
81
      mObjWidth = mTexture.getWidth();
82
      mObjHeight= mTexture.getHeight();
83
      mObjDepth = mTexture.getDepth(mMesh);
84

  
85
      mQuat1 = new Static4D(0,0,0,1);  // unity
86
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
87
      
88
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
89
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
90

  
91
      quatInt1.add(mQuat1);
92
      quatInt2.add(mQuat2);
93

  
94
      mEffects = new DistortedEffects();
95
      mEffects.apply( new MatrixEffectMove(mMove) );
96
      mEffects.apply( new MatrixEffectScale(mScale));
97
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) );
98
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, mCenter) );
99
      mEffects.apply( new FragmentEffectAlpha(mAlpha));
100

  
101
      mScreen = new DistortedScreen();
102
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
103
      }
104

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

  
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
115
      {
116
      mScreenMin = width<height ? width:height;
117
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (0.75f*height)/mObjHeight :  (0.75f*width)/mObjWidth;
118
      mMove.set( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0);
119
      mScale.set(factor,factor,factor);
120
      mCenter.set( (float)mObjWidth/2, (float)mObjHeight/2, (float)mObjDepth/2 );
121
      mScreen.resize(width, height);
122
      }
123

  
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

  
126
    void setRenderModeToOIT(boolean oit)
127
      {
128
      mScreen.setOrderIndependentTransparency(oit);
129
      }
130

  
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

  
133
    void setTransparency(int level)
134
      {
135
      mAlpha.set((float)level/100.0f);
136
      }
137

  
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
141
      {
142
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
143
      Bitmap bitmap;
144
        
145
      try 
146
        {
147
        bitmap = BitmapFactory.decodeStream(is);
148
        } 
149
      finally 
150
        {
151
        try 
152
          {
153
          is.close();
154
          } 
155
        catch(IOException e) { }
156
        }  
157
      
158
      mTexture.setTexture(bitmap);
159

  
160
      mScreen.detachAll();
161
      mScreen.attach(mTexture,mEffects,mMesh);
162

  
163
      FragmentEffectAlpha.enable();
164

  
165
      try
166
        {
167
        Distorted.onCreate(mView.getContext());
168
        }
169
      catch(Exception ex)
170
        {
171
        android.util.Log.e("Cubes", ex.getMessage() );
172
        }
173
      }
174
}
src/main/java/org/distorted/examples/cubes/CubesSurfaceView.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.cubes;
21

  
22
import org.distorted.examples.R;
23

  
24
import android.app.ActivityManager;
25
import android.content.Context;
26
import android.content.pm.ConfigurationInfo;
27
import android.opengl.GLSurfaceView;
28
import android.util.AttributeSet;
29
import android.view.MotionEvent;
30
import android.widget.Toast;
31

  
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

  
34
class CubesSurfaceView extends GLSurfaceView 
35
{
36
    private int mX, mY;
37
    private CubesRenderer mRenderer;
38
	
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
   
41
    public CubesSurfaceView(Context context, AttributeSet attrs)
42
      {
43
      super(context,attrs);
44
    
45
      mX = -1;
46
      mY = -1;
47
      
48
      if(!isInEditMode())
49
        {
50
        mRenderer = new CubesRenderer(this);
51
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
52
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
53
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
54
        setRenderer(mRenderer);
55
        Toast.makeText(context, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
56
        }
57
      }
58

  
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

  
61
    public CubesRenderer getRenderer()
62
      {
63
      return mRenderer;
64
      }
65

  
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
    
68
    @Override public boolean onTouchEvent(MotionEvent event) 
69
      {
70
      int action = event.getAction();
71
      int x = (int)event.getX();
72
      int y = (int)event.getY();
73
           
74
      switch(action)
75
         {
76
         case MotionEvent.ACTION_DOWN: mX = x;
77
                                       mY = y;
78
                                       break;
79
                                       
80
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
81
                                         {
82
                                         float px = mY-y;
83
                                         float py = mX-x;
84
                                         float pz = 0;
85
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
86
                                         
87
                                         if( plen>0 )
88
                                           {
89
                                           px /= plen;
90
                                           py /= plen;
91
                                           pz /= plen;
92

  
93
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
94
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
95
                                         
96
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
97
                                           }
98
                                         }                             
99
                                       break;
100
                                       
101
         case MotionEvent.ACTION_UP  : mX = -1;
102
                                       mY = -1;
103
        	                           
104
                                       float qx = mRenderer.mQuat1.get1();
105
                                       float qy = mRenderer.mQuat1.get2();
106
                                       float qz = mRenderer.mQuat1.get3();
107
                                       float qw = mRenderer.mQuat1.get4();
108

  
109
                                       float rx = mRenderer.mQuat2.get1();
110
                                       float ry = mRenderer.mQuat2.get2();
111
                                       float rz = mRenderer.mQuat2.get3();
112
                                       float rw = mRenderer.mQuat2.get4();
113

  
114
                                       // This is quaternion multiplication. (tx,ty,tz,tw)
115
                                       // is now equal to (qx,qy,qz,qw)*(rx,ry,rz,rw)
116
                                       float tx = rw*qx - rz*qy + ry*qz + rx*qw;
117
                                       float ty = rw*qy + rz*qx + ry*qw - rx*qz;
118
                                       float tz = rw*qz + rz*qw - ry*qx + rx*qy;
119
                                       float tw = rw*qw - rz*qz - ry*qy - rx*qx;
120

  
121
                                       // The point of this is so that there are always
122
                                       // exactly 2 quaternions: Quat1 representing the rotation
123
                                       // accumulating only since the last screen touch, and Quat2
124
                                       // which remembers the combined effect of all previous
125
                                       // swipes.
126
                                       // We cannot be accumulating an ever-growing list of quaternions
127
                                       // and add a new one every time user swipes the screen - there
128
                                       // is a limited number of slots in the EffectQueueMatrix!
129
                                       mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
130
                                       mRenderer.mQuat2.set(tx, ty, tz, tw);
131
                                       
132
                                       break;
133
         }
134
             
135
      return true;
136
      }
137
         
138
}
139

  
src/main/java/org/distorted/examples/earth/EarthActivity.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.app.Activity;
23
import android.content.Intent;
24
import android.os.Bundle;
25
import android.view.Gravity;
26
import android.view.View;
27
import android.widget.AdapterView;
28
import android.widget.ArrayAdapter;
29
import android.widget.Button;
30
import android.widget.LinearLayout;
31
import android.widget.NumberPicker;
32
import android.widget.Spinner;
33
import android.widget.TableRow;
34

  
35
import org.distorted.examples.R;
36

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

  
39
public class EarthActivity extends Activity
40
                               implements View.OnClickListener, AdapterView.OnItemSelectedListener
41
  {
42
  private static final int COLOR_OFF = 0xffffe81f;
43
  private static final int COLOR_ON  = 0xff0000ff;
44
  private static final int COLOR_INAC= 0xff999999;
45

  
46
  private int mNumCols = 1;
47
  private int mNumRows = 1;
48
  private int mNumSlic = 1;
49
  private boolean mGridInitialized;
50
  private NumberPicker mColsPicker, mRowsPicker, mSlicPicker;
51
  private boolean[] mShape;
52
  private int mObjectType;
53
  private int mBitmapID;
54
  private LinearLayout mLay;
55

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

  
58
  @Override
59
  protected void onCreate(Bundle savedState)
60
    {
61
    super.onCreate(savedState);
62

  
63
    setContentView(R.layout.objectpickerlayout);
64

  
65
    mLay = findViewById(R.id.objectpicker_buttongrid);
66

  
67
    mColsPicker = findViewById(R.id.objectpicker_cols);
68
    mRowsPicker = findViewById(R.id.objectpicker_rows);
69
    mSlicPicker = findViewById(R.id.objectpicker_slices);
70

  
71
    mColsPicker.setMaxValue(40);
72
    mColsPicker.setMinValue( 1);
73
    mRowsPicker.setMaxValue(40);
74
    mRowsPicker.setMinValue( 1);
75
    mSlicPicker.setMaxValue(40);
76
    mSlicPicker.setMinValue( 0);
77

  
78
    mColsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
79
         {
80
         @Override
81
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
82
           {
83
           setGrid();
84
           }
85
         });
86

  
87
    mRowsPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
88
         {
89
         @Override
90
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
91
           {
92
           setGrid();
93
           }
94
         });
95

  
96
    mSlicPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener()
97
         {
98
         @Override
99
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
100
           {
101
           mNumSlic = mSlicPicker.getValue();
102
           }
103
         });
104

  
105
    mObjectType = 0;
106
    mGridInitialized = false;
107

  
108
    Spinner typeSpinner  = findViewById(R.id.objectpicker_spinnerType);
109
    typeSpinner.setOnItemSelectedListener(this);
110

  
111
    String[] objectType = new String[] {"Mesh: Cubes", "Mesh: Flat", "Mesh: Sphere", "Mesh: Quad"};
112

  
113
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
114
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
115
    typeSpinner.setAdapter(adapterType);
116

  
117
    Spinner bitmapSpinner  = findViewById(R.id.objectpicker_spinnerBitmap);
118
    bitmapSpinner.setOnItemSelectedListener(this);
119

  
120
    String[] objectBitmap = new String[] { "Texture: Grid", "Texture: Girl", "Texture: Dog", "Texture: Cat",
121
                                           "Texture: Squares", "Texture: Bean", "Texture: Lisa", "Texture: World" };
122

  
123
    ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
124
    adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
125
    bitmapSpinner.setAdapter(adapterBitmap);
126
    }
127

  
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

  
130
  private void setGrid()
131
    {
132
    mGridInitialized = true;
133

  
134
    mNumCols = mColsPicker.getValue();
135
    mNumRows = mRowsPicker.getValue();
136

  
137
    int width = mLay.getWidth();
138
    int height= mLay.getHeight();
139
    int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0;
140
    int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0;
141
    int size= w<h ? w:h;
142
    int pad = size<20 ? 1 : size/20;
143

  
144
    mLay.removeAllViews();
145

  
146
    mShape = new boolean[mNumRows*mNumCols];
147

  
148
    TableRow.LayoutParams p = new TableRow.LayoutParams();
149

  
150
    p.rightMargin  = pad;
151
    p.leftMargin   = pad;
152
    p.topMargin    = pad;
153
    p.bottomMargin = pad;
154
    p.height       = size;
155
    p.width        = size;
156

  
157
    for (int rows=0; rows<mNumRows; rows++)
158
      {
159
      TableRow tr = new TableRow(this);
160
      tr.setGravity(Gravity.CENTER);
161

  
162
      for(int cols=0; cols<mNumCols; cols++)
163
        {
164
        Button b = new Button(this);
165
        b.setOnClickListener(this);
166
        b.setId(rows*mNumCols+cols);
167
        b.setLayoutParams(p);
168
        b.setBackgroundColor(mObjectType==0 ? COLOR_ON:COLOR_INAC);
169
        tr.addView(b, p);
170
        mShape[rows*mNumCols+cols] = true;
171
        }
172

  
173
      mLay.addView(tr);
174
      }
175
    }
176

  
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

  
179
  public void onClick(View view)
180
    {
181
    if( mObjectType==0 )  // cubes
182
      {
183
      Button tmp = (Button)view;
184
      int id = tmp.getId();
185
      mShape[id] = !mShape[id];
186
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
187
      }
188
    }
189

  
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

  
192
  private void uncheckAll()
193
    {
194
    TableRow tr;
195
    Button butt;
196

  
197
    for (int row=0; row<mNumRows; row++)
198
      {
199
      tr = (TableRow)mLay.getChildAt(row);
200

  
201
      for(int col=0; col<mNumCols; col++)
202
        {
203
        butt = (Button)tr.getVirtualChildAt(col);
204
        butt.setBackgroundColor(mObjectType==0 ? COLOR_ON : COLOR_INAC);
205
        mShape[row*mNumCols+col] = true;
206
        }
207
      }
208
    }
209

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

  
212
  public void Create(View v)
213
    {
214
    Intent mainInt = new Intent( getApplicationContext(), EarthActivity2.class);
215
    Bundle b = new Bundle();
216

  
217
    int rows=0, cols=0;
218

  
219
    switch(mObjectType)
220
      {
221
      case 0:
222
      case 1: rows = mNumRows;
223
              cols = mNumCols;
224
              break;
225
      case 2: rows = mNumRows;   // always make the sphere equal in X and Y
226
              cols = mNumRows;   //
227
              break;
228
      case 3: rows = 1;          // a quad is always 1x1
229
              cols = 1;
230
              break;
231
      }
232

  
233
    b.putInt("type", mObjectType);
234
    b.putInt("cols", cols);
235
    b.putInt("rows", rows);
236
    b.putInt("slices", mNumSlic);
237
    b.putInt("bitmap", mBitmapID);
238

  
239
    if( mObjectType==0 )   // cubes
240
      {
241
      String str = "";
242

  
243
      for(int i=0; i<mNumRows*mNumCols; i++)
244
        str += mShape[i] ? "1" : "0";
245

  
246
      b.putString("string", str);
247
      }
248
    else
249
      {
250
      b.putString("string", "");
251
      }
252

  
253
    mainInt.putExtras(b);
254
    startActivity(mainInt);
255
    }
256

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

  
259
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
260
    {
261
    switch(parent.getId())
262
      {
263
      case R.id.objectpicker_spinnerType  : if( mObjectType!=pos )
264
                                              {
265
                                              mObjectType = pos;
266
                                              uncheckAll();
267

  
268
                                              switch(mObjectType)
269
                                                {
270
                                                case 0: mColsPicker.setEnabled(true);
271
                                                        mRowsPicker.setEnabled(true);
272
                                                        mSlicPicker.setEnabled(true);
273
                                                        break;
274
                                                case 1: mColsPicker.setEnabled(true);
275
                                                        mRowsPicker.setEnabled(true);
276
                                                        mSlicPicker.setEnabled(false);
277
                                                        break;
278
                                                case 2: mColsPicker.setEnabled(false);
279
                                                        mRowsPicker.setEnabled(true);
280
                                                        mSlicPicker.setEnabled(false);
281
                                                        break;
282
                                                case 3: mColsPicker.setEnabled(false);
283
                                                        mRowsPicker.setEnabled(false);
284
                                                        mSlicPicker.setEnabled(false);
285
                                                        break;
286
                                                }
287
                                              }
288
                                            break;
289
      case R.id.objectpicker_spinnerBitmap: switch(pos)
290
                                              {
291
                                              case 0: mBitmapID = -1            ; break;
292
                                              case 1: mBitmapID = R.raw.face    ; break;
293
                                              case 2: mBitmapID = R.raw.dog     ; break;
294
                                              case 3: mBitmapID = R.raw.cat     ; break;
295
                                              case 4: mBitmapID = R.raw.grid    ; break;
296
                                              case 5: mBitmapID = R.raw.bean    ; break;
297
                                              case 6: mBitmapID = R.raw.monalisa; break;
298
                                              case 7: mBitmapID = R.raw.world   ; break;
299
                                              }
300
                                            break;
301
      }
302
    }
303

  
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

  
306
  public void onNothingSelected(AdapterView<?> parent)
307
    {
308
    }
309

  
310
///////////////////////////////////////////////////////////////////////////////////////////////////
311
// Overrides
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313

  
314
  @Override
315
  public void onWindowFocusChanged(boolean hasFocus)
316
    {
317
    super.onWindowFocusChanged(hasFocus);
318

  
319
    mColsPicker.setValue(mNumCols);
320
    mRowsPicker.setValue(mNumRows);
321
    mSlicPicker.setValue(mNumSlic);
322

  
323
    if( !mGridInitialized ) setGrid();
324
    }
325
  }
src/main/java/org/distorted/examples/earth/EarthActivity2.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.app.Activity;
23
import android.graphics.Bitmap;
24
import android.graphics.BitmapFactory;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLSurfaceView;
28
import android.os.Bundle;
29
import android.widget.SeekBar;
30
import android.widget.TextView;
31

  
32
import org.distorted.examples.R;
33
import org.distorted.library.main.Distorted;
34
import org.distorted.library.main.DistortedTexture;
35
import org.distorted.library.mesh.MeshBase;
36
import org.distorted.library.mesh.MeshCubes;
37
import org.distorted.library.mesh.MeshFlat;
38
import org.distorted.library.mesh.MeshQuad;
39
import org.distorted.library.mesh.MeshSphere;
40

  
41
import java.io.IOException;
42
import java.io.InputStream;
43

  
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

  
46
public class EarthActivity2 extends Activity implements SeekBar.OnSeekBarChangeListener
47
{
48
    private DistortedTexture mTexture;
49
    private MeshBase mMesh;
50
    private TextView mTextLevel;
51
    private int mBitmapID;
52
    private Bitmap mBitmap;
53
    private int mNumCols;
54
    private int mNumRows;
55
    private int mNumSlic;
56

  
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
    
59
    @Override
60
    protected void onPause() 
61
      {
62
      GLSurfaceView mView = this.findViewById(R.id.earthSurfaceView);
63
      if( mView!=null ) mView.onPause();
64

  
65
      Distorted.onPause();
66
      super.onPause();
67
      }
68

  
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
    
71
    @Override
72
    protected void onResume() 
73
      {
74
      super.onResume();
75
      
76
      GLSurfaceView mView = this.findViewById(R.id.earthSurfaceView);
77
      if( mView!=null ) mView.onResume();  
78
      }
79
    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
    
82
    @Override
83
    protected void onDestroy() 
84
      {
85
      Distorted.onDestroy();  
86
      super.onDestroy();
87
      }
88
 
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

  
91
    @Override
92
    protected void onCreate(Bundle savedState)
93
      {
94
      super.onCreate(savedState);
95

  
96
      Bundle b = getIntent().getExtras();
97

  
98
      String str     = b.getString("string");
99
      int objectType = b.getInt("type");
100
      mNumCols       = b.getInt("cols");
101
      mNumRows       = b.getInt("rows");
102
      mNumSlic       = b.getInt("slices");
103
      mBitmapID      = b.getInt("bitmap");
104

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

  
117
      mTexture= new DistortedTexture(mNumCols,mNumRows);
118

  
119
      setContentView(R.layout.earthlayout);
120

  
121
      mTextLevel = findViewById(R.id.earthInflateText);
122
      SeekBar levelBar = findViewById(R.id.earthInflateLevel);
123
      levelBar.setOnSeekBarChangeListener(this);
124
      levelBar.setProgress(50);
125
      }
126

  
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

  
129
    public Bitmap getBitmap()
130
      {
131
      if( mBitmap==null )
132
        {
133
        if( mBitmapID!=-1)
134
          {
135
          InputStream is = getResources().openRawResource(mBitmapID);
136

  
137
          try
138
            {
139
            mBitmap = BitmapFactory.decodeStream(is);
140
            }
141
          finally
142
            {
143
            try
144
              {
145
              is.close();
146
              }
147
            catch(IOException e) { }
148
            }
149
          }
150
        else
151
          {
152
          final int W = 64*mNumCols;
153
          final int H = 64*mNumRows;
154

  
155
          Paint paint = new Paint();
156
          mBitmap = Bitmap.createBitmap(W,H, Bitmap.Config.ARGB_8888);
157
          Canvas canvas = new Canvas(mBitmap);
158

  
159
          paint.setAntiAlias(true);
160
          paint.setTextAlign(Paint.Align.CENTER);
161
          paint.setColor(0xff008800);
162
          paint.setStyle(Paint.Style.FILL);
163
          canvas.drawRect(0, 0, W, H, paint);
164
          paint.setColor(0xffffffff);
165

  
166
          for(int i=0; i<=mNumCols ; i++ ) canvas.drawRect(W*i/mNumCols-1, 0, W*i/mNumCols + 1, H, paint);
167
          for(int i=0; i<=mNumRows ; i++ ) canvas.drawRect( 0, H*i/mNumRows-1, W,  H*i/mNumRows+1, paint);
168
          }
169
        }
170

  
171
      return mBitmap;
172
      }
173

  
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

  
176
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
177
      {
178
      switch (bar.getId())
179
        {
180
        case R.id.earthInflateLevel: EarthSurfaceView view = this.findViewById(R.id.earthSurfaceView);
181
                                     float level = view.getRenderer().setLevel(progress);
182
                                     mTextLevel.setText(getString(R.string.inflate_placeholder, level));
183
                                     break;
184
        }
185
      }
186

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

  
189
    public void onStartTrackingTouch(SeekBar bar) { }
190

  
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

  
193
    public void onStopTrackingTouch(SeekBar bar)  { }
194

  
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

  
197
    public DistortedTexture getTexture()
198
      {
199
      return mTexture;
200
      }
201

  
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

  
204
    public MeshBase getMesh()
205
      {
206
      return mMesh;
207
      }
208
}
src/main/java/org/distorted/examples/earth/EarthRenderer.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.opengl.GLSurfaceView;
23

  
24
import org.distorted.library.effect.MatrixEffectMove;
25
import org.distorted.library.effect.MatrixEffectQuaternion;
26
import org.distorted.library.effect.MatrixEffectScale;
27
import org.distorted.library.main.Distorted;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedScreen;
30
import org.distorted.library.main.DistortedTexture;
31
import org.distorted.library.mesh.MeshBase;
32
import org.distorted.library.type.DynamicQuat;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35

  
36
import javax.microedition.khronos.egl.EGLConfig;
37
import javax.microedition.khronos.opengles.GL10;
38

  
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

  
41
class EarthRenderer implements GLSurfaceView.Renderer
42
{
43
    private static final float FOV = 30.0f;
44
    private static final float NEAR = 0.1f;
45

  
46
    private GLSurfaceView mView;
47
    private DistortedTexture mTexture;
48
    private DistortedEffects mEffects;
49
    private MeshBase mMesh;
50
    private DistortedScreen mScreen;
51
    private int mObjWidth, mObjHeight, mObjDepth;
52
    private Static3D mMove, mScale, mCenter;
53

  
54
    Static4D mQuat1, mQuat2;
55
    int mScreenMin;
56

  
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

  
59
    EarthRenderer(GLSurfaceView v)
60
      {
61
      mView = v;
62

  
63
      mMove = new Static3D(0,0,0);
64
      mScale= new Static3D(1,1,1);
65
      mCenter=new Static3D(0,0,0);
66

  
67
      EarthActivity2 act = (EarthActivity2)v.getContext();
68

  
69
      mTexture = act.getTexture();
70
      mMesh    = act.getMesh();
71

  
72
      mObjWidth = mTexture.getWidth();
73
      mObjHeight= mTexture.getHeight();
74
      mObjDepth = mTexture.getDepth(mMesh);
75

  
76
      mQuat1 = new Static4D(0,0,0,1);  // unity
77
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
78
      
79
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
80
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
81

  
82
      quatInt1.add(mQuat1);
83
      quatInt2.add(mQuat2);
84

  
85
      mEffects = new DistortedEffects();
86
      mEffects.apply( new MatrixEffectMove(mMove) );
87
      mEffects.apply( new MatrixEffectScale(mScale));
88
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) );
89
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, mCenter) );
90

  
91
      mScreen = new DistortedScreen();
92
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
93
      mScreen.setProjection(FOV, NEAR);
94
      }
95

  
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
   
98
    public void onDrawFrame(GL10 glUnused) 
99
      {
100
      mScreen.render( System.currentTimeMillis() );
101
      }
102

  
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
    
105
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
106
      {
107
      final float SCALE = 0.75f;
108

  
109
      mScreenMin = width<height ? width:height;
110
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight :  (SCALE*width)/mObjWidth;
111
      mMove.set( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0);
112
      mScale.set(factor,factor,factor);
113
      mCenter.set( (float)mObjWidth/2, (float)mObjHeight/2, (float)mObjDepth/2 );
114
      mScreen.resize(width, height);
115
      }
116

  
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

  
119
    float setLevel(int level)
120
      {
121
      float inflateLevel = (level-50)/50.0f;
122
      mMesh.setInflate(inflateLevel);
123

  
124
      return inflateLevel;
125
      }
126

  
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
130
      {
131
      EarthActivity2 act = (EarthActivity2)mView.getContext();
132

  
133
      mTexture.setTexture( act.getBitmap() );
134

  
135
      mScreen.detachAll();
136
      mScreen.attach(mTexture,mEffects,mMesh);
137

  
138
      try
139
        {
140
        Distorted.onCreate(mView.getContext());
141
        }
142
      catch(Exception ex)
143
        {
144
        android.util.Log.e("Earth", ex.getMessage() );
145
        }
146
      }
147
}
src/main/java/org/distorted/examples/earth/EarthSurfaceView.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.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
import android.widget.Toast;
29

  
30
import org.distorted.examples.R;
31

  
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33

  
34
class EarthSurfaceView extends GLSurfaceView
35
{
36
    private int mX, mY;
37
    private EarthRenderer mRenderer;
38
	
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
   
41
    public EarthSurfaceView(Context context, AttributeSet attrs)
42
      {
43
      super(context,attrs);
44
    
45
      mX = -1;
46
      mY = -1;
47
      
48
      if(!isInEditMode())
49
        {
50
        mRenderer = new EarthRenderer(this);
51
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
52
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
53
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
54
        setRenderer(mRenderer);
55
        Toast.makeText(context, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
56
        }
57
      }
58

  
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

  
61
    public EarthRenderer getRenderer()
62
      {
63
      return mRenderer;
64
      }
65

  
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
    
68
    @Override public boolean onTouchEvent(MotionEvent event) 
69
      {
70
      int action = event.getAction();
71
      int x = (int)event.getX();
72
      int y = (int)event.getY();
73
           
74
      switch(action)
75
         {
76
         case MotionEvent.ACTION_DOWN: mX = x;
77
                                       mY = y;
78
                                       break;
79
                                       
80
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
81
                                         {
82
                                         float px = mY-y;
83
                                         float py = mX-x;
84
                                         float pz = 0;
85
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
86
                                         
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff