Project

General

Profile

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

examples / src / main / java / org / distorted / examples / deferredjob / DeferredJobRenderer.java @ 30f337e5

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.content.res.Resources;
23
import android.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26
import android.opengl.GLSurfaceView;
27

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

    
46
import java.io.InputStream;
47

    
48
import javax.microedition.khronos.egl.EGLConfig;
49
import javax.microedition.khronos.opengles.GL10;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

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

    
63
    private DistortedTexture mTexture;
64
    private MeshBase mMesh;
65

    
66
    Static4D mQuat1, mQuat2;
67
    int mScreenMin;
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
    DeferredJobRenderer(GLSurfaceView v)
72
      {
73
      mResources = v.getResources();
74

    
75
      mScreen = new DistortedScreen();
76
      mScale= new Static3D(1,1,1);
77
      Static3D center=new Static3D(0,0,0);
78

    
79
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
80
      sink.add( new Static1D(0.5f) );
81
      sink.add( new Static1D(2.0f) );
82

    
83
      mQuat1 = new Static4D(0,0,0,1);
84
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
85

    
86
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
87
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
88

    
89
      quatInt1.add(mQuat1);
90
      quatInt2.add(mQuat2);
91

    
92
      mAngle = new Static1D(0);
93

    
94
      mAngleDyn = new Dynamic1D(2000,0.5f);
95
      mAngleDyn.add(new Static1D(0));
96
      mAngleDyn.add( mAngle );
97

    
98
      mRotate = new VertexEffectRotate( mAngleDyn, new Static3D(1,0,0), new Static3D(0,0,0) );
99

    
100
      mEffects = new DistortedEffects();
101
      mEffects.apply( mRotate );
102
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
103
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
104
      mEffects.apply( new MatrixEffectScale(mScale));
105

    
106
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
   
111
    public void onDrawFrame(GL10 glUnused) 
112
      {
113
      mScreen.render( System.currentTimeMillis() );
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
    
118
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
119
      {
120
      final float SCALE = 0.4f;
121
      mScreenMin = Math.min(width, height);
122
      float factor = SCALE*mScreenMin;
123
      mScale.set(factor,factor,factor);
124
      mScreen.resize(width, height);
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
130
      {
131
      if( mTexture==null ) mTexture = new DistortedTexture();
132
      mTexture.setTexture( createTexture() );
133

    
134
      if( mMesh==null ) mMesh = createMesh();
135

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

    
139
      VertexEffectRotate.enable();
140

    
141
      DistortedLibrary.onSurfaceCreated(this);
142
      }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
    void apply(int number)
147
      {
148
      mRotate.setMeshAssociation(0,number);
149
      mAngle.set(360);
150
      mAngleDyn.resetToBeginning();
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
    private MeshBase createMesh()
156
      {
157
      final int MESHES=4;
158

    
159
      MeshBase[] meshes = new MeshTriangle[MESHES];
160
      VertexEffectMove[] moveEffect = new VertexEffectMove[MESHES];
161

    
162
      meshes[0] = new MeshTriangle(1);
163
      meshes[0].setEffectAssociation(0,1,0);
164

    
165
      for(int i=1; i<MESHES; i++)
166
        {
167
        meshes[i] = meshes[0].copy(true);
168
        meshes[i].setEffectAssociation(0,1,i);
169
        }
170

    
171
      Static4D[] textureMaps = new Static4D[MESHES];
172

    
173
      for(int i=0; i<MESHES; i++)
174
        {
175
        textureMaps[i] = new Static4D(i*0.25f,0.0f,0.25f,1.0f);
176
        }
177

    
178
      MeshBase[] tmp = new MeshBase[2];
179

    
180
      tmp[0] = new MeshJoined(meshes);
181

    
182
      for(int i=0; i<MESHES; i++)
183
        {
184
        moveEffect[i] = new VertexEffectMove( new Static3D(0,0,0.5f-i/(MESHES-1.0f)) );
185
        moveEffect[i].setMeshAssociation(0,i);
186
        tmp[0].apply(moveEffect[i]);
187
        }
188

    
189
      tmp[1] = tmp[0].copy(true);
190

    
191
      tmp[0].mergeEffComponents();
192
      tmp[1].mergeEffComponents();
193

    
194
      tmp[0].setEffectAssociation(0,0,0); // set the equAssoc of the 0th (the only) component to 0
195
      tmp[1].setEffectAssociation(0,0,1); // set the equAssoc of the 0th (the only) component to 1
196

    
197
      MeshBase combined = new MeshJoined(tmp);
198

    
199
      MatrixEffectMove moveL = new MatrixEffectMove( new Static3D(-0.6f,0,0) );
200
      MatrixEffectMove moveR = new MatrixEffectMove( new Static3D(+0.6f,0,0) );
201

    
202
      combined.apply(moveL,0,0);
203
      combined.apply(moveR,0,1);
204

    
205
      combined.setTextureMap(textureMaps,0);
206
      combined.setTextureMap(textureMaps,MESHES);
207

    
208
      combined.setComponentCenter(0,-0.6f,0,0);
209
      combined.setComponentCenter(1,+0.6f,0,0);
210

    
211
      return combined;
212
      }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

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

    
232
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
233
      Canvas canvas = new Canvas(result);
234
      Paint paint = new Paint();
235
      paint.setAntiAlias(true);
236
      paint.setStrokeWidth(STROKE);
237

    
238
      for(int i=0; i<FACES; i++)
239
        {
240
        paint.setColor(FACE_COLORS[i]);
241
        paint.setStyle(Paint.Style.FILL);
242

    
243
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
244

    
245
        paint.setColor(0xff000000);
246
        paint.setStyle(Paint.Style.STROKE);
247

    
248
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
249
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
250
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
251

    
252
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
253
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
254
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
255
        }
256

    
257
      return result;
258
      }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261

    
262
    public void distortedException(Exception ex)
263
      {
264
      android.util.Log.e("DeferredJob", ex.getMessage() );
265
      }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268

    
269
    public InputStream localFile(int fileID)
270
      {
271
      return mResources.openRawResource(fileID);
272
      }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
    public void logMessage(String message)
277
      {
278
      android.util.Log.e("DeferredJob", message );
279
      }
280
}
(2-2/3)