Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deferredjob / DeferredJobRenderer.java @ 625c67de

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

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.graphics.Bitmap;
27
import android.graphics.Canvas;
28
import android.graphics.Paint;
29
import android.opengl.GLSurfaceView;
30

    
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectMove;
35
import org.distorted.library.effect.VertexEffectRotate;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.mesh.MeshBase;
41
import org.distorted.library.mesh.MeshJoined;
42
import org.distorted.library.mesh.MeshTriangle;
43
import org.distorted.library.type.Dynamic1D;
44
import org.distorted.library.type.DynamicQuat;
45
import org.distorted.library.type.Static1D;
46
import org.distorted.library.type.Static3D;
47
import org.distorted.library.type.Static4D;
48

    
49
import java.io.InputStream;
50

    
51
import javax.microedition.khronos.egl.EGLConfig;
52
import javax.microedition.khronos.opengles.GL10;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
class DeferredJobRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
57
{
58
    private final GLSurfaceView mView;
59
    private final Resources mResources;
60
    private final DistortedScreen mScreen;
61
    private final DistortedEffects mEffects;
62
    private final Static3D mScale;
63
    private final VertexEffectRotate mRotate;
64
    private final Dynamic1D mAngleDyn;
65
    private final Static1D mAngle;
66

    
67
    private DistortedTexture mTexture;
68
    private MeshBase mMesh;
69

    
70
    Static4D mQuat1, mQuat2;
71
    int mScreenMin;
72

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

    
75
    DeferredJobRenderer(GLSurfaceView v)
76
      {
77
      mView = v;
78
      mResources = v.getResources();
79

    
80
      mScreen = new DistortedScreen();
81
      mScale= new Static3D(1,1,1);
82
      Static3D center=new Static3D(0,0,0);
83

    
84
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
85
      sink.add( new Static1D(0.5f) );
86
      sink.add( new Static1D(2.0f) );
87

    
88
      mQuat1 = new Static4D(0,0,0,1);
89
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
90

    
91
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
92
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
93

    
94
      quatInt1.add(mQuat1);
95
      quatInt2.add(mQuat2);
96

    
97
      mAngle = new Static1D(0);
98

    
99
      mAngleDyn = new Dynamic1D(2000,0.5f);
100
      mAngleDyn.add(new Static1D(0));
101
      mAngleDyn.add( mAngle );
102

    
103
      mRotate = new VertexEffectRotate( mAngleDyn, new Static3D(1,0,0), new Static3D(0,0,0) );
104

    
105
      mEffects = new DistortedEffects();
106
      mEffects.apply( mRotate );
107
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
108
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
109
      mEffects.apply( new MatrixEffectScale(mScale));
110

    
111
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
   
116
    public void onDrawFrame(GL10 glUnused) 
117
      {
118
      mScreen.render( System.currentTimeMillis() );
119
      }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
    
123
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
124
      {
125
      final float SCALE = 0.4f;
126
      mScreenMin = Math.min(width, height);
127
      float factor = SCALE*mScreenMin;
128
      mScale.set(factor,factor,factor);
129
      mScreen.resize(width, height);
130
      }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
    
134
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
135
      {
136
      if( mTexture==null ) mTexture = new DistortedTexture();
137
      mTexture.setTexture( createTexture() );
138

    
139
      if( mMesh==null ) mMesh = createMesh();
140

    
141
      mScreen.detachAll();
142
      mScreen.attach(mTexture,mEffects,mMesh);
143

    
144
      VertexEffectRotate.enable();
145

    
146
      DistortedLibrary.onSurfaceCreated(this);
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
    void apply(int number)
152
      {
153
      mRotate.setMeshAssociation(0,number);
154
      mAngle.set(360);
155
      mAngleDyn.resetToBeginning();
156
      }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
    private MeshBase createMesh()
161
      {
162
      final int MESHES=4;
163

    
164
      MeshBase[] meshes = new MeshTriangle[MESHES];
165
      VertexEffectMove[] moveEffect = new VertexEffectMove[MESHES];
166

    
167
      meshes[0] = new MeshTriangle(1);
168
      meshes[0].setEffectAssociation(0,1,0);
169

    
170
      for(int i=1; i<MESHES; i++)
171
        {
172
        meshes[i] = meshes[0].copy(true);
173
        meshes[i].setEffectAssociation(0,1,i);
174
        }
175

    
176
      Static4D[] textureMaps = new Static4D[MESHES];
177

    
178
      for(int i=0; i<MESHES; i++)
179
        {
180
        textureMaps[i] = new Static4D(i*0.25f,0.0f,0.25f,1.0f);
181
        }
182

    
183
      MeshBase[] tmp = new MeshBase[2];
184

    
185
      tmp[0] = new MeshJoined(meshes);
186

    
187
      for(int i=0; i<MESHES; i++)
188
        {
189
        moveEffect[i] = new VertexEffectMove( new Static3D(0,0,0.5f-i/(MESHES-1.0f)) );
190
        moveEffect[i].setMeshAssociation(0,i);
191
        tmp[0].apply(moveEffect[i]);
192
        }
193

    
194
      tmp[1] = tmp[0].copy(true);
195

    
196
      tmp[0].mergeEffComponents();
197
      tmp[1].mergeEffComponents();
198

    
199
      tmp[0].setEffectAssociation(0,0,0); // set the equAssoc of the 0th (the only) component to 0
200
      tmp[1].setEffectAssociation(0,0,1); // set the equAssoc of the 0th (the only) component to 1
201

    
202
      MeshBase combined = new MeshJoined(tmp);
203

    
204
      MatrixEffectMove moveL = new MatrixEffectMove( new Static3D(-0.6f,0,0) );
205
      MatrixEffectMove moveR = new MatrixEffectMove( new Static3D(+0.6f,0,0) );
206

    
207
      combined.apply(moveL,0,0);
208
      combined.apply(moveR,0,1);
209

    
210
      combined.setTextureMap(textureMaps,0);
211
      combined.setTextureMap(textureMaps,MESHES);
212

    
213
      combined.setComponentCenter(0,-0.6f,0,0);
214
      combined.setComponentCenter(1,+0.6f,0,0);
215

    
216
      return combined;
217
      }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
    private Bitmap createTexture()
222
      {
223
      final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
224
      final int FACES=FACE_COLORS.length;
225
      int SIZE = 200;
226
      float STROKE = 0.05f*SIZE;
227
      float OFF = STROKE/2 -1;
228
      float OFF2 = 0.5f*SIZE + OFF;
229
      float HEIGHT = SIZE - OFF;
230
      float RADIUS = SIZE/12;
231
      float ARC1_H = 0.2f*SIZE;
232
      float ARC1_W = SIZE*0.5f;
233
      float ARC2_W = 0.153f*SIZE;
234
      float ARC2_H = 0.905f*SIZE;
235
      float ARC3_W = SIZE-ARC2_W;
236

    
237
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
238
      Canvas canvas = new Canvas(result);
239
      Paint paint = new Paint();
240
      paint.setAntiAlias(true);
241
      paint.setStrokeWidth(STROKE);
242

    
243
      for(int i=0; i<FACES; i++)
244
        {
245
        paint.setColor(FACE_COLORS[i]);
246
        paint.setStyle(Paint.Style.FILL);
247

    
248
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
249

    
250
        paint.setColor(0xff000000);
251
        paint.setStyle(Paint.Style.STROKE);
252

    
253
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
254
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
255
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
256

    
257
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
258
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
259
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
260
        }
261

    
262
      return result;
263
      }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
    public void distortedException(Exception ex)
268
      {
269
      android.util.Log.e("DeferredJob", ex.getMessage() );
270
      }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
    public InputStream localFile(int fileID)
275
      {
276
      return mResources.openRawResource(fileID);
277
      }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
    public void logMessage(String message)
282
      {
283
      android.util.Log.e("DeferredJob", message );
284
      }
285
}
(2-2/3)