Project

General

Profile

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

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

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.GLSurfaceView;
25
26
import org.distorted.examples.R;
27 2890c5df Leszek Koltunski
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 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedScreen;
34 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
35 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
36 76939f96 Leszek Koltunski
import org.distorted.library.type.Dynamic;
37
import org.distorted.library.type.Dynamic5D;
38 175f355d Leszek Koltunski
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 40eef4b9 Leszek Koltunski
    private DistortedTexture mTexture;
54 d218d64e leszek
    private DistortedScreen mScreen;
55 41d39cea Leszek Koltunski
    private Dynamic5D mWaveDyn;
56
    private Static5D mWaveSta1, mWaveSta2;
57 392e16fd Leszek Koltunski
    private int mObjWidth, mObjHeight;
58 2890c5df Leszek Koltunski
    private Static3D mMove, mScale, mCenter;
59 175f355d Leszek Koltunski
60
    Static4D mQuat1, mQuat2;
61
    int mScreenMin;
62
    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
    FlagRenderer(GLSurfaceView v)
66
      {
67
      mView = v;
68
69 7451c98a Leszek Koltunski
      mTexture = new DistortedTexture(500,300);
70 175f355d Leszek Koltunski
71 40eef4b9 Leszek Koltunski
      mObjWidth = mTexture.getWidth();
72
      mObjHeight= mTexture.getHeight();
73 175f355d Leszek Koltunski
74 41d39cea Leszek Koltunski
      mWaveDyn = new Dynamic5D(1000,0.0f);
75
      mWaveSta1= new Static5D(0,0,-180,0,0);  // all other values besides the
76
      mWaveSta2= new Static5D(0,0,+180,0,0);  // fourth will be set from the UI
77 76939f96 Leszek Koltunski
78
      mWaveDyn.add(mWaveSta1);
79
      mWaveDyn.add(mWaveSta2);
80
      mWaveDyn.setMode(Dynamic.MODE_JUMP);
81
82 dba16c99 Leszek Koltunski
      mQuat1 = new Static4D(           0,         0,           0,          1);  // unity quaternion
83
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);  // something semi-random that looks good
84 175f355d Leszek Koltunski
85 334c13fa Leszek Koltunski
      Static3D waveCenter = new Static3D(mObjWidth, mObjHeight/2, 0);  // middle of the right edge
86 9e7b6dbd Leszek Koltunski
      Static4D waveRegion = new Static4D(0,0,0,mObjWidth);
87 41d39cea Leszek Koltunski
88 a4d59c0b Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
89
      effects.apply( new VertexEffectWave(mWaveDyn, waveCenter, waveRegion) );
90 2890c5df Leszek Koltunski
91
      mMove  = new Static3D(0,0,0);
92
      mScale = new Static3D(1,1,1);
93
      mCenter= new Static3D(0,0,0);
94
95 a4d59c0b Leszek Koltunski
      effects.apply( new MatrixEffectMove(mMove));
96
      effects.apply( new MatrixEffectScale(mScale));
97
      effects.apply( new MatrixEffectQuaternion(mQuat1, mCenter) );
98
      effects.apply( new MatrixEffectQuaternion(mQuat2, mCenter) );
99 392e16fd Leszek Koltunski
100 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
101 a4d59c0b Leszek Koltunski
      mScreen.attach(mTexture, effects, new MeshCubes(50,30,1) );
102 175f355d Leszek Koltunski
      }
103
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105 100e2da7 Leszek Koltunski
106 41d39cea Leszek Koltunski
    void setAmplitude(int a)
107 100e2da7 Leszek Koltunski
      {
108 41d39cea Leszek Koltunski
      mWaveSta1.set1(a);
109
      mWaveSta2.set1(a);
110 100e2da7 Leszek Koltunski
      }
111
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114 41d39cea Leszek Koltunski
    void setLength(int l)
115 100e2da7 Leszek Koltunski
      {
116 41d39cea Leszek Koltunski
      mWaveSta1.set2(l);
117
      mWaveSta2.set2(l);
118 100e2da7 Leszek Koltunski
      }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
122 41d39cea Leszek Koltunski
    void setAngleA(int a)
123 100e2da7 Leszek Koltunski
      {
124 41d39cea Leszek Koltunski
      mWaveSta1.set4(a);
125
      mWaveSta2.set4(a);
126 100e2da7 Leszek Koltunski
      }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130 41d39cea Leszek Koltunski
    void setAngleB(int b)
131 100e2da7 Leszek Koltunski
      {
132 41d39cea Leszek Koltunski
      mWaveSta1.set5(b);
133
      mWaveSta2.set5(b);
134 100e2da7 Leszek Koltunski
      }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
138 41d39cea Leszek Koltunski
    void setNoise(Static5D noise)
139 100e2da7 Leszek Koltunski
      {
140 41d39cea Leszek Koltunski
      mWaveDyn.setNoise(noise);
141 100e2da7 Leszek Koltunski
      }
142
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
145 175f355d Leszek Koltunski
    public void onDrawFrame(GL10 glUnused) 
146
      {
147 fe59d375 Leszek Koltunski
      mScreen.render( 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 2890c5df Leszek Koltunski
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (0.8f*height)/mObjHeight : (0.8f*width)/mObjWidth;
156
      mMove.set((width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0);
157
      mScale.set(factor,factor,factor);
158
      mCenter.set(mObjWidth/2,mObjHeight/2, 0);
159 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
160 175f355d Leszek Koltunski
      }
161
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
    
164
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
165
      {
166
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
167
      Bitmap bitmap;
168
        
169
      try 
170
        {
171
        bitmap = BitmapFactory.decodeStream(is);
172
        } 
173
      finally 
174
        {
175
        try 
176
          {
177
          is.close();
178
          } 
179
        catch(IOException e) { }
180
        }  
181
      
182 40eef4b9 Leszek Koltunski
      mTexture.setTexture(bitmap);
183 6637d0f2 Leszek Koltunski
184 885b9cca leszek
      VertexEffectWave.enable();
185 6637d0f2 Leszek Koltunski
186 175f355d Leszek Koltunski
      try
187
        {
188 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
189 175f355d Leszek Koltunski
        }
190
      catch(Exception ex)
191
        {
192 dba16c99 Leszek Koltunski
        android.util.Log.e("Flag", ex.getMessage() );
193 175f355d Leszek Koltunski
        }
194
      }
195
}