Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshfile / MeshFileRenderer.java @ 8ac4c0bc

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.meshfile;
21

    
22
import android.content.Context;
23
import android.content.res.Resources;
24
import android.graphics.Bitmap;
25
import android.graphics.Canvas;
26
import android.graphics.Paint;
27
import android.opengl.GLSurfaceView;
28

    
29
import org.distorted.examples.R;
30
import org.distorted.library.effect.MatrixEffectQuaternion;
31
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedLibrary;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.mesh.MeshBase;
37
import org.distorted.library.mesh.MeshFile;
38
import org.distorted.library.type.DynamicQuat;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

    
42
import java.io.DataInputStream;
43
import java.io.IOException;
44
import java.io.InputStream;
45

    
46
import javax.microedition.khronos.egl.EGLConfig;
47
import javax.microedition.khronos.opengles.GL10;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class MeshFileRenderer implements GLSurfaceView.Renderer
52
{
53
    private GLSurfaceView mView;
54
    private DistortedTexture mTexture;
55
    private DistortedScreen mScreen;
56
    private DistortedEffects mEffects;
57
    private Static3D mScale;
58
    private MeshBase mMesh;
59

    
60
    Static4D mQuat1, mQuat2;
61
    int mScreenMin;
62

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

    
65
    MeshFileRenderer(GLSurfaceView v)
66
      {
67
      mView = v;
68
      mScreen = new DistortedScreen();
69
      mScale= new Static3D(1,1,1);
70
      Static3D center=new Static3D(0,0,0);
71

    
72
      mQuat1 = new Static4D(0,0,0,1);
73
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
74

    
75
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
76
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
77

    
78
      quatInt1.add(mQuat1);
79
      quatInt2.add(mQuat2);
80

    
81
      mEffects = new DistortedEffects();
82
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
83
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
84
      mEffects.apply( new MatrixEffectScale(mScale));
85

    
86
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
87
      }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
   
91
    public void onDrawFrame(GL10 glUnused) 
92
      {
93
      mScreen.render( System.currentTimeMillis() );
94
      }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
    
98
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
99
      {
100
      final float SCALE = 0.3f;
101
      mScreenMin = Math.min(width, height);
102
      float factor = SCALE*mScreenMin;
103
      mScale.set(factor,factor,factor);
104
      mScreen.resize(width, height);
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
110
      {
111
      if( mTexture==null ) mTexture = new DistortedTexture();
112

    
113
      try
114
        {
115
        DistortedLibrary.onCreate(mView.getContext());
116
        }
117
      catch(Exception ex)
118
        {
119
        android.util.Log.e("MeshFile", ex.getMessage() );
120
        }
121
      }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
    void open(int resourceID)
126
      {
127
      mTexture.setTexture( createTexture(resourceID) );
128

    
129
      long t1 = System.currentTimeMillis();
130
      mMesh = createMesh(resourceID);
131
      long t2 = System.currentTimeMillis();
132

    
133
      android.util.Log.e("file", "time: "+(t2-t1));
134

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

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

    
141
    private Bitmap createTexture(int resourceID)
142
      {
143
      switch(resourceID)
144
          {
145
          case  R.raw.deferredjob:
146
          case  R.raw.meshjoin   : final int[] TET_COLORS4 = new int[] { 0xffffff00,
147
                                                                         0xff00ff00,
148
                                                                         0xff0000ff,
149
                                                                         0xffff0000 };
150
                                   return createTetrahedrenTexture(TET_COLORS4);
151
          case  R.raw.predeform  : return createGridTexture(3);
152
          case  R.raw.cube2      :
153
          case  R.raw.cube3      :
154
          case  R.raw.cube4      :
155
          case  R.raw.cube5      : final int[] CUBE_COLORS = new int[] { 0xffffff00,
156
                                                                         0xffffffff,
157
                                                                         0xff0000ff,
158
                                                                         0xff00ff00,
159
                                                                         0xffff0000,
160
                                                                         0xffb5651d };
161
                                   return createCubeTexture(CUBE_COLORS);
162
          case  R.raw.pyra3      :
163
          case  R.raw.pyra4      :
164
          case  R.raw.pyra5      : final int[] TET_COLORS5 = new int[] { 0xffffff00,
165
                                                                         0xff00ff00,
166
                                                                         0xff0000ff,
167
                                                                         0xffff0000,
168
                                                                         0xff000000 };
169
                                   return createTetrahedrenTexture(TET_COLORS5);
170
          }
171

    
172
      return null;
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
    private Bitmap createCubeTexture(int[] faceColors)
178
      {
179
      final int FACES=faceColors.length;
180
      int SIZE = 200;
181
      final float R = SIZE*0.10f;
182
      final float M = SIZE*0.05f;
183

    
184
      Bitmap bitmap;
185
      Paint paint = new Paint();
186
      bitmap = Bitmap.createBitmap( (FACES+1)*SIZE, SIZE, Bitmap.Config.ARGB_8888);
187
      Canvas canvas = new Canvas(bitmap);
188

    
189
      paint.setStyle(Paint.Style.FILL);
190
      paint.setColor(0xff000000);
191
      canvas.drawRect(0, 0, (FACES+1)*SIZE, SIZE, paint);
192

    
193
      for(int face=0; face<FACES; face++)
194
        {
195
        paint.setColor(faceColors[face]);
196
        canvas.drawRoundRect( face*SIZE+M, M, (face+1)*SIZE-M, SIZE-M, R, R, paint);
197
        }
198

    
199
      return bitmap;
200
      }
201

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

    
204
    private Bitmap createGridTexture(int lines)
205
      {
206
      int SIZE = 200;
207
      Bitmap bitmap = Bitmap.createBitmap(SIZE,SIZE, Bitmap.Config.ARGB_8888);
208
      Canvas canvas = new Canvas(bitmap);
209

    
210
      Paint paint = new Paint();
211
      paint.setColor(0xff008800);
212
      paint.setStyle(Paint.Style.FILL);
213
      canvas.drawRect(0, 0, SIZE, SIZE, paint);
214
      paint.setColor(0xffffffff);
215

    
216
      for(int i=0; i<=lines ; i++ )
217
        {
218
        int x = (SIZE*i)/lines;
219
        canvas.drawRect(x-1,   0,  x+1, SIZE, paint);
220
        canvas.drawRect(  0, x-1, SIZE,  x+1, paint);
221
        }
222

    
223
      return bitmap;
224
      }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
    private Bitmap createTetrahedrenTexture(int[] faceColors)
229
      {
230
      final int FACES=faceColors.length;
231
      int SIZE = 200;
232
      float STROKE = 0.05f*SIZE;
233
      float OFF = STROKE/2 -1;
234
      float OFF2 = 0.5f*SIZE + OFF;
235
      float HEIGHT = SIZE - OFF;
236
      float RADIUS = SIZE/12;
237
      float ARC1_H = 0.2f*SIZE;
238
      float ARC1_W = SIZE*0.5f;
239
      float ARC2_W = 0.153f*SIZE;
240
      float ARC2_H = 0.905f*SIZE;
241
      float ARC3_W = SIZE-ARC2_W;
242

    
243
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
244
      Canvas canvas = new Canvas(result);
245
      Paint paint = new Paint();
246
      paint.setAntiAlias(true);
247
      paint.setStrokeWidth(STROKE);
248

    
249
      for(int i=0; i<FACES; i++)
250
        {
251
        paint.setColor(faceColors[i]);
252
        paint.setStyle(Paint.Style.FILL);
253

    
254
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
255

    
256
        paint.setColor(0xff000000);
257
        paint.setStyle(Paint.Style.STROKE);
258

    
259
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
260
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
261
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
262

    
263
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
264
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
265
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
266
        }
267

    
268
      return result;
269
      }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
    private MeshBase createMesh(int resourceID)
274
      {
275
      Context con = mView.getContext();
276
      Resources res = con.getResources();
277
      InputStream is = res.openRawResource(resourceID);
278
      DataInputStream dos = new DataInputStream(is);
279
      MeshBase mesh = new MeshFile(dos);
280

    
281
      try
282
        {
283
        is.close();
284
        }
285
      catch(IOException e)
286
        {
287
        android.util.Log.e("meshFile", "Error closing InputStream: "+e.toString());
288
        }
289

    
290
      return mesh;
291
      }
292
}
(2-2/3)