Project

General

Profile

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

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

1 175f355d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 175f355d Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 175f355d Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 175f355d Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 175f355d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.flag;
21
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25
26
import org.distorted.examples.R;
27 2890c5df Leszek Koltunski
import org.distorted.library.effect.MatrixEffectQuaternion;
28
import org.distorted.library.effect.MatrixEffectScale;
29
import org.distorted.library.effect.VertexEffectWave;
30 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
31 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedScreen;
33 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
34 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
35 76939f96 Leszek Koltunski
import org.distorted.library.type.Dynamic;
36
import org.distorted.library.type.Dynamic5D;
37 175f355d Leszek Koltunski
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39 76939f96 Leszek Koltunski
import org.distorted.library.type.Static5D;
40 175f355d Leszek Koltunski
41
import java.io.IOException;
42
import java.io.InputStream;
43
44
import javax.microedition.khronos.egl.EGLConfig;
45
import javax.microedition.khronos.opengles.GL10;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
class FlagRenderer implements GLSurfaceView.Renderer
50
{
51
    private GLSurfaceView mView;
52 40eef4b9 Leszek Koltunski
    private DistortedTexture mTexture;
53 d218d64e leszek
    private DistortedScreen mScreen;
54 41d39cea Leszek Koltunski
    private Dynamic5D mWaveDyn;
55
    private Static5D mWaveSta1, mWaveSta2;
56 698ad0a8 Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
57 8664ea2e Leszek Koltunski
    private Static3D mScale;
58 175f355d Leszek Koltunski
59
    Static4D mQuat1, mQuat2;
60
    int mScreenMin;
61
    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
    FlagRenderer(GLSurfaceView v)
65
      {
66
      mView = v;
67
68 687263cc Leszek Koltunski
      final int GRIDX = 50;
69
      final int GRIDY = 30;
70
71
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f      ,1.0f      );
72
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRIDX,1.0f      );
73
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f      ,1.0f/GRIDY);
74
75 698ad0a8 Leszek Koltunski
      mObjWidth = 500;
76
      mObjHeight= 300;
77
      mObjDepth =   5;
78
79 687263cc Leszek Koltunski
      MeshCubes mesh = new MeshCubes(GRIDX,GRIDY,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
80 698ad0a8 Leszek Koltunski
      mesh.setStretch(mObjWidth,mObjHeight,mObjDepth);
81 175f355d Leszek Koltunski
82 687263cc Leszek Koltunski
      mTexture = new DistortedTexture();
83
84 41d39cea Leszek Koltunski
      mWaveDyn = new Dynamic5D(1000,0.0f);
85
      mWaveSta1= new Static5D(0,0,-180,0,0);  // all other values besides the
86
      mWaveSta2= new Static5D(0,0,+180,0,0);  // fourth will be set from the UI
87 76939f96 Leszek Koltunski
88
      mWaveDyn.add(mWaveSta1);
89
      mWaveDyn.add(mWaveSta2);
90
      mWaveDyn.setMode(Dynamic.MODE_JUMP);
91
92 dba16c99 Leszek Koltunski
      mQuat1 = new Static4D(           0,         0,           0,          1);  // unity quaternion
93
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);  // something semi-random that looks good
94 175f355d Leszek Koltunski
95 e50f49b6 Leszek Koltunski
      Static3D waveCenter = new Static3D(mObjWidth/2, 0, 0);  // middle of the right edge
96 9e7b6dbd Leszek Koltunski
      Static4D waveRegion = new Static4D(0,0,0,mObjWidth);
97 41d39cea Leszek Koltunski
98 698ad0a8 Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
99 a4d59c0b Leszek Koltunski
      effects.apply( new VertexEffectWave(mWaveDyn, waveCenter, waveRegion) );
100 2890c5df Leszek Koltunski
101
      mScale = new Static3D(1,1,1);
102 0de83d97 Leszek Koltunski
      Static3D center= new Static3D(0,0,0);
103 2890c5df Leszek Koltunski
104 dae661e9 Leszek Koltunski
      effects.apply( new MatrixEffectScale(mScale));
105 0de83d97 Leszek Koltunski
      effects.apply( new MatrixEffectQuaternion(mQuat1, center) );
106
      effects.apply( new MatrixEffectQuaternion(mQuat2, center) );
107 5b4a2d76 Leszek Koltunski
108 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
109 687263cc Leszek Koltunski
      mScreen.attach(mTexture, effects, mesh );
110 175f355d Leszek Koltunski
      }
111
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113 100e2da7 Leszek Koltunski
114 41d39cea Leszek Koltunski
    void setAmplitude(int a)
115 100e2da7 Leszek Koltunski
      {
116 bcbd5b45 Leszek Koltunski
      mWaveSta1.set0(a);
117
      mWaveSta2.set0(a);
118 100e2da7 Leszek Koltunski
      }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
122 41d39cea Leszek Koltunski
    void setLength(int l)
123 100e2da7 Leszek Koltunski
      {
124 bcbd5b45 Leszek Koltunski
      mWaveSta1.set1(l);
125
      mWaveSta2.set1(l);
126 100e2da7 Leszek Koltunski
      }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130 41d39cea Leszek Koltunski
    void setAngleA(int a)
131 100e2da7 Leszek Koltunski
      {
132 bcbd5b45 Leszek Koltunski
      mWaveSta1.set3(a);
133
      mWaveSta2.set3(a);
134 100e2da7 Leszek Koltunski
      }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
138 41d39cea Leszek Koltunski
    void setAngleB(int b)
139 100e2da7 Leszek Koltunski
      {
140 bcbd5b45 Leszek Koltunski
      mWaveSta1.set4(b);
141
      mWaveSta2.set4(b);
142 100e2da7 Leszek Koltunski
      }
143
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
146 41d39cea Leszek Koltunski
    void setNoise(Static5D noise)
147 100e2da7 Leszek Koltunski
      {
148 41d39cea Leszek Koltunski
      mWaveDyn.setNoise(noise);
149 100e2da7 Leszek Koltunski
      }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153 175f355d Leszek Koltunski
    public void onDrawFrame(GL10 glUnused) 
154
      {
155 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
156 175f355d Leszek Koltunski
      }
157
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
    
160
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
161
      {
162
      mScreenMin = width<height ? width:height;
163 2890c5df Leszek Koltunski
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (0.8f*height)/mObjHeight : (0.8f*width)/mObjWidth;
164
      mScale.set(factor,factor,factor);
165 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
166 175f355d Leszek Koltunski
      }
167
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
    
170
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
171
      {
172
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
173
      Bitmap bitmap;
174
        
175
      try 
176
        {
177
        bitmap = BitmapFactory.decodeStream(is);
178
        } 
179
      finally 
180
        {
181
        try 
182
          {
183
          is.close();
184
          } 
185
        catch(IOException e) { }
186
        }  
187
      
188 40eef4b9 Leszek Koltunski
      mTexture.setTexture(bitmap);
189 6637d0f2 Leszek Koltunski
190 885b9cca leszek
      VertexEffectWave.enable();
191 6637d0f2 Leszek Koltunski
192 175f355d Leszek Koltunski
      try
193
        {
194 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
195 175f355d Leszek Koltunski
        }
196
      catch(Exception ex)
197
        {
198 dba16c99 Leszek Koltunski
        android.util.Log.e("Flag", ex.getMessage() );
199 175f355d Leszek Koltunski
        }
200
      }
201
}