Project

General

Profile

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

examples / src / main / java / org / distorted / examples / meshjoin / MeshJoinRenderer.java @ 23c35a5d

1 508ee98f 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.meshjoin;
21
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25
26
import org.distorted.examples.R;
27 98b6b975 Leszek Koltunski
import org.distorted.library.effect.MatrixEffect;
28 508ee98f Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectQuaternion;
30 37320052 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectRotate;
31 508ee98f Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedLibrary;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.mesh.MeshBase;
37 37320052 Leszek Koltunski
import org.distorted.library.mesh.MeshJoined;
38 508ee98f Leszek Koltunski
import org.distorted.library.mesh.MeshQuad;
39 b3090c52 Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
40
import org.distorted.library.mesh.MeshSphere;
41
import org.distorted.library.mesh.MeshTriangles;
42 508ee98f Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
43 37320052 Leszek Koltunski
import org.distorted.library.type.Static1D;
44 508ee98f Leszek Koltunski
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46
47
import java.io.IOException;
48
import java.io.InputStream;
49
50
import javax.microedition.khronos.egl.EGLConfig;
51
import javax.microedition.khronos.opengles.GL10;
52
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55
class MeshJoinRenderer implements GLSurfaceView.Renderer
56
{
57
    private GLSurfaceView mView;
58
    private DistortedTexture mTexture;
59
    private DistortedScreen mScreen;
60
    private DistortedEffects mEffects;
61 ba9ae2c8 Leszek Koltunski
    private Static3D mScale;
62 508ee98f Leszek Koltunski
    private MeshBase mMesh;
63
64
    Static4D mQuat1, mQuat2;
65
    int mScreenMin;
66
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69
    MeshJoinRenderer(GLSurfaceView v)
70
      {
71
      mView = v;
72
      mScreen = new DistortedScreen();
73
      mScale= new Static3D(1,1,1);
74 ba9ae2c8 Leszek Koltunski
      Static3D center=new Static3D(0,0,0);
75 508ee98f Leszek Koltunski
76
      mQuat1 = new Static4D(0,0,0,1);  // unity
77
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
78
79
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
80
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
81
82
      quatInt1.add(mQuat1);
83
      quatInt2.add(mQuat2);
84
85
      mEffects = new DistortedEffects();
86 ba9ae2c8 Leszek Koltunski
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
87
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
88 508ee98f Leszek Koltunski
      mEffects.apply( new MatrixEffectScale(mScale));
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 b3090c52 Leszek Koltunski
      final float SCALE = 0.6f;
103 508ee98f Leszek Koltunski
      mScreenMin = width<height ? width:height;
104 37320052 Leszek Koltunski
      float factor = SCALE*mScreenMin;
105 508ee98f Leszek Koltunski
      mScale.set(factor,factor,factor);
106
      mScreen.resize(width, height);
107
      }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
    
111
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
112
      {
113 23c35a5d Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
114 508ee98f Leszek Koltunski
      Bitmap bitmap;
115
116
      try
117
        {
118
        bitmap = BitmapFactory.decodeStream(is);
119
        }
120
      finally
121
        {
122
        try
123
          {
124
          is.close();
125
          }
126
        catch(IOException e) { }
127
        }
128
129
      if( mTexture==null ) mTexture = new DistortedTexture();
130
      mTexture.setTexture(bitmap);
131
132
      if( mMesh==null ) mMesh = createJoinedMesh();
133
134 23c35a5d Leszek Koltunski
     // mMesh.setShowNormals(true);
135 b3090c52 Leszek Koltunski
136 508ee98f Leszek Koltunski
      mScreen.detachAll();
137
      mScreen.attach(mTexture,mEffects,mMesh);
138
139
      try
140
        {
141
        DistortedLibrary.onCreate(mView.getContext());
142
        }
143
      catch(Exception ex)
144
        {
145
        android.util.Log.e("MeshJoin", ex.getMessage() );
146
        }
147
      }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151
    private MeshBase createJoinedMesh()
152
      {
153 23c35a5d Leszek Koltunski
      /*
154
      MeshBase[] meshes = new MeshBase[2];
155
      meshes[0] = new MeshQuad();
156
      meshes[1] = new MeshQuad();
157
158
      MatrixEffect[] effects0 = new MatrixEffect[1];
159
      effects0[0] = new MatrixEffectMove( new Static3D(0,+0.6f,0));
160
161
      meshes[0].apply(effects0);
162
163
      MatrixEffect[] effects1 = new MatrixEffect[1];
164
      effects1[0] = new MatrixEffectMove( new Static3D(0,-0.6f,0));
165
166
      meshes[1].apply(effects1);
167
      */
168
      /*
169 b3090c52 Leszek Koltunski
      MeshBase[] meshes = new MeshBase[2];
170
      meshes[0] = new MeshSphere(5);
171
      meshes[1] = new MeshTriangles(5);
172
173
      MatrixEffect[] effects0 = new MatrixEffect[2];
174
      effects0[0] = new MatrixEffectScale( new Static3D(0.5f, 1.0f, 0.5f) );
175
      effects0[1] = new MatrixEffectMove( new Static3D(+0.25f,0,0));
176
177
      meshes[0].apply(effects0);
178
179
      MatrixEffect[] effects1 = new MatrixEffect[2];
180
      effects1[0] = new MatrixEffectScale( new Static3D(0.5f, 1.0f, 0.5f) );
181
      effects1[1] = new MatrixEffectMove( new Static3D(-0.25f,0,0));
182
183
      meshes[1].apply(effects1);
184 23c35a5d Leszek Koltunski
      */
185 b3090c52 Leszek Koltunski
      final int MESHES=6;
186
187
      Static3D axisY  = new Static3D(0,1,0);
188
      Static3D axisX  = new Static3D(1,0,0);
189 37320052 Leszek Koltunski
      Static3D center = new Static3D(0,0,0);
190
      Static1D angle  = new Static1D(0);
191
192 b3090c52 Leszek Koltunski
      MatrixEffect[] effectsY = new MatrixEffect[2];
193
      effectsY[0] = new MatrixEffectMove(new Static3D(0,0,+0.5f));
194
      effectsY[1] = new MatrixEffectRotate( angle, axisY, center );
195 37320052 Leszek Koltunski
196 b3090c52 Leszek Koltunski
      MeshBase[] meshes = new MeshRectangles[MESHES];
197
      for(int i=0; i<MESHES; i++) meshes[i] = new MeshRectangles(5,5);
198 98b6b975 Leszek Koltunski
199 37320052 Leszek Koltunski
      angle.set(0);
200 b3090c52 Leszek Koltunski
      meshes[0].apply(effectsY);
201 37320052 Leszek Koltunski
      angle.set(90);
202 b3090c52 Leszek Koltunski
      meshes[1].apply(effectsY);
203 37320052 Leszek Koltunski
      angle.set(180);
204 b3090c52 Leszek Koltunski
      meshes[2].apply(effectsY);
205 37320052 Leszek Koltunski
      angle.set(270);
206 b3090c52 Leszek Koltunski
      meshes[3].apply(effectsY);
207
208
      MatrixEffect[] effectsX = new MatrixEffect[2];
209
      effectsX[0] = new MatrixEffectMove(new Static3D(0,0,+0.5f));
210
      effectsX[1] = new MatrixEffectRotate( angle, axisX, center );
211 98b6b975 Leszek Koltunski
212 b3090c52 Leszek Koltunski
      angle.set( 90);
213
      meshes[4].apply(effectsX);
214
      angle.set(-90);
215
      meshes[5].apply(effectsX);
216 23c35a5d Leszek Koltunski
217
      MeshJoined result = new MeshJoined(meshes);
218
219
      float[][] maps = new float[MESHES][4];
220
221
      maps[0][0] = 3.0f/8; maps[0][1] = 3.0f/8; maps[0][2] = 1.0f/8; maps[0][3] = 2.0f/8;
222
      maps[1][0] = 5.0f/8; maps[1][1] = 3.0f/8; maps[1][2] = 1.0f/8; maps[1][3] = 2.0f/8;
223
      maps[2][0] = 3.0f/8; maps[2][1] = 5.0f/8; maps[2][2] = 1.0f/8; maps[2][3] = 2.0f/8;
224
      maps[3][0] = 1.0f/8; maps[3][1] = 5.0f/8; maps[3][2] = 1.0f/8; maps[3][3] = 2.0f/8;
225
      maps[4][0] = 5.0f/8; maps[4][1] = 1.0f/8; maps[4][2] = 1.0f/8; maps[4][3] = 2.0f/8;
226
      maps[5][0] = 5.0f/8; maps[5][1] = 5.0f/8; maps[5][2] = 1.0f/8; maps[5][3] = 2.0f/8;
227
228
      result.setTextureMap(maps);
229
230
      maps[0][0] = 3.0f/8; maps[0][1] = 3.0f/8; maps[0][2] = 2.0f/8; maps[0][3] = 2.0f/8;
231
      maps[1][0] = 5.0f/8; maps[1][1] = 1.0f/8; maps[1][2] = 2.0f/8; maps[1][3] = 2.0f/8;
232
      maps[2][0] = 1.0f/8; maps[2][1] = 5.0f/8; maps[2][2] = 2.0f/8; maps[2][3] = 2.0f/8;
233
      maps[3][0] = 1.0f/8; maps[3][1] = 3.0f/8; maps[3][2] = 2.0f/8; maps[3][3] = 2.0f/8;
234
      maps[4][0] = 3.0f/8; maps[4][1] = 1.0f/8; maps[4][2] = 2.0f/8; maps[4][3] = 2.0f/8;
235
      maps[5][0] = 1.0f/8; maps[5][1] = 1.0f/8; maps[5][2] = 2.0f/8; maps[5][3] = 2.0f/8;
236
237
      result.setTextureMap(maps);
238
      return result;
239 508ee98f Leszek Koltunski
      }
240
}