Project

General

Profile

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

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

1 175f355d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
25 175f355d Leszek Koltunski
import android.opengl.GLSurfaceView;
26
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
30 d218d64e leszek
import org.distorted.library.DistortedScreen;
31 b01acdaf Leszek Koltunski
import org.distorted.library.MeshCubes;
32 40eef4b9 Leszek Koltunski
import org.distorted.library.DistortedTexture;
33 175f355d Leszek Koltunski
import org.distorted.library.EffectTypes;
34 76939f96 Leszek Koltunski
import org.distorted.library.type.Dynamic;
35
import org.distorted.library.type.Dynamic5D;
36 175f355d Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
37
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 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
53 40eef4b9 Leszek Koltunski
    private DistortedTexture mTexture;
54 d218d64e leszek
    private DistortedScreen mScreen;
55 175f355d Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
56 41d39cea Leszek Koltunski
    private Dynamic5D mWaveDyn;
57
    private Static5D mWaveSta1, mWaveSta2;
58 392e16fd Leszek Koltunski
    private int mObjWidth, mObjHeight;
59 175f355d Leszek Koltunski
60
    Static4D mQuat1, mQuat2;
61
    int mScreenMin;
62
    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
    FlagRenderer(GLSurfaceView v)
66
      {
67
      mView = v;
68
69 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
70 7451c98a Leszek Koltunski
      mTexture = new DistortedTexture(500,300);
71 175f355d Leszek Koltunski
72 40eef4b9 Leszek Koltunski
      mObjWidth = mTexture.getWidth();
73
      mObjHeight= mTexture.getHeight();
74 175f355d Leszek Koltunski
75 41d39cea Leszek Koltunski
      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 76939f96 Leszek Koltunski
79
      mWaveDyn.add(mWaveSta1);
80
      mWaveDyn.add(mWaveSta2);
81
      mWaveDyn.setMode(Dynamic.MODE_JUMP);
82
83 dba16c99 Leszek Koltunski
      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 175f355d Leszek Koltunski
86
      mQuatInt1 = new DynamicQuat(0,0.5f);
87
      mQuatInt2 = new DynamicQuat(0,0.5f);
88
89
      mQuatInt1.add(mQuat1);
90
      mQuatInt2.add(mQuat2);
91 76939f96 Leszek Koltunski
92 334c13fa Leszek Koltunski
      Static3D waveCenter = new Static3D(mObjWidth, mObjHeight/2, 0);  // middle of the right edge
93 41d39cea Leszek Koltunski
      Static4D waveRegion = new Static4D(0,0,mObjWidth,mObjWidth);
94
95 392e16fd Leszek Koltunski
      mEffects.wave(mWaveDyn, waveCenter, waveRegion);
96
97 d218d64e leszek
      mScreen = new DistortedScreen();
98 fe59d375 Leszek Koltunski
      mScreen.attach(mTexture,mEffects, new MeshCubes(50,30,false) );
99 175f355d Leszek Koltunski
      }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102 100e2da7 Leszek Koltunski
103 41d39cea Leszek Koltunski
    void setAmplitude(int a)
104 100e2da7 Leszek Koltunski
      {
105 41d39cea Leszek Koltunski
      mWaveSta1.set1(a);
106
      mWaveSta2.set1(a);
107 100e2da7 Leszek Koltunski
      }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111 41d39cea Leszek Koltunski
    void setLength(int l)
112 100e2da7 Leszek Koltunski
      {
113 41d39cea Leszek Koltunski
      mWaveSta1.set2(l);
114
      mWaveSta2.set2(l);
115 100e2da7 Leszek Koltunski
      }
116
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119 41d39cea Leszek Koltunski
    void setAngleA(int a)
120 100e2da7 Leszek Koltunski
      {
121 41d39cea Leszek Koltunski
      mWaveSta1.set4(a);
122
      mWaveSta2.set4(a);
123 100e2da7 Leszek Koltunski
      }
124
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
127 41d39cea Leszek Koltunski
    void setAngleB(int b)
128 100e2da7 Leszek Koltunski
      {
129 41d39cea Leszek Koltunski
      mWaveSta1.set5(b);
130
      mWaveSta2.set5(b);
131 100e2da7 Leszek Koltunski
      }
132
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
135 41d39cea Leszek Koltunski
    void setNoise(Static5D noise)
136 100e2da7 Leszek Koltunski
      {
137 41d39cea Leszek Koltunski
      mWaveDyn.setNoise(noise);
138 100e2da7 Leszek Koltunski
      }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
142 175f355d Leszek Koltunski
    public void onDrawFrame(GL10 glUnused) 
143
      {
144 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
145 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
146 175f355d Leszek Koltunski
      }
147
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
151
      {
152
      mScreenMin = width<height ? width:height;
153
    	
154 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
155 175f355d Leszek Koltunski
      float factor;
156
157
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
158
        {
159
        factor = (0.8f*height)/mObjHeight;
160
        }
161
      else
162
        {
163
        factor = (0.8f*width)/mObjWidth;
164
        }
165
166 392e16fd Leszek Koltunski
      mEffects.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
167
      mEffects.scale(factor);
168 175f355d Leszek Koltunski
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
169
170 392e16fd Leszek Koltunski
      mEffects.quaternion(mQuatInt1, center);
171
      mEffects.quaternion(mQuatInt2, center);
172 175f355d Leszek Koltunski
       
173 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
174 175f355d Leszek Koltunski
      }
175
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
    
178
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
179
      {
180 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
181 e7a4ef16 Leszek Koltunski
182 175f355d Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
183
      Bitmap bitmap;
184
        
185
      try 
186
        {
187
        bitmap = BitmapFactory.decodeStream(is);
188
        } 
189
      finally 
190
        {
191
        try 
192
          {
193
          is.close();
194
          } 
195
        catch(IOException e) { }
196
        }  
197
      
198 40eef4b9 Leszek Koltunski
      mTexture.setTexture(bitmap);
199 175f355d Leszek Koltunski
      
200
      try
201
        {
202 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
203 175f355d Leszek Koltunski
        }
204
      catch(Exception ex)
205
        {
206 dba16c99 Leszek Koltunski
        android.util.Log.e("Flag", ex.getMessage() );
207 175f355d Leszek Koltunski
        }
208
      }
209
}