Project

General

Profile

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

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

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.BitmapFactory;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26
import android.opengl.GLSurfaceView;
27

    
28
import org.distorted.examples.R;
29
import org.distorted.library.effect.MatrixEffect;
30
import org.distorted.library.effect.MatrixEffectMove;
31
import org.distorted.library.effect.MatrixEffectQuaternion;
32
import org.distorted.library.effect.MatrixEffectRotate;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectSink;
35
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedLibrary;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.mesh.MeshBase;
40
import org.distorted.library.mesh.MeshJoined;
41
import org.distorted.library.mesh.MeshTriangles;
42
import org.distorted.library.type.Dynamic1D;
43
import org.distorted.library.type.DynamicQuat;
44
import org.distorted.library.type.Static1D;
45
import org.distorted.library.type.Static3D;
46
import org.distorted.library.type.Static4D;
47

    
48
import java.io.IOException;
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 MeshJoinRenderer implements GLSurfaceView.Renderer
57
{
58
    private GLSurfaceView mView;
59
    private DistortedTexture mTexture;
60
    private DistortedScreen mScreen;
61
    private DistortedEffects mEffects;
62
    private Static3D mScale;
63
    private MeshBase mMesh;
64

    
65
    Static4D mQuat1, mQuat2;
66
    int mScreenMin;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
    MeshJoinRenderer(GLSurfaceView v)
71
      {
72
      mView = v;
73
      mScreen = new DistortedScreen();
74
      mScale= new Static3D(1,1,1);
75
      Static3D center=new Static3D(0,0,0);
76

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

    
81
      mQuat1 = new Static4D(0,0,0,1);  // unity
82
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
83

    
84
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
85
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
86

    
87
      quatInt1.add(mQuat1);
88
      quatInt2.add(mQuat2);
89

    
90
      mEffects = new DistortedEffects();
91
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
92
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
93
      mEffects.apply( new MatrixEffectScale(mScale));
94
      mEffects.apply( new VertexEffectSink( sink, center, new Static4D(0,0,0,0.75f) ) );
95

    
96
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
97
      }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
   
101
    public void onDrawFrame(GL10 glUnused) 
102
      {
103
      mScreen.render( System.currentTimeMillis() );
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
109
      {
110
      final float SCALE = 0.7f;
111
      mScreenMin = width<height ? width:height;
112
      float factor = SCALE*mScreenMin;
113
      mScale.set(factor,factor,factor);
114
      mScreen.resize(width, height);
115
      }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
120
      {
121
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.pyraminx);
122
      Bitmap bitmap;
123

    
124
      try
125
        {
126
        bitmap = BitmapFactory.decodeStream(is);
127
        }
128
      finally
129
        {
130
        try
131
          {
132
          is.close();
133
          }
134
        catch(IOException e) { }
135
        }
136

    
137
      if( mTexture==null ) mTexture = new DistortedTexture();
138
      mTexture.setTexture( createTetrahedronTexture(bitmap) );
139

    
140
      if( mMesh==null ) mMesh = createJoinedTetrahedron();
141

    
142
      //mMesh.setShowNormals(true);
143

    
144
      mScreen.detachAll();
145
      mScreen.attach(mTexture,mEffects,mMesh);
146

    
147
      VertexEffectSink.enable();
148

    
149
      try
150
        {
151
        DistortedLibrary.onCreate(mView.getContext());
152
        }
153
      catch(Exception ex)
154
        {
155
        android.util.Log.e("MeshJoin", ex.getMessage() );
156
        }
157
      }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
    private Bitmap createTetrahedronTexture(Bitmap input)
162
      {
163
      final int[] FACE_COLORS = new int[] { 0xffffff00, 0xff00ff00, 0xff0000ff, 0xffff0000 };
164
      final int FACES=FACE_COLORS.length;
165
      int height = input.getHeight();
166

    
167
      Bitmap result = Bitmap.createBitmap(FACES*height,height, Bitmap.Config.ARGB_8888);
168
      Canvas canvas = new Canvas(result);
169
      Paint paint = new Paint();
170
      paint.setStyle(Paint.Style.FILL);
171

    
172
      for(int i=0; i<FACES; i++)
173
        {
174
        paint.setColor(FACE_COLORS[i]);
175
        canvas.drawRect(i*height,0,(i+1)*height,height,paint);
176
        canvas.drawBitmap(input,i*height,0,null);
177
        }
178

    
179
      return result;
180
      }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

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

    
190
      MeshBase[] meshes = new MeshTriangles[MESHES];
191
      for(int i=0; i<MESHES; i++) meshes[i] = new MeshTriangles(10);
192

    
193
      MatrixEffect[] effects0 = new MatrixEffect[3];
194
      effects0[0] = new MatrixEffectScale( new Static3D(1,SQ3/2,1) );
195
      effects0[1] = new MatrixEffectRotate( new Static1D(90), new Static3D(1,0,0), new Static3D(0,0,0) );
196
      effects0[2] = new MatrixEffectMove( new Static3D(0,-SQ3/6,SQ3/12) );
197

    
198
      meshes[0].apply(effects0);
199

    
200
      Static1D angle = new Static1D(angleFaces);
201
      Static3D axis  = new Static3D(-1,0,0);
202
      Static3D center= new Static3D(0,-SQ3/6,-SQ3/6);
203

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

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

    
216
      axis.set2(SQ3/2);
217
      meshes[3].apply(effects1);
218

    
219
      Static4D[] textureMaps = new Static4D[MESHES];
220

    
221
      for(int i=0; i<MESHES; i++) textureMaps[i] = new Static4D(i*0.25f,0.0f,0.25f,0.866f);
222

    
223
      MeshBase result = new MeshJoined(meshes);
224
      result.setTextureMap(textureMaps);
225

    
226
      return result;
227
      }
228
}
(2-2/3)