Project

General

Profile

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

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

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