Project

General

Profile

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

examples / src / main / java / org / distorted / examples / flag / FlagRenderer.java @ 698ad0a8

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

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

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

    
69
      final int GRIDX = 50;
70
      final int GRIDY = 30;
71

    
72
      final Static4D mapFB = new Static4D(0.0f,0.0f,1.0f      ,1.0f      );
73
      final Static4D mapLR = new Static4D(0.0f,0.0f,1.0f/GRIDX,1.0f      );
74
      final Static4D mapTB = new Static4D(0.0f,0.0f,1.0f      ,1.0f/GRIDY);
75

    
76
      mObjWidth = 500;
77
      mObjHeight= 300;
78
      mObjDepth =   5;
79

    
80
      MeshCubes mesh = new MeshCubes(GRIDX,GRIDY,1, mapFB, mapFB, mapLR, mapLR, mapTB, mapTB);
81
      mesh.setStretch(mObjWidth,mObjHeight,mObjDepth);
82

    
83
      mTexture = new DistortedTexture();
84

    
85
      mWaveDyn = new Dynamic5D(1000,0.0f);
86
      mWaveSta1= new Static5D(0,0,-180,0,0);  // all other values besides the
87
      mWaveSta2= new Static5D(0,0,+180,0,0);  // fourth will be set from the UI
88

    
89
      mWaveDyn.add(mWaveSta1);
90
      mWaveDyn.add(mWaveSta2);
91
      mWaveDyn.setMode(Dynamic.MODE_JUMP);
92

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

    
96
      Static3D waveCenter = new Static3D(mObjWidth, mObjHeight/2, mObjDepth/2);  // middle of the right edge
97
      Static4D waveRegion = new Static4D(0,0,0,mObjWidth);
98

    
99
      DistortedEffects effects = new DistortedEffects();
100
      effects.apply( new VertexEffectWave(mWaveDyn, waveCenter, waveRegion) );
101

    
102
      mMove  = new Static3D(0,0,0);
103
      mScale = new Static3D(1,1,1);
104
      mCenter= new Static3D(0,0,0);
105

    
106
      effects.apply( new MatrixEffectQuaternion(mQuat2, mCenter) );
107
      effects.apply( new MatrixEffectQuaternion(mQuat1, mCenter) );
108
      effects.apply( new MatrixEffectScale(mScale));
109
      effects.apply( new MatrixEffectMove(mMove));
110

    
111

    
112
      mScreen = new DistortedScreen();
113
      mScreen.attach(mTexture, effects, mesh );
114
      }
115

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

    
118
    void setAmplitude(int a)
119
      {
120
      mWaveSta1.set0(a);
121
      mWaveSta2.set0(a);
122
      }
123

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

    
126
    void setLength(int l)
127
      {
128
      mWaveSta1.set1(l);
129
      mWaveSta2.set1(l);
130
      }
131

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

    
134
    void setAngleA(int a)
135
      {
136
      mWaveSta1.set3(a);
137
      mWaveSta2.set3(a);
138
      }
139

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

    
142
    void setAngleB(int b)
143
      {
144
      mWaveSta1.set4(b);
145
      mWaveSta2.set4(b);
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
    void setNoise(Static5D noise)
151
      {
152
      mWaveDyn.setNoise(noise);
153
      }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
    public void onDrawFrame(GL10 glUnused) 
158
      {
159
      mScreen.render( System.currentTimeMillis() );
160
      }
161

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

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

    
196
      VertexEffectWave.enable();
197

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