Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deferredjob / DeferredJobRenderer.java @ 77e66c58

1 887e1853 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26 887e1853 Leszek Koltunski
import android.graphics.Bitmap;
27
import android.graphics.Canvas;
28
import android.graphics.Paint;
29
import android.opengl.GLSurfaceView;
30
31 ba984444 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
32 887e1853 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
33
import org.distorted.library.effect.MatrixEffectScale;
34 38b31baa Leszek Koltunski
import org.distorted.library.effect.VertexEffectMove;
35 887e1853 Leszek Koltunski
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 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshTriangle;
43 887e1853 Leszek Koltunski
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 dc10a48d Leszek Koltunski
import java.io.InputStream;
50
51 887e1853 Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
52
import javax.microedition.khronos.opengles.GL10;
53
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 dc10a48d Leszek Koltunski
class DeferredJobRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
57 887e1853 Leszek Koltunski
{
58 dc10a48d Leszek Koltunski
    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 887e1853 Leszek Koltunski
    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 dc10a48d Leszek Koltunski
      mResources = v.getResources();
79
80 887e1853 Leszek Koltunski
      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 9cbe58b0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);
89
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
90 887e1853 Leszek Koltunski
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 bf3b5480 Leszek Koltunski
      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 887e1853 Leszek Koltunski
105
      mEffects = new DistortedEffects();
106 bf3b5480 Leszek Koltunski
      mEffects.apply( mRotate );
107 887e1853 Leszek Koltunski
      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 d08bd5a3 Leszek Koltunski
      final float SCALE = 0.4f;
126 887e1853 Leszek Koltunski
      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 bf3b5480 Leszek Koltunski
      mTexture.setTexture( createTexture() );
138 887e1853 Leszek Koltunski
139 bf3b5480 Leszek Koltunski
      if( mMesh==null ) mMesh = createMesh();
140 887e1853 Leszek Koltunski
141
      mScreen.detachAll();
142
      mScreen.attach(mTexture,mEffects,mMesh);
143
144 bf3b5480 Leszek Koltunski
      VertexEffectRotate.enable();
145 887e1853 Leszek Koltunski
146 dc10a48d Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(this);
147 887e1853 Leszek Koltunski
      }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151 bf3b5480 Leszek Koltunski
    void apply(int number)
152 887e1853 Leszek Koltunski
      {
153 eb507734 Leszek Koltunski
      mRotate.setMeshAssociation(0,number);
154 bf3b5480 Leszek Koltunski
      mAngle.set(360);
155
      mAngleDyn.resetToBeginning();
156 887e1853 Leszek Koltunski
      }
157
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160 bf3b5480 Leszek Koltunski
    private MeshBase createMesh()
161 887e1853 Leszek Koltunski
      {
162 ba984444 Leszek Koltunski
      final int MESHES=4;
163 887e1853 Leszek Koltunski
164 f2085b96 Leszek Koltunski
      MeshBase[] meshes = new MeshTriangle[MESHES];
165 38b31baa Leszek Koltunski
      VertexEffectMove[] moveEffect = new VertexEffectMove[MESHES];
166 887e1853 Leszek Koltunski
167 f2085b96 Leszek Koltunski
      meshes[0] = new MeshTriangle(1);
168 eb507734 Leszek Koltunski
      meshes[0].setEffectAssociation(0,1,0);
169 bf3b5480 Leszek Koltunski
170
      for(int i=1; i<MESHES; i++)
171 887e1853 Leszek Koltunski
        {
172 bf3b5480 Leszek Koltunski
        meshes[i] = meshes[0].copy(true);
173 eb507734 Leszek Koltunski
        meshes[i].setEffectAssociation(0,1,i);
174 887e1853 Leszek Koltunski
        }
175
176 ba984444 Leszek Koltunski
      Static4D[] textureMaps = new Static4D[MESHES];
177 5dfbbda7 Leszek Koltunski
178
      for(int i=0; i<MESHES; i++)
179
        {
180 ba984444 Leszek Koltunski
        textureMaps[i] = new Static4D(i*0.25f,0.0f,0.25f,1.0f);
181 5dfbbda7 Leszek Koltunski
        }
182
183
      MeshBase[] tmp = new MeshBase[2];
184
185
      tmp[0] = new MeshJoined(meshes);
186 887e1853 Leszek Koltunski
187 ba984444 Leszek Koltunski
      for(int i=0; i<MESHES; i++)
188
        {
189 38b31baa Leszek Koltunski
        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 ba984444 Leszek Koltunski
        }
193 887e1853 Leszek Koltunski
194 5dfbbda7 Leszek Koltunski
      tmp[1] = tmp[0].copy(true);
195
196 fcb09e1f Leszek Koltunski
      tmp[0].mergeEffComponents();
197
      tmp[1].mergeEffComponents();
198 5dfbbda7 Leszek Koltunski
199 eb507734 Leszek Koltunski
      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 5dfbbda7 Leszek Koltunski
      MeshBase combined = new MeshJoined(tmp);
203
204 ba984444 Leszek Koltunski
      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 5dfbbda7 Leszek Koltunski
210 ba984444 Leszek Koltunski
      combined.setTextureMap(textureMaps,0);
211
      combined.setTextureMap(textureMaps,MESHES);
212 5dfbbda7 Leszek Koltunski
213 38b31baa Leszek Koltunski
      combined.setComponentCenter(0,-0.6f,0,0);
214
      combined.setComponentCenter(1,+0.6f,0,0);
215 b1950186 Leszek Koltunski
216 5dfbbda7 Leszek Koltunski
      return combined;
217 887e1853 Leszek Koltunski
      }
218 ba984444 Leszek Koltunski
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 dc10a48d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 887e1853 Leszek Koltunski
}