Project

General

Profile

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

examples / src / main / java / org / distorted / examples / flag / FlagRenderer.java @ 76f9798b

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