Project

General

Profile

« Previous | Next » 

Revision 59835a0a

Added by Leszek Koltunski over 4 years ago

Beginnings of a new 'Predeform' app which tests pre-applying a queue of Vertex Effects to a (single?) Mesh.
So far just a copy of Inflate app.

View differences:

src/main/AndroidManifest.xml
57 57
        <activity android:name=".earth.EarthActivity"/>
58 58
        <activity android:name=".rubik.RubikActivity"/>
59 59
        <activity android:name=".meshjoin.MeshJoinActivity"/>
60
        <activity android:name=".predeform.PredeformActivity"/>
61
        <activity android:name=".predeform.PredeformActivity2"/>
60 62
    </application>
61 63
</manifest>
src/main/java/org/distorted/examples/TableOfContents.java
71 71
import org.distorted.examples.earth.EarthActivity;
72 72
import org.distorted.examples.rubik.RubikActivity;
73 73
import org.distorted.examples.meshjoin.MeshJoinActivity;
74
import org.distorted.examples.predeform.PredeformActivity;
74 75

  
75 76
///////////////////////////////////////////////////////////////////////////////////////////////////
76 77

  
......
119 120
    EARTH             (R.drawable.icon_example_earth           , R.string.example_earth           , R.string.example_earth_subtitle           ,            EarthActivity.class),
120 121
    RUBIK             (R.drawable.icon_example_rubik           , R.string.example_rubik           , R.string.example_rubik_subtitle           ,            RubikActivity.class),
121 122
    MESHJOIN          (R.drawable.icon_example_meshjoin        , R.string.example_meshjoin           , R.string.example_meshjoin_subtitle           ,            MeshJoinActivity.class),
123
    PREDEFORM         (R.drawable.icon_example_wip             , R.string.example_predeform           , R.string.example_predeform_subtitle           ,            PredeformActivity.class),
122 124
    ;
123 125

  
124 126
    final int icon, title, subtitle;
src/main/java/org/distorted/examples/predeform/PredeformActivity.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.predeform;
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 PredeformActivity 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[PredeformMeshList.LENGTH];
112

  
113
    for(int mesh = 0; mesh< PredeformMeshList.LENGTH; mesh++)
114
      {
115
      objectType[mesh] = "Mesh: "+ PredeformMeshList.getName(mesh);
116
      }
117

  
118
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
119
    adapterType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
120
    typeSpinner.setAdapter(adapterType);
121

  
122
    Spinner bitmapSpinner  = findViewById(R.id.objectpicker_spinnerBitmap);
123
    bitmapSpinner.setOnItemSelectedListener(this);
124

  
125
    String[] objectBitmap = new String[] { "Texture: Grid", "Texture: Girl", "Texture: Dog", "Texture: Cat",
126
                                           "Texture: Squares", "Texture: Bean", "Texture: Lisa", "Texture: World" };
127

  
128
    ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
129
    adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
130
    bitmapSpinner.setAdapter(adapterBitmap);
131
    }
132

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

  
135
  private void setGrid()
136
    {
137
    mGridInitialized = true;
138

  
139
    mNumCols = mColsPicker.getValue();
140
    mNumRows = mRowsPicker.getValue();
141

  
142
    int width = mLay.getWidth();
143
    int height= mLay.getHeight();
144
    int w = mNumCols>0 ? (int)( 0.9f*width / mNumCols) : 0;
145
    int h = mNumRows>0 ? (int)( 0.9f*height/ mNumRows) : 0;
146
    int size= Math.min(w, h);
147
    int pad = size<20 ? 1 : size/20;
148

  
149
    mLay.removeAllViews();
150

  
151
    mShape = new boolean[mNumRows*mNumCols];
152

  
153
    TableRow.LayoutParams p = new TableRow.LayoutParams();
154

  
155
    p.rightMargin  = pad;
156
    p.leftMargin   = pad;
157
    p.topMargin    = pad;
158
    p.bottomMargin = pad;
159
    p.height       = size;
160
    p.width        = size;
161

  
162
    for (int rows=0; rows<mNumRows; rows++)
163
      {
164
      TableRow tr = new TableRow(this);
165
      tr.setGravity(Gravity.CENTER);
166

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

  
178
      mLay.addView(tr);
179
      }
180
    }
181

  
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

  
184
  public void onClick(View view)
185
    {
186
    if( mObjectType==0 )  // cubes
187
      {
188
      Button tmp = (Button)view;
189
      int id = tmp.getId();
190
      mShape[id] = !mShape[id];
191
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
192
      }
193
    }
194

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

  
197
  private void uncheckAll()
198
    {
199
    TableRow tr;
200
    Button butt;
201

  
202
    for (int row=0; row<mNumRows; row++)
203
      {
204
      tr = (TableRow)mLay.getChildAt(row);
205

  
206
      for(int col=0; col<mNumCols; col++)
207
        {
208
        butt = (Button)tr.getVirtualChildAt(col);
209
        butt.setBackgroundColor(mObjectType==0 ? COLOR_ON : COLOR_INAC);
210
        mShape[row*mNumCols+col] = true;
211
        }
212
      }
213
    }
214

  
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

  
217
  public void Create(View v)
218
    {
219
    Intent mainInt = new Intent( getApplicationContext(), PredeformActivity2.class);
220
    Bundle b = new Bundle();
221

  
222
    b.putInt("type"  , mObjectType);
223
    b.putInt("cols"  , PredeformMeshList.getCols(mObjectType, mNumCols, mNumRows, mNumSlic) );
224
    b.putInt("rows"  , PredeformMeshList.getRows(mObjectType, mNumCols, mNumRows, mNumSlic) );
225
    b.putInt("slices", PredeformMeshList.getSlic(mObjectType, mNumCols, mNumRows, mNumSlic) );
226
    b.putInt("bitmap", mBitmapID);
227

  
228
    b.putString("string", PredeformMeshList.getString(mObjectType, mNumCols, mNumRows, mShape));
229

  
230
    mainInt.putExtras(b);
231
    startActivity(mainInt);
232
    }
233

  
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

  
236
  public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
237
    {
238
    switch(parent.getId())
239
      {
240
      case R.id.objectpicker_spinnerType  : if( mObjectType!=pos )
241
                                              {
242
                                              mObjectType = pos;
243
                                              uncheckAll();
244

  
245
                                              int dim = PredeformMeshList.getDimension(mObjectType);
246

  
247
                                              mRowsPicker.setEnabled(dim>=1);
248
                                              mColsPicker.setEnabled(dim>=2);
249
                                              mSlicPicker.setEnabled(dim>=3);
250
                                              }
251
                                            break;
252
      case R.id.objectpicker_spinnerBitmap: switch(pos)
253
                                              {
254
                                              case 0: mBitmapID = -1            ; break;
255
                                              case 1: mBitmapID = R.raw.face    ; break;
256
                                              case 2: mBitmapID = R.raw.dog     ; break;
257
                                              case 3: mBitmapID = R.raw.cat     ; break;
258
                                              case 4: mBitmapID = R.raw.grid    ; break;
259
                                              case 5: mBitmapID = R.raw.bean    ; break;
260
                                              case 6: mBitmapID = R.raw.monalisa; break;
261
                                              case 7: mBitmapID = R.raw.world   ; break;
262
                                              }
263
                                            break;
264
      }
265
    }
266

  
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

  
269
  public void onNothingSelected(AdapterView<?> parent)
270
    {
271
    }
272

  
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
// Overrides
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

  
277
  @Override
278
  public void onWindowFocusChanged(boolean hasFocus)
279
    {
280
    super.onWindowFocusChanged(hasFocus);
281

  
282
    mColsPicker.setValue(mNumCols);
283
    mRowsPicker.setValue(mNumRows);
284
    mSlicPicker.setValue(mNumSlic);
285

  
286
    if( !mGridInitialized ) setGrid();
287
    }
288
  }
src/main/java/org/distorted/examples/predeform/PredeformActivity2.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.predeform;
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.view.View;
30
import android.widget.AdapterView;
31
import android.widget.ArrayAdapter;
32
import android.widget.SeekBar;
33
import android.widget.Spinner;
34
import android.widget.TextView;
35

  
36
import org.distorted.examples.R;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.mesh.MeshBase;
40

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

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

  
46
public class PredeformActivity2 extends Activity implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
47
{
48
    private Bitmap mBitmap;
49
    private TextView mTextLevel;
50
    private DistortedTexture mTexture;
51
    private MeshBase mMesh;
52
    private int mNumRows, mNumCols, mNumSlic;
53

  
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
    
56
    @Override
57
    protected void onPause() 
58
      {
59
      GLSurfaceView mView = this.findViewById(R.id.predeformSurfaceView);
60
      if( mView!=null ) mView.onPause();
61

  
62
      DistortedLibrary.onPause();
63
      super.onPause();
64
      }
65

  
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
    
68
    @Override
69
    protected void onResume() 
70
      {
71
      super.onResume();
72
      
73
      GLSurfaceView mView = this.findViewById(R.id.predeformSurfaceView);
74
      if( mView!=null ) mView.onResume();  
75
      }
76
    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
    
79
    @Override
80
    protected void onDestroy() 
81
      {
82
      DistortedLibrary.onDestroy();
83
      super.onDestroy();
84
      }
85

  
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

  
88
    @Override
89
    protected void onCreate(Bundle savedState)
90
      {
91
      super.onCreate(savedState);
92

  
93
      Bundle b = getIntent().getExtras();
94

  
95
      String str     = b.getString("string");
96
      int objectType = b.getInt("type");
97
      int bitmapID   = b.getInt("bitmap");
98
      mNumCols       = b.getInt("cols");
99
      mNumRows       = b.getInt("rows");
100
      mNumSlic       = b.getInt("slices");
101

  
102
      int maxsize = mNumCols > mNumRows ? (Math.max(mNumCols, mNumSlic)) : (Math.max(mNumRows, mNumSlic));
103
      createBitmap(maxsize,bitmapID);
104
      mMesh =  PredeformMeshList.createMesh(objectType, mNumCols, mNumRows, mNumSlic, bitmapID, str);
105

  
106
      mTexture = new DistortedTexture();
107

  
108
      setContentView(R.layout.predeformlayout);
109

  
110
      Spinner renderSpinner = findViewById(R.id.predeformSpinnerMode);
111
      renderSpinner.setOnItemSelectedListener(this);
112

  
113
      String[] objectBitmap = new String[] { "Render: Normal", "Render: OIT" };
114

  
115
      ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
116
      adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
117
      renderSpinner.setAdapter(adapterBitmap);
118

  
119
      mTextLevel = findViewById(R.id.predeformInflateText);
120

  
121
      SeekBar transparencyBar = findViewById(R.id.predeformTransparency);
122
      transparencyBar.setOnSeekBarChangeListener(this);
123
      transparencyBar.setProgress(50);
124

  
125
      SeekBar inflateBar = findViewById(R.id.predeformInflateLevel);
126
      inflateBar.setOnSeekBarChangeListener(this);
127
      inflateBar.setProgress(50);
128
      }
129

  
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

  
132
    private void createBitmap(int size, int bitmapID)
133
      {
134
      if( bitmapID!=-1)
135
        {
136
        try (InputStream is = getResources().openRawResource(bitmapID))
137
          {
138
          mBitmap = BitmapFactory.decodeStream(is);
139
          }
140
        catch( IOException ex ) { android.util.Log.e("act", "failed to open resource "+bitmapID); }
141
        }
142
      else
143
        {
144
        final int T = 64;
145
        final int S = T*size;
146

  
147
        Paint paint = new Paint();
148
        mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
149
        Canvas canvas = new Canvas(mBitmap);
150

  
151
        paint.setAntiAlias(true);
152
        paint.setTextAlign(Paint.Align.CENTER);
153
        paint.setColor(0xff008800);
154
        paint.setStyle(Paint.Style.FILL);
155
        canvas.drawRect(0, 0, S, S, paint);
156
        paint.setColor(0xffffffff);
157

  
158
        for(int i=0; i<=size ; i++ )
159
          {
160
          canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
161
          canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
162
          }
163
        }
164
      }
165

  
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

  
168
    int getRows()
169
      {
170
      return mNumRows;
171
      }
172

  
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

  
175
    int getCols()
176
      {
177
      return mNumCols;
178
      }
179

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

  
182
    int getSlic()
183
      {
184
      return mNumSlic;
185
      }
186

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

  
189
    public Bitmap getBitmap()
190
      {
191
      return mBitmap;
192
      }
193

  
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

  
196
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
197
      {
198
      PredeformSurfaceView v = this.findViewById(R.id.predeformSurfaceView);
199
      PredeformRenderer renderer = v.getRenderer();
200

  
201
      if( parent.getId()==R.id.predeformSpinnerMode)
202
        {
203
        renderer.setRenderModeToOIT(pos==1);
204
        }
205
      }
206

  
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

  
209
    public void onNothingSelected(AdapterView<?> parent)
210
      {
211
      }
212

  
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

  
215
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
216
      {
217
      switch (bar.getId())
218
        {
219
        case R.id.predeformTransparency: PredeformSurfaceView v1 = this.findViewById(R.id.predeformSurfaceView);
220
                                         v1.getRenderer().setTransparency(progress);
221
                                         break;
222
        case R.id.predeformInflateLevel: PredeformSurfaceView v2 = this.findViewById(R.id.predeformSurfaceView);
223
                                         float level = v2.getRenderer().setLevel(progress);
224
                                         mTextLevel.setText(getString(R.string.inflate_placeholder, level));
225
                                         break;
226
        }
227
      }
228

  
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

  
231
    public void onStartTrackingTouch(SeekBar bar) { }
232

  
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

  
235
    public void onStopTrackingTouch(SeekBar bar)  { }
236

  
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

  
239
    public DistortedTexture getTexture()
240
      {
241
      return mTexture;
242
      }
243

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

  
246
    public MeshBase getMesh()
247
      {
248
      return mMesh;
249
      }
250
}
src/main/java/org/distorted/examples/predeform/PredeformMeshList.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.predeform;
21

  
22
import org.distorted.library.mesh.MeshBase;
23
import org.distorted.library.mesh.MeshCubes;
24
import org.distorted.library.mesh.MeshQuad;
25
import org.distorted.library.mesh.MeshRectangles;
26
import org.distorted.library.mesh.MeshSphere;
27
import org.distorted.library.mesh.MeshTriangles;
28
import org.distorted.library.type.Static4D;
29

  
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

  
32
public enum PredeformMeshList
33
  {
34
  Cubes      ( 3 ),
35
  Rectangles ( 2 ),
36
  Sphere     ( 1 ),
37
  Quad       ( 0 ),
38
  Triangles  ( 1 ),
39
  ;
40

  
41
  static final int LENGTH = values().length;
42
  private static final PredeformMeshList[] meshes;
43
  private int mDimension;
44

  
45
  static
46
    {
47
    int i=0;
48
    meshes = new PredeformMeshList[LENGTH];
49

  
50
    for(PredeformMeshList mesh: PredeformMeshList.values())
51
      {
52
      meshes[i++] = mesh;
53
      }
54
    }
55

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

  
58
  PredeformMeshList(int dim )
59
    {
60
    mDimension =  dim;
61
    }
62

  
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

  
65
  static String getName(int ordinal)
66
    {
67
    return meshes[ordinal].name();
68
    }
69

  
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

  
72
  static int getDimension(int ordinal)
73
    {
74
    return  meshes[ordinal].mDimension;
75
    }
76

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

  
79
  static int getRows(int ordinal, int cols, int rows, int slic)
80
    {
81
    switch(ordinal)
82
      {
83
      case 0: return rows;  // cubes
84
      case 1: return rows;  // rectangles
85
      case 2: return rows;  // sphere
86
      case 3: return 1;     // quad
87
      case 4: return rows;  // triangles
88
      }
89

  
90
    return 0;
91
    }
92

  
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

  
95
  static int getCols(int ordinal, int cols, int rows, int slic)
96
    {
97
    switch(ordinal)
98
      {
99
      case 0: return cols;  // cubes
100
      case 1: return cols;  // rectangles
101
      case 2: return rows;  // sphere
102
      case 3: return 1;     // quad
103
      case 4: return rows;  // triangles
104
      }
105

  
106
    return 0;
107
    }
108

  
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

  
111
  static int getSlic(int ordinal, int cols, int rows, int slic)
112
    {
113
    switch(ordinal)
114
      {
115
      case 0: return slic;  // cubes
116
      case 1: return 0;     // rectangles
117
      case 2: return rows;  // sphere
118
      case 3: return 0;     // quad
119
      case 4: return 0;     // triangles
120
      }
121

  
122
    return 0;
123
    }
124

  
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

  
127
  static String getString(int ordinal, int cols, int rows, boolean[] shape)
128
    {
129
    if( ordinal==0 )
130
      {
131
      String str = "";
132

  
133
      for(int i=0; i<rows*cols; i++)
134
        str += shape[i] ? "1" : "0";
135

  
136
      return str;
137
      }
138

  
139
    return "";
140
    }
141

  
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

  
144
  static MeshBase createMesh(int ordinal, int cols, int rows, int slic, int bitmapID, String str)
145
    {
146
    MeshBase mesh;
147

  
148
    int maxsize = cols > rows ? (Math.max(cols, slic)) : (Math.max(rows, slic)) ;
149

  
150
    switch( meshes[ordinal] )
151
      {
152
      case Cubes     : if( bitmapID!=-1 )
153
                         {
154
                         mesh = new MeshCubes(cols, str, slic);
155
                         }
156
                       else
157
                         {
158
                         Static4D mapFB = new Static4D(0.0f,0.0f, (float)cols/maxsize, (float)rows/maxsize);
159
                         Static4D mapLR = new Static4D(0.0f,0.0f, (float)slic/maxsize, (float)rows/maxsize);
160
                         Static4D mapTB = new Static4D(0.0f,0.0f, (float)cols/maxsize, (float)slic/maxsize);
161

  
162
                         mesh = new MeshCubes(cols, str, slic, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
163
                         }
164
                       break;
165
      case Rectangles: mesh = new MeshRectangles(cols,rows);
166
                       break;
167
      case Sphere    : mesh = new MeshSphere(rows);
168
                       break;
169
      case Quad      : mesh = new MeshQuad();
170
                       break;
171
      case Triangles : mesh = new MeshTriangles(rows);
172
                       break;
173
      default:         mesh = null;
174
                       android.util.Log.e("Meshes", "Error: unimplemented Mesh!");
175
      }
176

  
177
    return mesh;
178
    }
179
  }
src/main/java/org/distorted/examples/predeform/PredeformRenderer.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.predeform;
21

  
22
import android.opengl.GLSurfaceView;
23

  
24
import org.distorted.library.effect.FragmentEffectAlpha;
25
import org.distorted.library.effect.MatrixEffectQuaternion;
26
import org.distorted.library.effect.MatrixEffectScale;
27
import org.distorted.library.effect.VertexEffectScale;
28
import org.distorted.library.main.DistortedEffects;
29
import org.distorted.library.main.DistortedLibrary;
30
import org.distorted.library.main.DistortedScreen;
31
import org.distorted.library.main.DistortedTexture;
32
import org.distorted.library.mesh.MeshBase;
33
import org.distorted.library.type.DynamicQuat;
34
import org.distorted.library.type.Static1D;
35
import org.distorted.library.type.Static3D;
36
import org.distorted.library.type.Static4D;
37

  
38
import javax.microedition.khronos.egl.EGLConfig;
39
import javax.microedition.khronos.opengles.GL10;
40

  
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

  
43
class PredeformRenderer implements GLSurfaceView.Renderer
44
{
45
    private static final float FOV = 30.0f;
46
    private static final float NEAR = 0.1f;
47

  
48
    private GLSurfaceView mView;
49
    private DistortedTexture mTexture;
50
    private DistortedEffects mEffects;
51
    private MeshBase mMesh;
52
    private DistortedScreen mScreen;
53
    private float mObjWidth, mObjHeight, mObjDepth;
54
    private Static3D mScale;
55
    private Static1D mAlpha;
56

  
57
    Static4D mQuat1, mQuat2;
58
    int mScreenMin;
59

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

  
62
    PredeformRenderer(GLSurfaceView v)
63
      {
64
      mView = v;
65

  
66
      mAlpha = new Static1D(1.0f);
67
      mScale= new Static3D(1,1,1);
68

  
69
      Static3D center=new Static3D(0,0,0);
70

  
71
      PredeformActivity2 act = (PredeformActivity2)v.getContext();
72

  
73
      mTexture = act.getTexture();
74
      mMesh    = act.getMesh();
75

  
76
      mObjWidth = act.getCols();
77
      mObjHeight= act.getRows();
78
      mObjDepth = act.getSlic();
79

  
80
      mQuat1 = new Static4D(0,0,0,1);  // unity
81
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
82
      
83
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
84
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
85

  
86
      quatInt1.add(mQuat1);
87
      quatInt2.add(mQuat2);
88

  
89
      mEffects = new DistortedEffects();
90
      mEffects.apply( new VertexEffectScale(new Static3D(mObjWidth,mObjHeight,mObjDepth) ) );
91
      mEffects.apply( new MatrixEffectScale(mScale));
92
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
93
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
94
      mEffects.apply( new FragmentEffectAlpha(mAlpha));
95

  
96
      mScreen = new DistortedScreen();
97
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
98
      mScreen.setProjection(FOV, NEAR);
99
      }
100

  
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
   
103
    public void onDrawFrame(GL10 glUnused) 
104
      {
105
      mScreen.render( System.currentTimeMillis() );
106
      }
107

  
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
111
      {
112
      final float SCALE = 0.75f;
113

  
114
      mScreenMin = Math.min(width, height);
115
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (SCALE*height)/mObjHeight :  (SCALE*width)/mObjWidth;
116
      mScale.set(factor,factor,factor);
117
      mScreen.resize(width, height);
118
      }
119

  
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

  
122
    void setRenderModeToOIT(boolean oit)
123
      {
124
      mScreen.setOrderIndependentTransparency(oit);
125
      }
126

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

  
129
    void setTransparency(int level)
130
      {
131
      mAlpha.set((float)level/100.0f);
132
      }
133

  
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

  
136
    float setLevel(int level)
137
      {
138
      float inflateLevel = (level-50)/50.0f;
139
      mMesh.setInflate(inflateLevel);
140

  
141
      return inflateLevel;
142
      }
143

  
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
    
146
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
147
      {
148
      PredeformActivity2 act = (PredeformActivity2)mView.getContext();
149

  
150
      mTexture.setTexture( act.getBitmap() );
151
      mScreen.detachAll();
152
      mScreen.attach(mTexture,mEffects,mMesh);
153

  
154
      VertexEffectScale.enable();
155
      FragmentEffectAlpha.enable();
156

  
157
      try
158
        {
159
        DistortedLibrary.onCreate(act);
160
        }
161
      catch(Exception ex)
162
        {
163
        android.util.Log.e("Inflate", ex.getMessage() );
164
        }
165
      }
166
}
src/main/java/org/distorted/examples/predeform/PredeformSurfaceView.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.predeform;
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 PredeformSurfaceView extends GLSurfaceView
35
{
36
    private final static int DIRECTION_SENSITIVITY=  12;
37
    private int mX, mY;
38
    private PredeformRenderer mRenderer;
39

  
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

  
42
    public PredeformSurfaceView(Context context, AttributeSet attrs)
43
      {
44
      super(context,attrs);
45

  
46
      mX = -1;
47
      mY = -1;
48

  
49
      if(!isInEditMode())
50
        {
51
        mRenderer = new PredeformRenderer(this);
52
        final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
53
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
54
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
55
        setRenderer(mRenderer);
56
        Toast.makeText(context, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
57
        }
58
      }
59

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

  
62
    public PredeformRenderer getRenderer()
63
      {
64
      return mRenderer;
65
      }
66

  
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

  
69
    private void resetQuats()
70
      {
71
      float qx = mRenderer.mQuat1.get0();
72
      float qy = mRenderer.mQuat1.get1();
73
      float qz = mRenderer.mQuat1.get2();
74
      float qw = mRenderer.mQuat1.get3();
75

  
76
      float rx = mRenderer.mQuat2.get0();
77
      float ry = mRenderer.mQuat2.get1();
78
      float rz = mRenderer.mQuat2.get2();
79
      float rw = mRenderer.mQuat2.get3();
80

  
81
      float tx = rw*qx - rz*qy + ry*qz + rx*qw;
82
      float ty = rw*qy + rz*qx + ry*qw - rx*qz;
83
      float tz = rw*qz + rz*qw - ry*qx + rx*qy;
84
      float tw = rw*qw - rz*qz - ry*qy - rx*qx;
85

  
86
      mRenderer.mQuat1.set(0f, 0f, 0f, 1f);
87
      mRenderer.mQuat2.set(tx, ty, tz, tw);
88
      }
89

  
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
    
92
    @Override public boolean onTouchEvent(MotionEvent event) 
93
      {
94
      int action = event.getAction();
95
      int x = (int)event.getX();
96
      int y = (int)event.getY();
97
           
98
      switch(action)
99
         {
100
         case MotionEvent.ACTION_DOWN: mX = x;
101
                                       mY = y;
102
                                       break;
103
                                       
104
         case MotionEvent.ACTION_MOVE: if( mX>=0 && mY>= 0 )
105
                                         {
106
                                         float px = mY-y;
107
                                         float py = mX-x;
108
                                         float pz = 0;
109
                                         float plen = (float)Math.sqrt(px*px + py*py + pz*pz);
110
                                         
111
                                         if( plen>0 )
112
                                           {
113
                                           px /= plen;
114
                                           py /= plen;
115
                                           pz /= plen;
116

  
117
                                           float cosA = (float)Math.cos(plen*3.14f/mRenderer.mScreenMin);
118
                                           float sinA = (float)Math.sqrt(1-cosA*cosA);
119
                                         
120
                                           mRenderer.mQuat1.set(px*sinA, py*sinA, pz*sinA, cosA);
121
                                           }
122
                                         }
123
                                       if( (mX-x)*(mX-x) + (mY-y)*(mY-y) > mRenderer.mScreenMin*mRenderer.mScreenMin/(DIRECTION_SENSITIVITY*DIRECTION_SENSITIVITY) )
124
                                         {
125
                                         mX = x;
126
                                         mY = y;
127
                                         resetQuats();
128
                                         }
129
                                       break;
130
                                       
131
         case MotionEvent.ACTION_UP  : mX = -1;
132
                                       mY = -1;
133
        	                             resetQuats();
134
                                       break;
135
         }
136
             
137
      return true;
138
      }
139
         
140
}
141

  
src/main/res/layout/predeformlayout.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
    android:layout_width="fill_parent"
4
    android:layout_height="fill_parent"
5
    android:gravity="center_horizontal"
6
    android:orientation="vertical" >
7

  
8
    <org.distorted.examples.predeform.PredeformSurfaceView
9
        android:id="@+id/predeformSurfaceView"
10
        android:layout_width="match_parent"
11
        android:layout_height="0dp"
12
        android:layout_weight="1.00" />
13

  
14
    <LinearLayout
15
        android:id="@+id/linearLayout"
16
        android:layout_width="fill_parent"
17
        android:layout_height="wrap_content"
18
        android:gravity="center|fill_horizontal"
19
        android:orientation="horizontal"
20
        android:background="@color/blue">
21

  
22
        <TextView
23
            android:id="@+id/predeformInflateText"
24
            android:layout_width="0dp"
25
            android:layout_height="wrap_content"
26
            android:layout_weight="0.3"
27
            android:paddingLeft="10dp"
28
            android:text="@string/inflate" />
29

  
30
        <SeekBar
31
            android:id="@+id/predeformInflateLevel"
32
            android:layout_weight="0.7"
33
            android:layout_width="0dp"
34
            android:layout_height="50dp"
35
            android:paddingLeft="10dp"
36
            android:paddingRight="5dp" />
37

  
38
    </LinearLayout>
39

  
40
    <LinearLayout
41
        android:id="@+id/linearLayout3"
42
        android:layout_width="fill_parent"
43
        android:layout_height="wrap_content"
44
        android:gravity="center|fill_horizontal"
45
        android:orientation="horizontal"
46
        android:background="@color/blue">
47

  
48
        <SeekBar
49
            android:id="@+id/predeformTransparency"
50
            android:layout_weight="0.50"
51
            android:layout_width="0dp"
52
            android:layout_height="50dp"
53
            android:paddingLeft="10dp"
54
            android:paddingRight="10dp" />
55

  
56
        <Spinner
57
            android:layout_width="0dp"
58
            android:layout_height="50dp"
59
            android:layout_weight="0.5"
60
            android:id="@+id/predeformSpinnerMode"
61
            />
62
    </LinearLayout>
63

  
64
</LinearLayout>
src/main/res/values/strings.xml
195 195
    <string name="example_rubik_subtitle">Keep an eye on memory consumption while allocating and deallocating Nodes.</string>
196 196
    <string name="example_meshjoin">Joining Meshes</string>
197 197
    <string name="example_meshjoin_subtitle">See how one can join several simple meshes to form a single, more advanced one.</string>
198
    <string name="example_predeform">PreApply Vertex Effects to a Mesh</string>
199
    <string name="example_predeform_subtitle">Create a more advanced Mesh by taking one of the simple ones and pre-applying a queue of Vertex Effects to it.</string>
198 200

  
199 201
    <string name="example_movingeffects_toast">Click on \'RESET\' and define your path by touching the screen. Then click on one of the effects and see it move along your path.</string>
200 202
    <string name="example_rotate_toast">Rotate the scene by swiping the screen</string>

Also available in: Unified diff