Project

General

Profile

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

examples / src / main / java / org / distorted / examples / flag / FlagRenderer.java @ 39a0d81b

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.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLES30;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedEffects;
30
import org.distorted.library.DistortedScreen;
31
import org.distorted.library.EffectNames;
32
import org.distorted.library.MeshCubes;
33
import org.distorted.library.DistortedTexture;
34
import org.distorted.library.EffectTypes;
35
import org.distorted.library.type.Dynamic;
36
import org.distorted.library.type.Dynamic5D;
37
import org.distorted.library.type.DynamicQuat;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40
import org.distorted.library.type.Static5D;
41

    
42
import java.io.IOException;
43
import java.io.InputStream;
44

    
45
import javax.microedition.khronos.egl.EGLConfig;
46
import javax.microedition.khronos.opengles.GL10;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class FlagRenderer implements GLSurfaceView.Renderer
51
{
52
    private GLSurfaceView mView;
53
    private DistortedEffects mEffects;
54
    private DistortedTexture mTexture;
55
    private DistortedScreen mScreen;
56
    private DynamicQuat mQuatInt1, mQuatInt2;
57
    private Dynamic5D mWaveDyn;
58
    private Static5D mWaveSta1, mWaveSta2;
59
    private int mObjWidth, mObjHeight;
60

    
61
    Static4D mQuat1, mQuat2;
62
    int mScreenMin;
63
    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
    FlagRenderer(GLSurfaceView v)
67
      {
68
      mView = v;
69

    
70
      mEffects = new DistortedEffects();
71
      mTexture = new DistortedTexture(500,300);
72

    
73
      mObjWidth = mTexture.getWidth();
74
      mObjHeight= mTexture.getHeight();
75

    
76
      mWaveDyn = new Dynamic5D(1000,0.0f);
77
      mWaveSta1= new Static5D(0,0,-180,0,0);  // all other values besides the
78
      mWaveSta2= new Static5D(0,0,+180,0,0);  // fourth will be set from the UI
79

    
80
      mWaveDyn.add(mWaveSta1);
81
      mWaveDyn.add(mWaveSta2);
82
      mWaveDyn.setMode(Dynamic.MODE_JUMP);
83

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

    
87
      mQuatInt1 = new DynamicQuat(0,0.5f);
88
      mQuatInt2 = new DynamicQuat(0,0.5f);
89

    
90
      mQuatInt1.add(mQuat1);
91
      mQuatInt2.add(mQuat2);
92

    
93
      Static3D waveCenter = new Static3D(mObjWidth, mObjHeight/2, 0);  // middle of the right edge
94
      Static4D waveRegion = new Static4D(0,0,mObjWidth,mObjWidth);
95

    
96
      mEffects.wave(mWaveDyn, waveCenter, waveRegion);
97

    
98
      mScreen = new DistortedScreen();
99
      mScreen.attach(mTexture,mEffects, new MeshCubes(50,30,false) );
100
      }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
    void setAmplitude(int a)
105
      {
106
      mWaveSta1.set1(a);
107
      mWaveSta2.set1(a);
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
    void setLength(int l)
113
      {
114
      mWaveSta1.set2(l);
115
      mWaveSta2.set2(l);
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
    void setAngleA(int a)
121
      {
122
      mWaveSta1.set4(a);
123
      mWaveSta2.set4(a);
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
    void setAngleB(int b)
129
      {
130
      mWaveSta1.set5(b);
131
      mWaveSta2.set5(b);
132
      }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
    void setNoise(Static5D noise)
137
      {
138
      mWaveDyn.setNoise(noise);
139
      }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
    public void onDrawFrame(GL10 glUnused) 
144
      {
145
      mScreen.render( System.currentTimeMillis() );
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
151
      {
152
      mScreenMin = width<height ? width:height;
153

    
154
    	float q= ((float)width/height)*((float)mObjHeight/mObjWidth);
155
      float scale = 0.8f;
156
      Static3D center = new Static3D(0,0,0);
157

    
158
      mEffects.abortEffects(EffectTypes.MATRIX);
159
      mEffects.quaternion(mQuatInt1, center);
160
      mEffects.quaternion(mQuatInt2, center);
161
      mEffects.scale(  q<1 ? (new Static3D(scale,scale*q,scale)) : (new Static3D(scale/q,scale,scale/q)) );
162

    
163
      mScreen.resize(width, height);
164
      }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
    
168
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
169
      {
170
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
171
      Bitmap bitmap;
172
        
173
      try 
174
        {
175
        bitmap = BitmapFactory.decodeStream(is);
176
        } 
177
      finally 
178
        {
179
        try 
180
          {
181
          is.close();
182
          } 
183
        catch(IOException e) { }
184
        }  
185
      
186
      mTexture.setTexture(bitmap);
187

    
188
      DistortedEffects.enableEffect(EffectNames.WAVE);
189

    
190
      try
191
        {
192
        Distorted.onCreate(mView.getContext());
193
        }
194
      catch(Exception ex)
195
        {
196
        android.util.Log.e("Flag", ex.getMessage() );
197
        }
198
      }
199
}
(2-2/3)