Project

General

Profile

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

examples / src / main / java / org / distorted / examples / flag / FlagRenderer.java @ 7451c98a

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 10b7e588 Leszek Koltunski
import org.distorted.library.GridCubes;
30 40eef4b9 Leszek Koltunski
import org.distorted.library.DistortedEffectQueues;
31
import org.distorted.library.DistortedTexture;
32 175f355d Leszek Koltunski
import org.distorted.library.EffectTypes;
33 76939f96 Leszek Koltunski
import org.distorted.library.type.Dynamic;
34
import org.distorted.library.type.Dynamic5D;
35 175f355d Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38 76939f96 Leszek Koltunski
import org.distorted.library.type.Static5D;
39 175f355d Leszek Koltunski
40
import java.io.IOException;
41
import java.io.InputStream;
42
43
import javax.microedition.khronos.egl.EGLConfig;
44
import javax.microedition.khronos.opengles.GL10;
45
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48
class FlagRenderer implements GLSurfaceView.Renderer
49
{
50
    private GLSurfaceView mView;
51 40eef4b9 Leszek Koltunski
    private DistortedEffectQueues mQueues;
52
    private DistortedTexture mTexture;
53 10b7e588 Leszek Koltunski
    private GridCubes mGrid;
54 175f355d Leszek Koltunski
    private int mObjWidth, mObjHeight;
55
    private DynamicQuat mQuatInt1, mQuatInt2;
56 41d39cea Leszek Koltunski
    private Dynamic5D mWaveDyn;
57
    private Static5D mWaveSta1, mWaveSta2;
58 175f355d Leszek Koltunski
59
    Static4D mQuat1, mQuat2;
60
    int mScreenMin;
61
    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64
    FlagRenderer(GLSurfaceView v)
65
      {
66
      mView = v;
67
68 40eef4b9 Leszek Koltunski
      mQueues  = new DistortedEffectQueues();
69
      mGrid    = new GridCubes(50,30,false);
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 40eef4b9 Leszek Koltunski
      mQueues.wave(mWaveDyn, waveCenter, waveRegion);
96 175f355d Leszek Koltunski
      }
97
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99 100e2da7 Leszek Koltunski
100 41d39cea Leszek Koltunski
    void setAmplitude(int a)
101 100e2da7 Leszek Koltunski
      {
102 41d39cea Leszek Koltunski
      mWaveSta1.set1(a);
103
      mWaveSta2.set1(a);
104 100e2da7 Leszek Koltunski
      }
105
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108 41d39cea Leszek Koltunski
    void setLength(int l)
109 100e2da7 Leszek Koltunski
      {
110 41d39cea Leszek Koltunski
      mWaveSta1.set2(l);
111
      mWaveSta2.set2(l);
112 100e2da7 Leszek Koltunski
      }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
116 41d39cea Leszek Koltunski
    void setAngleA(int a)
117 100e2da7 Leszek Koltunski
      {
118 41d39cea Leszek Koltunski
      mWaveSta1.set4(a);
119
      mWaveSta2.set4(a);
120 100e2da7 Leszek Koltunski
      }
121
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124 41d39cea Leszek Koltunski
    void setAngleB(int b)
125 100e2da7 Leszek Koltunski
      {
126 41d39cea Leszek Koltunski
      mWaveSta1.set5(b);
127
      mWaveSta2.set5(b);
128 100e2da7 Leszek Koltunski
      }
129
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
132 41d39cea Leszek Koltunski
    void setNoise(Static5D noise)
133 100e2da7 Leszek Koltunski
      {
134 41d39cea Leszek Koltunski
      mWaveDyn.setNoise(noise);
135 100e2da7 Leszek Koltunski
      }
136
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
139 175f355d Leszek Koltunski
    public void onDrawFrame(GL10 glUnused) 
140
      {
141
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
142 40eef4b9 Leszek Koltunski
      mQueues.draw(System.currentTimeMillis(), mTexture, mGrid);
143 175f355d Leszek Koltunski
      }
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
    
147
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
148
      {
149
      mScreenMin = width<height ? width:height;
150
    	
151 40eef4b9 Leszek Koltunski
      mQueues.abortEffects(EffectTypes.MATRIX);
152 175f355d Leszek Koltunski
      float factor;
153
154
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
155
        {
156
        factor = (0.8f*height)/mObjHeight;
157
        }
158
      else
159
        {
160
        factor = (0.8f*width)/mObjWidth;
161
        }
162
163 40eef4b9 Leszek Koltunski
      mQueues.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
164
      mQueues.scale(factor);
165 175f355d Leszek Koltunski
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
166
167 40eef4b9 Leszek Koltunski
      mQueues.quaternion(mQuatInt1, center);
168
      mQueues.quaternion(mQuatInt2, center);
169 175f355d Leszek Koltunski
       
170
      Distorted.onSurfaceChanged(width, height); 
171
      }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
    
175
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
176
      {
177 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
178
179 175f355d Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
180
      Bitmap bitmap;
181
        
182
      try 
183
        {
184
        bitmap = BitmapFactory.decodeStream(is);
185
        } 
186
      finally 
187
        {
188
        try 
189
          {
190
          is.close();
191
          } 
192
        catch(IOException e) { }
193
        }  
194
      
195 40eef4b9 Leszek Koltunski
      mTexture.setTexture(bitmap);
196 175f355d Leszek Koltunski
      
197
      try
198
        {
199
        Distorted.onSurfaceCreated(mView.getContext());
200
        }
201
      catch(Exception ex)
202
        {
203 dba16c99 Leszek Koltunski
        android.util.Log.e("Flag", ex.getMessage() );
204 175f355d Leszek Koltunski
        }
205
      }
206
}