Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshjoin / MeshJoinRenderer.java @ cdd71ce7

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

    
22
import android.graphics.Bitmap;
23
import android.graphics.Canvas;
24
import android.graphics.Paint;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.library.effect.MatrixEffectQuaternion;
28
import org.distorted.library.effect.MatrixEffectScale;
29
import org.distorted.library.effect.VertexEffectSink;
30
import org.distorted.library.main.DistortedEffects;
31
import org.distorted.library.main.DistortedLibrary;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.main.DistortedTexture;
34
import org.distorted.library.mesh.MeshBase;
35
import org.distorted.library.mesh.MeshJoined;
36
import org.distorted.library.mesh.MeshTriangles;
37
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.DynamicQuat;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42

    
43
import javax.microedition.khronos.egl.EGLConfig;
44
import javax.microedition.khronos.opengles.GL10;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
class MeshJoinRenderer implements GLSurfaceView.Renderer
49
{
50
    private GLSurfaceView mView;
51
    private DistortedTexture mTexture;
52
    private DistortedScreen mScreen;
53
    private DistortedEffects mEffects;
54
    private Static3D mScale;
55
    private MeshBase mMesh;
56

    
57
    Static4D mQuat1, mQuat2;
58
    int mScreenMin;
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
    MeshJoinRenderer(GLSurfaceView v)
63
      {
64
      mView = v;
65
      mScreen = new DistortedScreen();
66
      mScale= new Static3D(1,1,1);
67
      Static3D center=new Static3D(0,0,0);
68

    
69
      Dynamic1D sink = new Dynamic1D(5000,0.0f);
70
      sink.add( new Static1D(0.5f) );
71
      sink.add( new Static1D(2.0f) );
72

    
73
      mQuat1 = new Static4D(0,0,0,1);  // unity
74
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
75

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

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

    
82
      mEffects = new DistortedEffects();
83
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
84
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
85
      mEffects.apply( new MatrixEffectScale(mScale));
86
      mEffects.apply( new VertexEffectSink( sink, center, new Static4D(0,0,0,0.75f) ) );
87

    
88
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
89
      }
90

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

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
    
100
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
101
      {
102
      final float SCALE = 0.7f;
103
      mScreenMin = width<height ? width:height;
104
      float factor = SCALE*mScreenMin;
105
      mScale.set(factor,factor,factor);
106
      mScreen.resize(width, height);
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
    
111
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
112
      {
113
      if( mTexture==null ) mTexture = new DistortedTexture();
114
      mTexture.setTexture( createTetrahedronTexture() );
115

    
116
      if( mMesh==null ) mMesh = createJoinedTetrahedron();
117

    
118
      mScreen.detachAll();
119
      mScreen.attach(mTexture,mEffects,mMesh);
120

    
121
      VertexEffectSink.enable();
122

    
123
      try
124
        {
125
        DistortedLibrary.onCreate(mView.getContext());
126
        }
127
      catch(Exception ex)
128
        {
129
        android.util.Log.e("MeshJoin", ex.getMessage() );
130
        }
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
    private Bitmap createTetrahedronTexture()
136
      {
137
      final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
138
      final int FACES=FACE_COLORS.length;
139
      int SIZE = 200;
140
      float STROKE = 0.05f*SIZE;
141
      float OFF = STROKE/2 -1;
142
      float OFF2 = 0.5f*SIZE + OFF;
143
      float HEIGHT = SIZE - OFF;
144
      float RADIUS = SIZE/12;
145
      float ARC1_H = 0.2f*SIZE;
146
      float ARC1_W = SIZE*0.5f;
147
      float ARC2_W = 0.153f*SIZE;
148
      float ARC2_H = 0.905f*SIZE;
149
      float ARC3_W = SIZE-ARC2_W;
150

    
151
      Bitmap result = Bitmap.createBitmap(FACES*SIZE,SIZE, Bitmap.Config.ARGB_8888);
152
      Canvas canvas = new Canvas(result);
153
      Paint paint = new Paint();
154
      paint.setAntiAlias(true);
155
      paint.setStrokeWidth(STROKE);
156

    
157
      for(int i=0; i<FACES; i++)
158
        {
159
        paint.setColor(FACE_COLORS[i]);
160
        paint.setStyle(Paint.Style.FILL);
161

    
162
        canvas.drawRect(i*SIZE,0,(i+1)*SIZE,SIZE,paint);
163

    
164
        paint.setColor(0xff000000);
165
        paint.setStyle(Paint.Style.STROKE);
166

    
167
        canvas.drawLine(           i*SIZE, HEIGHT,  SIZE       +i*SIZE, HEIGHT, paint);
168
        canvas.drawLine(      OFF +i*SIZE,   SIZE,       OFF2  +i*SIZE,      0, paint);
169
        canvas.drawLine((SIZE-OFF)+i*SIZE,   SIZE, (SIZE-OFF2) +i*SIZE,      0, paint);
170

    
171
        canvas.drawArc( ARC1_W-RADIUS+i*SIZE, ARC1_H-RADIUS, ARC1_W+RADIUS+i*SIZE, ARC1_H+RADIUS, 225, 90, false, paint);
172
        canvas.drawArc( ARC2_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC2_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 105, 90, false, paint);
173
        canvas.drawArc( ARC3_W-RADIUS+i*SIZE, ARC2_H-RADIUS, ARC3_W+RADIUS+i*SIZE, ARC2_H+RADIUS, 345, 90, false, paint);
174
        }
175

    
176
      return result;
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
    private MeshBase createJoinedTetrahedron()
182
      {
183
      final float SQ2 = (float)Math.sqrt(2);
184
      final float SQ3 = (float)Math.sqrt(3);
185
      final float angleFaces = (float)((180/Math.PI)*(2*Math.asin(SQ3/3))); // angle between two faces of a tetrahedron
186
      final int MESHES=4;
187

    
188
      MeshBase[] meshes = new MeshTriangles[MESHES];
189
      for(int i=0; i<MESHES; i++) meshes[i] = new MeshTriangles(10);
190
/*
191
      MatrixEffect[] effects0 = new MatrixEffect[3];
192
      effects0[0] = new MatrixEffectScale( new Static3D(1,SQ3/2,1) );
193
      effects0[1] = new MatrixEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
194
      effects0[2] = new MatrixEffectMove( new Static3D(0,-SQ3*SQ2/12,SQ3/12) );
195

    
196
      meshes[0].apply(effects0);
197

    
198
      Static1D angle = new Static1D(angleFaces);
199
      Static3D axis  = new Static3D(-1,0,0);
200
      Static3D center= new Static3D(0,-SQ3*SQ2/12,-SQ3/6);
201

    
202
      MatrixEffect[] effects1 = new MatrixEffect[5];
203
      effects1[0] = effects0[0];
204
      effects1[1] = effects0[1];
205
      effects1[2] = effects0[2];
206
      effects1[3] = new MatrixEffectRotate( new Static1D(180), new Static3D(0,0,1), center );
207
      effects1[4] = new MatrixEffectRotate( angle, axis, center );
208
      meshes[1].apply(effects1);
209

    
210
      axis.set(0.5f,0,-SQ3/2);
211
      center.set2(SQ3/3);
212
      meshes[2].apply(effects1);
213

    
214
      axis.set2(SQ3/2);
215
      meshes[3].apply(effects1);
216
*/
217
      Static4D[] textureMaps = new Static4D[MESHES];
218

    
219
      for(int i=0; i<MESHES; i++) textureMaps[i] = new Static4D(i*0.25f,0.0f,0.25f,1.0f);
220

    
221
      MeshBase result = new MeshJoined(meshes);
222
      result.setTextureMap(textureMaps);
223

    
224
      return result;
225
      }
226
}
(2-2/3)