Project

General

Profile

Download (10.5 KB) Statistics
| Branch: | Revision:

examples / src / main / java / org / distorted / examples / inflate / InflateActivity2.java @ 698ad0a8

1 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 77a500b3 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 77a500b3 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 77a500b3 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.inflate;
21
22 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
23 64558e4e Leszek Koltunski
import org.distorted.examples.R;
24
import org.distorted.library.main.DistortedTexture;
25
import org.distorted.library.mesh.MeshBase;
26
import org.distorted.library.mesh.MeshCubes;
27 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
28 64558e4e Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
29
import org.distorted.library.mesh.MeshSphere;
30 4db4e39b Leszek Koltunski
import org.distorted.library.type.Static4D;
31 64558e4e Leszek Koltunski
32 77a500b3 Leszek Koltunski
import android.app.Activity;
33
import android.graphics.Bitmap;
34
import android.graphics.BitmapFactory;
35
import android.graphics.Canvas;
36
import android.graphics.Paint;
37
import android.opengl.GLSurfaceView;
38
import android.os.Bundle;
39 64558e4e Leszek Koltunski
import android.view.View;
40
import android.widget.AdapterView;
41
import android.widget.ArrayAdapter;
42 77a500b3 Leszek Koltunski
import android.widget.SeekBar;
43 64558e4e Leszek Koltunski
import android.widget.Spinner;
44 77a500b3 Leszek Koltunski
import android.widget.TextView;
45
46
import java.io.IOException;
47
import java.io.InputStream;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 64558e4e Leszek Koltunski
public class InflateActivity2 extends Activity implements SeekBar.OnSeekBarChangeListener, AdapterView.OnItemSelectedListener
52 77a500b3 Leszek Koltunski
{
53
    private Bitmap mBitmap;
54 64558e4e Leszek Koltunski
    private TextView mTextLevel;
55
    private DistortedTexture mTexture;
56
    private MeshBase mMesh;
57 687263cc Leszek Koltunski
    private int mNumCols, mNumRows, mNumSlic;
58 77a500b3 Leszek Koltunski
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
    
61
    @Override
62
    protected void onPause() 
63
      {
64
      GLSurfaceView mView = this.findViewById(R.id.inflateSurfaceView);
65
      if( mView!=null ) mView.onPause();
66
67 e3900503 Leszek Koltunski
      DistortedLibrary.onPause();
68 77a500b3 Leszek Koltunski
      super.onPause();
69
      }
70
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
    
73
    @Override
74
    protected void onResume() 
75
      {
76
      super.onResume();
77
      
78
      GLSurfaceView mView = this.findViewById(R.id.inflateSurfaceView);
79
      if( mView!=null ) mView.onResume();  
80
      }
81
    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
    
84
    @Override
85
    protected void onDestroy() 
86
      {
87 e3900503 Leszek Koltunski
      DistortedLibrary.onDestroy();
88 77a500b3 Leszek Koltunski
      super.onDestroy();
89
      }
90 64558e4e Leszek Koltunski
91 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93
    @Override
94
    protected void onCreate(Bundle savedState)
95
      {
96
      super.onCreate(savedState);
97
98
      Bundle b = getIntent().getExtras();
99
100
      String str     = b.getString("string");
101
      int objectType = b.getInt("type");
102 4db4e39b Leszek Koltunski
      int bitmapID   = b.getInt("bitmap");
103 687263cc Leszek Koltunski
      mNumCols       = b.getInt("cols");
104
      mNumRows       = b.getInt("rows");
105
      mNumSlic       = b.getInt("slices");
106 4db4e39b Leszek Koltunski
107 687263cc Leszek Koltunski
      int maxsize = mNumCols > mNumRows ? (mNumCols>mNumSlic ? mNumCols:mNumSlic) : (mNumRows>mNumSlic ? mNumRows:mNumSlic) ;
108 4db4e39b Leszek Koltunski
109
      createBitmap(maxsize,bitmapID);
110 77a500b3 Leszek Koltunski
111 24624a1a Leszek Koltunski
      switch(objectType)
112
        {
113 4db4e39b Leszek Koltunski
        case 0: if( bitmapID!=-1 )
114
                  {
115 687263cc Leszek Koltunski
                  mMesh = new MeshCubes(mNumCols, str, mNumSlic);
116 4db4e39b Leszek Koltunski
                  }
117
                else
118
                  {
119 687263cc Leszek Koltunski
                  Static4D mapFB = new Static4D(0.0f,0.0f, (float)mNumCols/maxsize, (float)mNumRows/maxsize);
120
                  Static4D mapLR = new Static4D(0.0f,0.0f, (float)mNumSlic/maxsize, (float)mNumRows/maxsize);
121
                  Static4D mapTB = new Static4D(0.0f,0.0f, (float)mNumCols/maxsize, (float)mNumSlic/maxsize);
122 4db4e39b Leszek Koltunski
123 687263cc Leszek Koltunski
                  mMesh = new MeshCubes(mNumCols, str, mNumSlic, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
124 4db4e39b Leszek Koltunski
                  }
125 698ad0a8 Leszek Koltunski
                mMesh.setStretch(mNumCols,mNumRows,mNumSlic);
126 4db4e39b Leszek Koltunski
                break;
127 687263cc Leszek Koltunski
        case 1: mMesh = new MeshRectangles(mNumCols,mNumRows);
128 698ad0a8 Leszek Koltunski
                mMesh.setStretch(mNumCols,mNumRows,0);
129 4db4e39b Leszek Koltunski
                break;
130 687263cc Leszek Koltunski
        case 2: mMesh = new MeshSphere(mNumRows);
131 698ad0a8 Leszek Koltunski
                mMesh.setStretch(mNumRows,mNumRows,mNumRows);
132 4db4e39b Leszek Koltunski
                break;
133
        case 3: mMesh = new MeshQuad();
134 698ad0a8 Leszek Koltunski
                mMesh.setStretch(1,1,0);
135 4db4e39b Leszek Koltunski
                break;
136 24624a1a Leszek Koltunski
        }
137 77a500b3 Leszek Koltunski
138 687263cc Leszek Koltunski
      mTexture = new DistortedTexture();
139 77a500b3 Leszek Koltunski
140
      setContentView(R.layout.inflatelayout);
141
142 64558e4e Leszek Koltunski
      Spinner renderSpinner  = findViewById(R.id.inflateSpinnerMode);
143
      renderSpinner.setOnItemSelectedListener(this);
144 77a500b3 Leszek Koltunski
145 64558e4e Leszek Koltunski
      String[] objectBitmap = new String[] { "Render: Normal", "Render: OIT" };
146
147
      ArrayAdapter<String> adapterBitmap = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, objectBitmap);
148
      adapterBitmap.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
149
      renderSpinner.setAdapter(adapterBitmap);
150
151
      mTextLevel = findViewById(R.id.inflateInflateText);
152 77a500b3 Leszek Koltunski
153 64558e4e Leszek Koltunski
      SeekBar transparencyBar = findViewById(R.id.inflateTransparency);
154
      transparencyBar.setOnSeekBarChangeListener(this);
155
      transparencyBar.setProgress(50);
156
157
      SeekBar inflateBar = findViewById(R.id.inflateInflateLevel);
158
      inflateBar.setOnSeekBarChangeListener(this);
159
      inflateBar.setProgress(50);
160
      }
161 77a500b3 Leszek Koltunski
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
164 4db4e39b Leszek Koltunski
    private void createBitmap(int size, int bitmapID)
165 77a500b3 Leszek Koltunski
      {
166 4db4e39b Leszek Koltunski
      if( bitmapID!=-1)
167 77a500b3 Leszek Koltunski
        {
168 4db4e39b Leszek Koltunski
        InputStream is = getResources().openRawResource(bitmapID);
169 77a500b3 Leszek Koltunski
170 4db4e39b Leszek Koltunski
        try
171
          {
172
          mBitmap = BitmapFactory.decodeStream(is);
173
          }
174
        finally
175
          {
176 77a500b3 Leszek Koltunski
          try
177
            {
178 4db4e39b Leszek Koltunski
            is.close();
179 77a500b3 Leszek Koltunski
            }
180 4db4e39b Leszek Koltunski
          catch(IOException e) { }
181 77a500b3 Leszek Koltunski
          }
182 4db4e39b Leszek Koltunski
        }
183
      else
184
        {
185
        final int T = 64;
186
        final int S = T*size;
187
188
        Paint paint = new Paint();
189
        mBitmap = Bitmap.createBitmap(S,S, Bitmap.Config.ARGB_8888);
190
        Canvas canvas = new Canvas(mBitmap);
191
192
        paint.setAntiAlias(true);
193
        paint.setTextAlign(Paint.Align.CENTER);
194
        paint.setColor(0xff008800);
195
        paint.setStyle(Paint.Style.FILL);
196
        canvas.drawRect(0, 0, S, S, paint);
197
        paint.setColor(0xffffffff);
198
199
        for(int i=0; i<=size ; i++ )
200 77a500b3 Leszek Koltunski
          {
201 4db4e39b Leszek Koltunski
          canvas.drawRect( T*i-1, 0, T*i+1, S, paint);
202
          canvas.drawRect( 0, T*i-1, S, T*i+1, paint);
203 77a500b3 Leszek Koltunski
          }
204
        }
205 4db4e39b Leszek Koltunski
      }
206 77a500b3 Leszek Koltunski
207 4db4e39b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
208
209
    public Bitmap getBitmap()
210
      {
211 77a500b3 Leszek Koltunski
      return mBitmap;
212
      }
213
214 64558e4e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
215
216
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
217
      {
218
      InflateSurfaceView v = this.findViewById(R.id.inflateSurfaceView);
219
      InflateRenderer renderer = v.getRenderer();
220
221
      switch(parent.getId())
222
        {
223
        case R.id.inflateSpinnerMode: renderer.setRenderModeToOIT(pos==1);
224
                                      break;
225
        }
226
      }
227
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
230
    public void onNothingSelected(AdapterView<?> parent)
231
      {
232
      }
233
234 77a500b3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
235
236
    public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
237
      {
238
      switch (bar.getId())
239
        {
240 64558e4e Leszek Koltunski
        case R.id.inflateTransparency: InflateSurfaceView v1 = this.findViewById(R.id.inflateSurfaceView);
241
                                       v1.getRenderer().setTransparency(progress);
242
                                       break;
243
        case R.id.inflateInflateLevel: InflateSurfaceView v2 = this.findViewById(R.id.inflateSurfaceView);
244
                                       float level = v2.getRenderer().setLevel(progress);
245
                                       mTextLevel.setText(getString(R.string.inflate_placeholder, level));
246
                                       break;
247 77a500b3 Leszek Koltunski
        }
248
      }
249
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251
252
    public void onStartTrackingTouch(SeekBar bar) { }
253
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
256
    public void onStopTrackingTouch(SeekBar bar)  { }
257
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
260
    public DistortedTexture getTexture()
261
      {
262
      return mTexture;
263
      }
264
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
267
    public MeshBase getMesh()
268
      {
269
      return mMesh;
270
      }
271 687263cc Leszek Koltunski
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273
274
    public int getNumCols()
275
      {
276
      return mNumCols;
277
      }
278
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280
281
    public int getNumRows()
282
      {
283
      return mNumRows;
284
      }
285
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287
288
    public int getNumSlic()
289
      {
290
      return mNumSlic;
291
      }
292 77a500b3 Leszek Koltunski
}