Project

General

Profile

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

examples / src / main / java / org / distorted / examples / flag / FlagRenderer.java @ 100e2da7

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 807775b9 Leszek Koltunski
import org.distorted.library.DistortedCubes;
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 76939f96 Leszek Koltunski
import org.distorted.library.type.Static2D;
36 175f355d Leszek Koltunski
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
    private DistortedObject mObject;
52
    private int mObjWidth, mObjHeight;
53
    private DynamicQuat mQuatInt1, mQuatInt2;
54
55
    Static4D mQuat1, mQuat2;
56
57
    int mScreenMin;
58
    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61
    FlagRenderer(GLSurfaceView v)
62
      {
63
      mView = v;
64
65 807775b9 Leszek Koltunski
      mObject = new DistortedCubes(50,30,10,false);
66 175f355d Leszek Koltunski
67
      mObjWidth = mObject.getWidth();
68
      mObjHeight= mObject.getHeight();
69
70 76939f96 Leszek Koltunski
      Static2D mWaveCenter = new Static2D(mObjWidth, mObjHeight/2);  // middle of the right edge
71
      Static4D mWaveRegion = new Static4D(0,0,mObjWidth,mObjWidth);
72
73
      Dynamic5D mWaveDyn = new Dynamic5D(1000,0.0f);
74
      Static5D  mWaveSta1= new Static5D(50,100,-180,90,0);
75
      Static5D  mWaveSta2= new Static5D(50,100,+180,90,0);
76
77
      mWaveDyn.add(mWaveSta1);
78
      mWaveDyn.add(mWaveSta2);
79
      mWaveDyn.setMode(Dynamic.MODE_JUMP);
80
81 ef0fd531 Leszek Koltunski
      //mWaveDyn.setNoise( new Static5D(0.1f, 0.1f, 0.1f, 0.1f, 0.1f) );
82 2010cf53 Leszek Koltunski
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
      mObject.wave(mWaveDyn, mWaveCenter, mWaveRegion);
93 175f355d Leszek Koltunski
      }
94
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96 100e2da7 Leszek Koltunski
97
    public static void setAmplitude(int a)
98
      {
99
100
      }
101
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104
    public static void setLength(int l)
105
      {
106
107
      }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111
    public static void setAngleA(int a)
112
      {
113
114
      }
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118
    public static void setAngleB(int b)
119
      {
120
121
      }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125
    public static void setNoise(Static5D noise)
126
      {
127
128
      }
129
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
132 175f355d Leszek Koltunski
    public void onDrawFrame(GL10 glUnused) 
133
      {
134
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
135
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
136
      
137
      mObject.draw(System.currentTimeMillis());
138
      }
139
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
    
142
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
143
      {
144
      mScreenMin = width<height ? width:height;
145
    	
146
      mObject.abortEffects(EffectTypes.MATRIX);
147
      float factor;
148
149
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
150
        {
151
        factor = (0.8f*height)/mObjHeight;
152
        }
153
      else
154
        {
155
        factor = (0.8f*width)/mObjWidth;
156
        }
157
158
      mObject.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
159
      mObject.scale(factor);
160
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
161
162
      mObject.quaternion(mQuatInt1, center);
163
      mObject.quaternion(mQuatInt2, center);
164
       
165
      Distorted.onSurfaceChanged(width, height); 
166
      }
167
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
    
170
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
171
      {
172
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
173
      Bitmap bitmap;
174
        
175
      try 
176
        {
177
        bitmap = BitmapFactory.decodeStream(is);
178
        } 
179
      finally 
180
        {
181
        try 
182
          {
183
          is.close();
184
          } 
185
        catch(IOException e) { }
186
        }  
187
      
188
      mObject.setBitmap(bitmap);
189
      
190
      try
191
        {
192
        Distorted.onSurfaceCreated(mView.getContext());
193
        }
194
      catch(Exception ex)
195
        {
196 dba16c99 Leszek Koltunski
        android.util.Log.e("Flag", ex.getMessage() );
197 175f355d Leszek Koltunski
        }
198
      }
199
}