Project

General

Profile

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

examples / src / main / java / org / distorted / examples / flag / FlagRenderer.java @ 10b7e588

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