Project

General

Profile

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

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

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.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.effect.MatrixEffect;
28
import org.distorted.library.effect.MatrixEffectMove;
29
import org.distorted.library.effect.MatrixEffectQuaternion;
30
import org.distorted.library.effect.MatrixEffectRotate;
31
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
import org.distorted.library.mesh.MeshJoined;
38
import org.distorted.library.mesh.MeshQuad;
39
import org.distorted.library.mesh.MeshRectangles;
40
import org.distorted.library.mesh.MeshSphere;
41
import org.distorted.library.mesh.MeshTriangles;
42
import org.distorted.library.type.DynamicQuat;
43
import org.distorted.library.type.Static1D;
44
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
    private Static3D mScale;
62
    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
      Static3D center=new Static3D(0,0,0);
75

    
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
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, center) );
87
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, center) );
88
      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
      final float SCALE = 0.6f;
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
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
114
      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
      mMesh.setShowNormals(true);
135

    
136
      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
      MeshBase[] meshes = new MeshBase[2];
154
      meshes[0] = new MeshSphere(5);
155
      meshes[1] = new MeshTriangles(5);
156

    
157
      MatrixEffect[] effects0 = new MatrixEffect[2];
158
      effects0[0] = new MatrixEffectScale( new Static3D(0.5f, 1.0f, 0.5f) );
159
      effects0[1] = new MatrixEffectMove( new Static3D(+0.25f,0,0));
160

    
161
      meshes[0].apply(effects0);
162

    
163
      MatrixEffect[] effects1 = new MatrixEffect[2];
164
      effects1[0] = new MatrixEffectScale( new Static3D(0.5f, 1.0f, 0.5f) );
165
      effects1[1] = new MatrixEffectMove( new Static3D(-0.25f,0,0));
166

    
167
      meshes[1].apply(effects1);
168
      /*
169
      final int MESHES=6;
170

    
171
      Static3D axisY  = new Static3D(0,1,0);
172
      Static3D axisX  = new Static3D(1,0,0);
173
      Static3D center = new Static3D(0,0,0);
174
      Static1D angle  = new Static1D(0);
175

    
176
      MatrixEffect[] effectsY = new MatrixEffect[2];
177
      effectsY[0] = new MatrixEffectMove(new Static3D(0,0,+0.5f));
178
      effectsY[1] = new MatrixEffectRotate( angle, axisY, center );
179

    
180
      MeshBase[] meshes = new MeshRectangles[MESHES];
181
      for(int i=0; i<MESHES; i++) meshes[i] = new MeshRectangles(5,5);
182

    
183
      angle.set(0);
184
      meshes[0].apply(effectsY);
185
      angle.set(90);
186
      meshes[1].apply(effectsY);
187
      angle.set(180);
188
      meshes[2].apply(effectsY);
189
      angle.set(270);
190
      meshes[3].apply(effectsY);
191

    
192
      MatrixEffect[] effectsX = new MatrixEffect[2];
193
      effectsX[0] = new MatrixEffectMove(new Static3D(0,0,+0.5f));
194
      effectsX[1] = new MatrixEffectRotate( angle, axisX, center );
195

    
196
      angle.set( 90);
197
      meshes[4].apply(effectsX);
198
      angle.set(-90);
199
      meshes[5].apply(effectsX);
200
*/
201
      return new MeshJoined(meshes);
202
      }
203
}
(2-2/3)