Project

General

Profile

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

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

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.type.DynamicQuat;
40
import org.distorted.library.type.Static1D;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43

    
44
import java.io.IOException;
45
import java.io.InputStream;
46

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class MeshJoinRenderer implements GLSurfaceView.Renderer
53
{
54
    private GLSurfaceView mView;
55
    private DistortedTexture mTexture;
56
    private DistortedScreen mScreen;
57
    private DistortedEffects mEffects;
58
    private Static3D mScale;
59
    private MeshBase mMesh;
60

    
61
    Static4D mQuat1, mQuat2;
62
    int mScreenMin;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
    MeshJoinRenderer(GLSurfaceView v)
67
      {
68
      mView = v;
69
      mScreen = new DistortedScreen();
70
      mScale= new Static3D(1,1,1);
71
      Static3D center=new Static3D(0,0,0);
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
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
   
90
    public void onDrawFrame(GL10 glUnused) 
91
      {
92
      mScreen.render( System.currentTimeMillis() );
93
      }
94

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

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
109
      {
110
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.girl);
111
      Bitmap bitmap;
112

    
113
      try
114
        {
115
        bitmap = BitmapFactory.decodeStream(is);
116
        }
117
      finally
118
        {
119
        try
120
          {
121
          is.close();
122
          }
123
        catch(IOException e) { }
124
        }
125

    
126
      if( mTexture==null ) mTexture = new DistortedTexture();
127
      mTexture.setTexture(bitmap);
128

    
129
      if( mMesh==null ) mMesh = createJoinedMesh();
130

    
131
      mScreen.detachAll();
132
      mScreen.attach(mTexture,mEffects,mMesh);
133

    
134
      try
135
        {
136
        DistortedLibrary.onCreate(mView.getContext());
137
        }
138
      catch(Exception ex)
139
        {
140
        android.util.Log.e("MeshJoin", ex.getMessage() );
141
        }
142
      }
143

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

    
146
    private MeshBase createJoinedMesh()
147
      {
148
      Static3D axis   = new Static3D(0,1,0);
149
      Static3D center = new Static3D(0,0,0);
150
      Static1D angle  = new Static1D(0);
151

    
152
      MatrixEffect[] effects = new MatrixEffect[2];
153
      effects[0] = new MatrixEffectMove(new Static3D(0,0,-0.5f));
154
      effects[1] = new MatrixEffectRotate( angle, axis, center );
155

    
156
      MeshBase[] meshes = new MeshQuad[4];
157

    
158
      for(int i=0; i<4; i++) meshes[i] = new MeshQuad();
159

    
160
      angle.set(0);
161
      meshes[0].apply(effects);
162
      angle.set(90);
163
      meshes[1].apply(effects);
164
      angle.set(180);
165
      meshes[2].apply(effects);
166
      angle.set(270);
167
      meshes[3].apply(effects);
168

    
169
      return new MeshJoined(meshes);
170
      }
171
}
(2-2/3)