Project

General

Profile

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

examples / src / main / java / org / distorted / examples / flag / FlagRenderer.java @ 01782e85

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

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

    
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
    private DistortedEffects mEffects;
53
    private DistortedTexture mTexture;
54
    private DistortedScreen mScreen;
55
    private DynamicQuat mQuatInt1, mQuatInt2;
56
    private Dynamic5D mWaveDyn;
57
    private Static5D mWaveSta1, mWaveSta2;
58
    private int mObjWidth, mObjHeight;
59

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

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

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

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

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

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

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

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

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

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

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

    
97
      mScreen = new DistortedScreen(mView);
98
      mScreen.attach(mTexture,mEffects, new MeshCubes(50,30,1) );
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

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

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

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

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

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

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

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

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

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

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
    
149
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
150
      {
151
      mScreenMin = width<height ? width:height;
152
    	
153
      mEffects.abortEffects(EffectTypes.MATRIX);
154
      float factor;
155

    
156
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
157
        {
158
        factor = (0.8f*height)/mObjHeight;
159
        }
160
      else
161
        {
162
        factor = (0.8f*width)/mObjWidth;
163
        }
164

    
165
      mEffects.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
166
      mEffects.scale(factor);
167
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
168

    
169
      mEffects.quaternion(mQuatInt1, center);
170
      mEffects.quaternion(mQuatInt2, center);
171
       
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
      DistortedEffects.enableEffect(EffectNames.WAVE);
198

    
199
      try
200
        {
201
        Distorted.onCreate(mView.getContext());
202
        }
203
      catch(Exception ex)
204
        {
205
        android.util.Log.e("Flag", ex.getMessage() );
206
        }
207
      }
208
}
(2-2/3)