Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import org.distorted.library.DistortedCubes;
30
import org.distorted.library.DistortedObject;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Dynamic;
33
import org.distorted.library.type.Dynamic5D;
34
import org.distorted.library.type.DynamicQuat;
35
import org.distorted.library.type.Static2D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38
import org.distorted.library.type.Static5D;
39

    
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
    private Dynamic5D mWaveDyn;
55
    private Static5D mWaveSta1, mWaveSta2;
56

    
57
    Static4D mQuat1, mQuat2;
58
    int mScreenMin;
59
    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
    FlagRenderer(GLSurfaceView v)
63
      {
64
      mView = v;
65

    
66
      mObject = new DistortedCubes(50,30,10,false);
67

    
68
      mObjWidth = mObject.getWidth();
69
      mObjHeight= mObject.getHeight();
70

    
71
      mWaveDyn = new Dynamic5D(1000,0.0f);
72
      mWaveSta1= new Static5D(0,0,-180,0,0);  // all other values besides the
73
      mWaveSta2= new Static5D(0,0,+180,0,0);  // fourth will be set from the UI
74

    
75
      mWaveDyn.add(mWaveSta1);
76
      mWaveDyn.add(mWaveSta2);
77
      mWaveDyn.setMode(Dynamic.MODE_JUMP);
78

    
79
      mQuat1 = new Static4D(           0,         0,           0,          1);  // unity quaternion
80
      mQuat2 = new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);  // something semi-random that looks good
81

    
82
      mQuatInt1 = new DynamicQuat(0,0.5f);
83
      mQuatInt2 = new DynamicQuat(0,0.5f);
84

    
85
      mQuatInt1.add(mQuat1);
86
      mQuatInt2.add(mQuat2);
87

    
88
      Static2D waveCenter = new Static2D(mObjWidth, mObjHeight/2);  // middle of the right edge
89
      Static4D waveRegion = new Static4D(0,0,mObjWidth,mObjWidth);
90

    
91
      mObject.wave(mWaveDyn, waveCenter, waveRegion);
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
    void setAmplitude(int a)
97
      {
98
      mWaveSta1.set1(a);
99
      mWaveSta2.set1(a);
100
      }
101

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
    void setLength(int l)
105
      {
106
      mWaveSta1.set2(l);
107
      mWaveSta2.set2(l);
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
    void setAngleA(int a)
113
      {
114
      mWaveSta1.set4(a);
115
      mWaveSta2.set4(a);
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
    void setAngleB(int b)
121
      {
122
      mWaveSta1.set5(b);
123
      mWaveSta2.set5(b);
124
      }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
    void setNoise(Static5D noise)
129
      {
130
      mWaveDyn.setNoise(noise);
131
      }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
    public void onDrawFrame(GL10 glUnused) 
136
      {
137
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
138
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
139
      
140
      mObject.draw(System.currentTimeMillis());
141
      }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
    
145
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
146
      {
147
      mScreenMin = width<height ? width:height;
148
    	
149
      mObject.abortEffects(EffectTypes.MATRIX);
150
      float factor;
151

    
152
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
153
        {
154
        factor = (0.8f*height)/mObjHeight;
155
        }
156
      else
157
        {
158
        factor = (0.8f*width)/mObjWidth;
159
        }
160

    
161
      mObject.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
162
      mObject.scale(factor);
163
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
164

    
165
      mObject.quaternion(mQuatInt1, center);
166
      mObject.quaternion(mQuatInt2, center);
167
       
168
      Distorted.onSurfaceChanged(width, height); 
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
    
173
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
174
      {
175
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
176
      Bitmap bitmap;
177
        
178
      try 
179
        {
180
        bitmap = BitmapFactory.decodeStream(is);
181
        } 
182
      finally 
183
        {
184
        try 
185
          {
186
          is.close();
187
          } 
188
        catch(IOException e) { }
189
        }  
190
      
191
      mObject.setBitmap(bitmap);
192
      
193
      try
194
        {
195
        Distorted.onSurfaceCreated(mView.getContext());
196
        }
197
      catch(Exception ex)
198
        {
199
        android.util.Log.e("Flag", ex.getMessage() );
200
        }
201
      }
202
}
(2-2/3)