Project

General

Profile

« Previous | Next » 

Revision 4b7c432e

Added by Leszek Koltunski almost 4 years ago

Progress (?) with Predeform: give up trying to render the Mesh in Activity1.

View differences:

src/main/java/org/distorted/examples/predeform/PredeformActivity.java
21 21

  
22 22
import android.app.Activity;
23 23
import android.content.Intent;
24
import android.graphics.Bitmap;
25
import android.graphics.BitmapFactory;
26
import android.graphics.Canvas;
27
import android.graphics.Paint;
28 24
import android.os.Bundle;
25
import android.view.Gravity;
29 26
import android.view.View;
30 27
import android.widget.AdapterView;
31 28
import android.widget.ArrayAdapter;
29
import android.widget.Button;
30
import android.widget.LinearLayout;
32 31
import android.widget.NumberPicker;
33 32
import android.widget.Spinner;
33
import android.widget.TableRow;
34 34

  
35 35
import org.distorted.examples.R;
36
import org.distorted.library.mesh.MeshBase;
37

  
38
import java.io.IOException;
39
import java.io.InputStream;
40 36

  
41 37
///////////////////////////////////////////////////////////////////////////////////////////////////
42 38

  
43
public class PredeformActivity extends Activity implements AdapterView.OnItemSelectedListener
39
public class PredeformActivity extends Activity
40
                               implements View.OnClickListener, AdapterView.OnItemSelectedListener
44 41
  {
45
  private int mNumCols;
46
  private int mNumRows;
47
  private int mNumSlic;
48
  private int mObjectType;
49
  private int mBitmapID;
50

  
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;
51 50
  private NumberPicker mColsPicker, mRowsPicker, mSlicPicker;
52 51
  private boolean[] mShape;
53
  private MeshBase mMesh;
52
  private int mObjectType;
53
  private int mBitmapID;
54
  private LinearLayout mLay;
54 55

  
55 56
///////////////////////////////////////////////////////////////////////////////////////////////////
56 57

  
......
59 60
    {
60 61
    super.onCreate(savedState);
61 62

  
62
    mNumCols = 1;
63
    mNumRows = 1;
64
    mNumSlic = 1;
65
    mBitmapID=-1;
66
    resetShape();
63
    setContentView(R.layout.objectpickerlayout);
67 64

  
68
    setContentView(R.layout.object_picker_layout);
65
    mLay = findViewById(R.id.objectpicker_buttongrid);
69 66

  
70 67
    mColsPicker = findViewById(R.id.objectpicker_cols);
71 68
    mRowsPicker = findViewById(R.id.objectpicker_rows);
......
83 80
         @Override
84 81
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
85 82
           {
86
           mNumCols = newVal;
87
           resetShape();
83
           setGrid();
88 84
           }
89 85
         });
90 86

  
......
93 89
         @Override
94 90
         public void onValueChange(NumberPicker picker, int oldVal, int newVal)
95 91
           {
96
           mNumRows = newVal;
97
           resetShape();
92
           setGrid();
98 93
           }
99 94
         });
100 95

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

  
111 105
    mObjectType = 0;
106
    mGridInitialized = false;
112 107

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

  
116 111
    String[] objectType = new String[PredeformMeshList.LENGTH];
117 112

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

  
123 118
    ArrayAdapter<String> adapterType = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectType);
......
135 130
    bitmapSpinner.setAdapter(adapterBitmap);
136 131
    }
137 132

  
138

  
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

  
141
  private void resetShape()
142
    {
143
    mShape = new boolean[mNumRows*mNumCols];
144

  
145
    for (int rows=0; rows<mNumRows; rows++)
146
      {
147
      for(int cols=0; cols<mNumCols; cols++)
148
        {
149
        mShape[rows*mNumCols+cols] = true;
150
        }
151
      }
152

  
153
    recreateObject();
154
    }
155

  
156 133
///////////////////////////////////////////////////////////////////////////////////////////////////
157 134

  
158
  MeshBase getMesh()
135
  private void setGrid()
159 136
    {
160
    return mMesh;
161
    }
137
    mGridInitialized = true;
162 138

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

  
165
  Bitmap getBitmap()
166
    {
167
    return createBitmap(mBitmapID);
168
    }
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;
169 148

  
170
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    mLay.removeAllViews();
171 150

  
172
  private void recreateObject()
173
    {
174
    String str = PredeformMeshList.getString(mObjectType, mNumCols, mNumRows, mShape);
175
    mMesh      = PredeformMeshList.createMesh(mObjectType, mNumCols, mNumRows, mNumSlic, mBitmapID, str);
176
    }
151
    mShape = new boolean[mNumRows*mNumCols];
177 152

  
178
///////////////////////////////////////////////////////////////////////////////////////////////////
153
    TableRow.LayoutParams p = new TableRow.LayoutParams();
179 154

  
180
  private Bitmap createBitmap(int bitmapID)
181
    {
182
    int size = mNumCols > mNumRows ? (Math.max(mNumCols, mNumSlic)) : (Math.max(mNumRows, mNumSlic));
155
    p.rightMargin  = pad;
156
    p.leftMargin   = pad;
157
    p.topMargin    = pad;
158
    p.bottomMargin = pad;
159
    p.height       = size;
160
    p.width        = size;
183 161

  
184
    if( bitmapID!=-1)
185
      {
186
      try (InputStream is = getResources().openRawResource(bitmapID))
187
        {
188
        return BitmapFactory.decodeStream(is);
189
        }
190
      catch( IOException ex ) { android.util.Log.e("act", "failed to open resource "+bitmapID); }
191
      }
192
    else
162
    for (int rows=0; rows<mNumRows; rows++)
193 163
      {
194
      final int T = 64;
195
      final int S = T*size;
196

  
197
      Paint paint = new Paint();
198
      Bitmap bmp = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
199
      Canvas canvas = new Canvas(bmp);
200

  
201
      paint.setAntiAlias(true);
202
      paint.setTextAlign(Paint.Align.CENTER);
203
      paint.setColor(0xff008800);
204
      paint.setStyle(Paint.Style.FILL);
205
      canvas.drawRect(0, 0, S, S, paint);
206
      paint.setColor(0xffffffff);
164
      TableRow tr = new TableRow(this);
165
      tr.setGravity(Gravity.CENTER);
207 166

  
208
      for(int i=0; i<=size ; i++ )
167
      for(int cols=0; cols<mNumCols; cols++)
209 168
        {
210
        canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
211
        canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
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;
212 176
        }
213 177

  
214
      return bmp;
178
      mLay.addView(tr);
215 179
      }
216

  
217
    return null;
218 180
    }
219 181

  
220 182
///////////////////////////////////////////////////////////////////////////////////////////////////
221 183

  
222
  void click(int row, int col)
184
  public void onClick(View view)
223 185
    {
224 186
    if( mObjectType==0 )  // cubes
225 187
      {
226
      int id = row*mNumCols+col;
188
      Button tmp = (Button)view;
189
      int id = tmp.getId();
227 190
      mShape[id] = !mShape[id];
228
      recreateObject();
191
      tmp.setBackgroundColor(mShape[id] ? COLOR_ON:COLOR_OFF);
229 192
      }
230 193
    }
231 194

  
......
233 196

  
234 197
  private void uncheckAll()
235 198
    {
236
    if( mObjectType==0 )  // cubes
199
    TableRow tr;
200
    Button butt;
201

  
202
    for (int row=0; row<mNumRows; row++)
237 203
      {
238
      for (int row=0; row<mNumRows; row++)
239
        for(int col=0; col<mNumCols; col++)
240
          mShape[row*mNumCols+col] = true;
204
      tr = (TableRow)mLay.getChildAt(row);
241 205

  
242
      recreateObject();
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
        }
243 212
      }
244 213
    }
245 214

  
......
278 247
                                              mRowsPicker.setEnabled(dim>=1);
279 248
                                              mColsPicker.setEnabled(dim>=2);
280 249
                                              mSlicPicker.setEnabled(dim>=3);
281

  
282
                                              recreateObject();
283 250
                                              }
284 251
                                            break;
285 252
      case R.id.objectpicker_spinnerBitmap: switch(pos)
......
293 260
                                              case 6: mBitmapID = R.raw.monalisa; break;
294 261
                                              case 7: mBitmapID = R.raw.world   ; break;
295 262
                                              }
296

  
297
                                            PredeformSurfaceView v = findViewById(R.id.predeformSurfaceView);
298
                                            v.getRenderer().setTexture( createBitmap(mBitmapID) );
299 263
                                            break;
300 264
      }
301 265
    }
......
319 283
    mRowsPicker.setValue(mNumRows);
320 284
    mSlicPicker.setValue(mNumSlic);
321 285

  
322
    resetShape();
286
    if( !mGridInitialized ) setGrid();
323 287
    }
324 288
  }
src/main/java/org/distorted/examples/predeform/PredeformActivity2.java
56 56
    @Override
57 57
    protected void onPause() 
58 58
      {
59
      GLSurfaceView mView = findViewById(R.id.predeform2SurfaceView);
59
      GLSurfaceView mView = findViewById(R.id.predeformSurfaceView);
60 60
      if( mView!=null ) mView.onPause();
61 61

  
62 62
      DistortedLibrary.onPause();
......
70 70
      {
71 71
      super.onResume();
72 72
      
73
      GLSurfaceView mView = findViewById(R.id.predeform2SurfaceView);
73
      GLSurfaceView mView = findViewById(R.id.predeformSurfaceView);
74 74
      if( mView!=null ) mView.onResume();  
75 75
      }
76 76
    
......
107 107

  
108 108
      setContentView(R.layout.predeformlayout);
109 109

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

  
113 113
      String[] objectBitmap = new String[] { "Render: Normal", "Render: OIT" };
......
116 116
      adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
117 117
      renderSpinner.setAdapter(adapterBitmap);
118 118

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

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

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

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

  
201
      if( parent.getId()==R.id.predeform2SpinnerMode)
201
      if( parent.getId()==R.id.predeformSpinnerMode)
202 202
        {
203 203
        renderer.setRenderModeToOIT(pos==1);
204 204
        }
......
216 216
      {
217 217
      switch (bar.getId())
218 218
        {
219
        case R.id.predeform2Transparency: PredeformSurfaceView2 v1 = findViewById(R.id.predeform2SurfaceView);
220
                                          v1.getRenderer().setTransparency(progress);
221
                                          break;
222
        case R.id.predeform2InflateLevel: PredeformSurfaceView2 v2 = findViewById(R.id.predeform2SurfaceView);
223
                                          float level = v2.getRenderer().setLevel(progress);
224
                                          mTextLevel.setText(getString(R.string.inflate_placeholder, level));
225
                                          break;
219
        case R.id.predeformTransparency: PredeformSurfaceView v1 = findViewById(R.id.predeformSurfaceView);
220
                                         v1.getRenderer().setTransparency(progress);
221
                                         break;
222
        case R.id.predeformInflateLevel: PredeformSurfaceView v2 = findViewById(R.id.predeformSurfaceView);
223
                                         float level = v2.getRenderer().setLevel(progress);
224
                                         mTextLevel.setText(getString(R.string.inflate_placeholder, level));
225
                                         break;
226 226
        }
227 227
      }
228 228

  
src/main/java/org/distorted/examples/predeform/PredeformRenderer.java
19 19

  
20 20
package org.distorted.examples.predeform;
21 21

  
22
import android.graphics.Bitmap;
23 22
import android.opengl.GLSurfaceView;
24 23

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

  
33 38
import javax.microedition.khronos.egl.EGLConfig;
34 39
import javax.microedition.khronos.opengles.GL10;
......
37 42

  
38 43
class PredeformRenderer implements GLSurfaceView.Renderer
39 44
{
40
    private static final float FOV =  0.0f;
45
    private static final float FOV = 30.0f;
41 46
    private static final float NEAR = 0.1f;
42 47

  
43 48
    private GLSurfaceView mView;
......
45 50
    private DistortedEffects mEffects;
46 51
    private MeshBase mMesh;
47 52
    private DistortedScreen mScreen;
53
    private float mObjWidth, mObjHeight, mObjDepth;
48 54
    private Static3D mScale;
55
    private Static1D mAlpha;
56

  
57
    Static4D mQuat1, mQuat2;
58
    int mScreenMin;
49 59

  
50 60
///////////////////////////////////////////////////////////////////////////////////////////////////
51 61

  
......
53 63
      {
54 64
      mView = v;
55 65

  
66
      mAlpha = new Static1D(1.0f);
56 67
      mScale= new Static3D(1,1,1);
57
      mTexture = new DistortedTexture();
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

  
58 89
      mEffects = new DistortedEffects();
90
      mEffects.apply( new VertexEffectScale(new Static3D(mObjWidth,mObjHeight,mObjDepth) ) );
59 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));
60 95

  
61 96
      mScreen = new DistortedScreen();
62 97
      mScreen.glClearColor(1.0f,1.0f,1.0f,0.0f);
......
64 99
      }
65 100

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

  
68
    void setMesh(MeshBase mesh)
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
69 111
      {
70
      mMesh = mesh;
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);
71 118
      }
72 119

  
73 120
///////////////////////////////////////////////////////////////////////////////////////////////////
74 121

  
75
    void setTexture(Bitmap bmp)
122
    void setRenderModeToOIT(boolean oit)
76 123
      {
77
      mTexture.setTexture(bmp);
124
      mScreen.setOrderIndependentTransparency(oit);
78 125
      }
79 126

  
80 127
///////////////////////////////////////////////////////////////////////////////////////////////////
81 128

  
82
    public void onDrawFrame(GL10 glUnused) 
129
    void setTransparency(int level)
83 130
      {
84
      mScreen.render( System.currentTimeMillis() );
131
      mAlpha.set((float)level/100.0f);
85 132
      }
86 133

  
87 134
///////////////////////////////////////////////////////////////////////////////////////////////////
88
    
89
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
135

  
136
    float setLevel(int level)
90 137
      {
91
      final float SCALE = 0.75f;
138
      float inflateLevel = (level-50)/50.0f;
139
      mMesh.setInflate(inflateLevel);
92 140

  
93
      float factor = ( width > height ) ? SCALE*height : SCALE*width;
94
      mScale.set(factor,factor,factor);
95
      mScreen.resize(width, height);
141
      return inflateLevel;
96 142
      }
97 143

  
98 144
///////////////////////////////////////////////////////////////////////////////////////////////////
99 145
    
100 146
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
101 147
      {
102
      PredeformActivity act = (PredeformActivity)mView.getContext();
148
      PredeformActivity2 act = (PredeformActivity2)mView.getContext();
103 149

  
104
      mMesh = act.getMesh();
105 150
      mTexture.setTexture( act.getBitmap() );
106 151
      mScreen.detachAll();
107 152
      mScreen.attach(mTexture,mEffects,mMesh);
108 153

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

  
109 157
      try
110 158
        {
111 159
        DistortedLibrary.onCreate(act);
src/main/java/org/distorted/examples/predeform/PredeformRenderer2.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 PredeformRenderer2 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
    PredeformRenderer2(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
24 24
import android.content.pm.ConfigurationInfo;
25 25
import android.opengl.GLSurfaceView;
26 26
import android.util.AttributeSet;
27
import android.view.MotionEvent;
28
import android.widget.Toast;
29

  
30
import org.distorted.examples.R;
27 31

  
28 32
///////////////////////////////////////////////////////////////////////////////////////////////////
29 33

  
30 34
class PredeformSurfaceView extends GLSurfaceView
31 35
{
36
    private final static int DIRECTION_SENSITIVITY=  12;
37
    private int mX, mY;
32 38
    private PredeformRenderer mRenderer;
33 39

  
34 40
///////////////////////////////////////////////////////////////////////////////////////////////////
......
37 43
      {
38 44
      super(context,attrs);
39 45

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

  
40 49
      if(!isInEditMode())
41 50
        {
42 51
        mRenderer = new PredeformRenderer(this);
......
44 53
        final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
45 54
        setEGLContextClientVersion( (configurationInfo.reqGlEsVersion>>16) >= 3 ? 3:2 );
46 55
        setRenderer(mRenderer);
56
        Toast.makeText(context, R.string.example_rotate_toast , Toast.LENGTH_SHORT).show();
47 57
        }
48 58
      }
49 59

  
......
53 63
      {
54 64
      return mRenderer;
55 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
         
56 140
}
57 141

  
src/main/java/org/distorted/examples/predeform/PredeformSurfaceView2.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 PredeformSurfaceView2 extends GLSurfaceView
35
{
36
    private final static int DIRECTION_SENSITIVITY=  12;
37
    private int mX, mY;
38
    private PredeformRenderer2 mRenderer;
39

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

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

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

  
49
      if(!isInEditMode())
50
        {
51
        mRenderer = new PredeformRenderer2(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 PredeformRenderer2 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/object_picker_layout.xml
1
<?xml version="1.0" encoding="utf-8"?>
2

  
3
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
4
     android:orientation="vertical"
5
     android:layout_width="match_parent"
6
     android:layout_height="match_parent">
7

  
8
     <LinearLayout
9
           android:orientation="horizontal"
10
           android:layout_width="match_parent"
11
           android:layout_height="wrap_content">
12

  
13
           <Spinner
14
                android:layout_width="0dp"
15
                android:layout_height="50dp"
16
                android:layout_weight="0.5"
17
                android:id="@+id/objectpicker_spinnerType"
18
           />
19

  
20
           <Spinner
21
                   android:layout_width="0dp"
22
                   android:layout_height="50dp"
23
                   android:layout_weight="0.5"
24
                   android:id="@+id/objectpicker_spinnerBitmap"
25
           />
26
     </LinearLayout>
27

  
28
     <LinearLayout
29
           android:orientation="horizontal"
30
           android:layout_width="match_parent"
31
           android:layout_height="wrap_content">
32

  
33
           <NumberPicker
34
               android:id="@+id/objectpicker_rows"
35
               android:layout_width="wrap_content"
36
               android:layout_height="wrap_content"
37
               android:orientation="vertical"
38
               android:descendantFocusability="blocksDescendants"
39
           />
40

  
41
           <NumberPicker
42
               android:id="@+id/objectpicker_cols"
43
               android:layout_width="wrap_content"
44
               android:layout_height="wrap_content"
45
               android:orientation="vertical"
46
               android:descendantFocusability="blocksDescendants"
47
           />
48

  
49
           <NumberPicker
50
               android:id="@+id/objectpicker_slices"
51
               android:layout_width="wrap_content"
52
               android:layout_height="wrap_content"
53
               android:orientation="vertical"
54
               android:descendantFocusability="blocksDescendants"
55
           />
56

  
57
           <Button
58
               android:id="@+id/objectpicker_create"
59
               android:onClick="Create"
60
               android:text="@string/Create"
61
               android:layout_width="match_parent"
62
               android:layout_height="fill_parent"
63
           />
64
       </LinearLayout>
65

  
66
       <org.distorted.examples.predeform.PredeformSurfaceView
67
           android:id="@+id/predeformSurfaceView"
68
           android:layout_width="fill_parent"
69
           android:layout_height="0dp"
70
           android:layout_weight="0.80" />
71

  
72
</LinearLayout>
src/main/res/layout/predeformlayout.xml
5 5
    android:gravity="center_horizontal"
6 6
    android:orientation="vertical" >
7 7

  
8
    <org.distorted.examples.predeform.PredeformSurfaceView2
9
        android:id="@+id/predeform2SurfaceView"
8
    <org.distorted.examples.predeform.PredeformSurfaceView
9
        android:id="@+id/predeformSurfaceView"
10 10
        android:layout_width="match_parent"
11 11
        android:layout_height="0dp"
12 12
        android:layout_weight="1.00" />
......
20 20
        android:background="@color/blue">
21 21

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

  
30 30
        <SeekBar
31
            android:id="@+id/predeform2InflateLevel"
31
            android:id="@+id/predeformInflateLevel"
32 32
            android:layout_weight="0.7"
33 33
            android:layout_width="0dp"
34 34
            android:layout_height="50dp"
......
46 46
        android:background="@color/blue">
47 47

  
48 48
        <SeekBar
49
            android:id="@+id/predeform2Transparency"
49
            android:id="@+id/predeformTransparency"
50 50
            android:layout_weight="0.50"
51 51
            android:layout_width="0dp"
52 52
            android:layout_height="50dp"
......
57 57
            android:layout_width="0dp"
58 58
            android:layout_height="50dp"
59 59
            android:layout_weight="0.5"
60
            android:id="@+id/predeform2SpinnerMode"
60
            android:id="@+id/predeformSpinnerMode"
61 61
            />
62 62
    </LinearLayout>
63 63

  

Also available in: Unified diff