Project

General

Profile

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

examples / src / main / java / org / distorted / examples / flag / FlagRenderer.java @ 0de83d97

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.effect.MatrixEffectQuaternion;
28
import org.distorted.library.effect.MatrixEffectScale;
29
import org.distorted.library.effect.VertexEffectWave;
30
import org.distorted.library.main.Distorted;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedScreen;
33
import org.distorted.library.mesh.MeshCubes;
34
import org.distorted.library.main.DistortedTexture;
35
import org.distorted.library.type.Dynamic;
36
import org.distorted.library.type.Dynamic5D;
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 DistortedTexture mTexture;
53
    private DistortedScreen mScreen;
54
    private Dynamic5D mWaveDyn;
55
    private Static5D mWaveSta1, mWaveSta2;
56
    private int mObjWidth, mObjHeight;
57
    private Static3D mScale;
58

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

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

    
68
      mTexture = new DistortedTexture(500,300);
69

    
70
      mObjWidth = mTexture.getWidth();
71
      mObjHeight= mTexture.getHeight();
72

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

    
77
      mWaveDyn.add(mWaveSta1);
78
      mWaveDyn.add(mWaveSta2);
79
      mWaveDyn.setMode(Dynamic.MODE_JUMP);
80

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

    
84
      Static3D waveCenter = new Static3D(mObjWidth, mObjHeight/2, 0);  // middle of the right edge
85
      Static4D waveRegion = new Static4D(0,0,0,mObjWidth);
86

    
87
      DistortedEffects effects = new DistortedEffects();
88
      effects.apply( new VertexEffectWave(mWaveDyn, waveCenter, waveRegion) );
89

    
90
      mScale = new Static3D(1,1,1);
91
      Static3D center= new Static3D(0,0,0);
92

    
93
      effects.apply( new MatrixEffectScale(mScale));
94
      effects.apply( new MatrixEffectQuaternion(mQuat1, center) );
95
      effects.apply( new MatrixEffectQuaternion(mQuat2, center) );
96

    
97
      final int GRIDX = 50;
98
      final int GRIDY = 30;
99

    
100
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f      ,1.0f      );
101
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRIDX,1.0f      );
102
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f      ,1.0f/GRIDY);
103

    
104
      mScreen = new DistortedScreen();
105
      mScreen.attach(mTexture, effects, new MeshCubes(GRIDX,GRIDY,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB) );
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
    void setAmplitude(int a)
111
      {
112
      mWaveSta1.set1(a);
113
      mWaveSta2.set1(a);
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    void setLength(int l)
119
      {
120
      mWaveSta1.set2(l);
121
      mWaveSta2.set2(l);
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
    void setAngleA(int a)
127
      {
128
      mWaveSta1.set4(a);
129
      mWaveSta2.set4(a);
130
      }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
    void setAngleB(int b)
135
      {
136
      mWaveSta1.set5(b);
137
      mWaveSta2.set5(b);
138
      }
139

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

    
142
    void setNoise(Static5D noise)
143
      {
144
      mWaveDyn.setNoise(noise);
145
      }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
    public void onDrawFrame(GL10 glUnused) 
150
      {
151
      mScreen.render( System.currentTimeMillis() );
152
      }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
    
156
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
157
      {
158
      mScreenMin = width<height ? width:height;
159
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (0.8f*height)/mObjHeight : (0.8f*width)/mObjWidth;
160
      mScale.set(factor,factor,factor);
161
      mScreen.resize(width, height);
162
      }
163

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

    
186
      VertexEffectWave.enable();
187

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