Project

General

Profile

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

examples / src / main / java / org / distorted / examples / flag / FlagRenderer.java @ dc10a48d

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

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29

    
30
import org.distorted.examples.R;
31
import org.distorted.library.effect.MatrixEffectQuaternion;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectScale;
34
import org.distorted.library.effect.VertexEffectWave;
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.mesh.MeshCubes;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.type.Dynamic;
41
import org.distorted.library.type.Dynamic5D;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44
import org.distorted.library.type.Static5D;
45

    
46
import java.io.IOException;
47
import java.io.InputStream;
48

    
49
import javax.microedition.khronos.egl.EGLConfig;
50
import javax.microedition.khronos.opengles.GL10;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
class FlagRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
55
{
56
    private final GLSurfaceView mView;
57
    private final DistortedTexture mTexture;
58
    private final DistortedScreen mScreen;
59
    private final Dynamic5D mWaveDyn;
60
    private final Static5D mWaveSta1, mWaveSta2;
61
    private final int mObjWidth, mObjHeight, mObjDepth;
62
    private final Static3D mScale;
63
    private final Resources mResources;
64

    
65
    Static4D mQuat1, mQuat2;
66
    int mScreenMin;
67
    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
    FlagRenderer(GLSurfaceView v)
71
      {
72
      mView = v;
73
      mResources = v.getResources();
74

    
75
      final int GRIDX = 50;
76
      final int GRIDY = 30;
77

    
78
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f      ,1.0f      );
79
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRIDX,1.0f      );
80
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f      ,1.0f/GRIDY);
81

    
82
      mObjWidth = 500;
83
      mObjHeight= 300;
84
      mObjDepth =   5;
85

    
86
      MeshCubes mesh = new MeshCubes(GRIDX,GRIDY,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
87

    
88
      mTexture = new DistortedTexture();
89

    
90
      mWaveDyn = new Dynamic5D(1000,0.0f);
91
      mWaveSta1= new Static5D(0,0,-180,0,0);  // all other values besides the
92
      mWaveSta2= new Static5D(0,0,+180,0,0);  // fourth will be set from the UI
93

    
94
      mWaveDyn.add(mWaveSta1);
95
      mWaveDyn.add(mWaveSta2);
96
      mWaveDyn.setMode(Dynamic.MODE_JUMP);
97

    
98
      mQuat1 = new Static4D(           0,         0,           0,          1);  // unity quaternion
99
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);  // something semi-random that looks good
100

    
101
      Static3D waveCenter = new Static3D(mObjWidth/2, 0, 0);  // middle of the right edge
102
      Static4D waveRegion = new Static4D(0,0,0,mObjWidth);
103

    
104
      DistortedEffects effects = new DistortedEffects();
105
      effects.apply( new VertexEffectScale( new Static3D(mObjWidth,mObjHeight,mObjDepth) ) );
106
      effects.apply( new VertexEffectWave(mWaveDyn, waveCenter, waveRegion) );
107

    
108
      mScale = new Static3D(1,1,1);
109
      Static3D center= new Static3D(0,0,0);
110

    
111
      effects.apply( new MatrixEffectScale(mScale));
112
      effects.apply( new MatrixEffectQuaternion(mQuat2, center) );
113
      effects.apply( new MatrixEffectQuaternion(mQuat1, center) );
114

    
115
      mScreen = new DistortedScreen();
116
      mScreen.attach(mTexture, effects, mesh );
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
    void setAmplitude(int a)
122
      {
123
      mWaveSta1.set0(a);
124
      mWaveSta2.set0(a);
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
    void setLength(int l)
130
      {
131
      mWaveSta1.set1(l);
132
      mWaveSta2.set1(l);
133
      }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
    void setAngleA(int a)
138
      {
139
      mWaveSta1.set3(a);
140
      mWaveSta2.set3(a);
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
    void setAngleB(int b)
146
      {
147
      mWaveSta1.set4(b);
148
      mWaveSta2.set4(b);
149
      }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
    void setNoise(Static5D noise)
154
      {
155
      mWaveDyn.setNoise(noise);
156
      }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
    public void onDrawFrame(GL10 glUnused) 
161
      {
162
      mScreen.render( System.currentTimeMillis() );
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
    
167
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
168
      {
169
      mScreenMin = width<height ? width:height;
170
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (0.8f*height)/mObjHeight : (0.8f*width)/mObjWidth;
171
      mScale.set(factor,factor,factor);
172
      mScreen.resize(width, height);
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176
    
177
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
178
      {
179
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
180
      Bitmap bitmap;
181
        
182
      try 
183
        {
184
        bitmap = BitmapFactory.decodeStream(is);
185
        } 
186
      finally 
187
        {
188
        try 
189
          {
190
          is.close();
191
          } 
192
        catch(IOException e) { }
193
        }  
194
      
195
      mTexture.setTexture(bitmap);
196

    
197
      VertexEffectScale.enable();
198
      VertexEffectWave.enable();
199

    
200
      DistortedLibrary.onSurfaceCreated(this);
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
    public void distortedException(Exception ex)
206
      {
207
      android.util.Log.e("Flag", ex.getMessage() );
208
      }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
    public int openGlVersion()
213
      {
214
      Context context = mView.getContext();
215
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
216
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
217
      int glESversion = configurationInfo.reqGlEsVersion;
218
      int major = glESversion >> 16;
219
      int minor = glESversion & 0xff;
220

    
221
      return 100*major + 10*minor;
222
      }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
    public InputStream localFile(int fileID)
227
      {
228
      return mResources.openRawResource(fileID);
229
      }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
    public void logMessage(String message)
234
      {
235
      android.util.Log.e("Flag", message );
236
      }
237
}
(2-2/3)