Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindRenderer.java @ dc10a48d

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.wind;
21

    
22
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26
import android.graphics.Bitmap;
27
import android.graphics.BitmapFactory;
28
import android.opengl.GLSurfaceView;
29

    
30
import org.distorted.examples.R;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectRotate;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectDeform;
35
import org.distorted.library.effect.VertexEffectScale;
36
import org.distorted.library.effect.VertexEffectWave;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.mesh.MeshCubes;
40
import org.distorted.library.main.DistortedEffects;
41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.type.Static1D;
43
import org.distorted.library.type.Static3D;
44

    
45
import java.io.IOException;
46
import java.io.InputStream;
47

    
48
import javax.microedition.khronos.egl.EGLConfig;
49
import javax.microedition.khronos.opengles.GL10;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
class WindRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
54
{
55
   private static final int X = 50;
56
   private static final int Y = 30;
57
   private static final int Z =  1;
58

    
59
   private final GLSurfaceView mView;
60
   private final Resources mResources;
61
   private final DistortedTexture mTexture;
62
   private final DistortedScreen mScreen;
63
   private final WindEffectsManager mManager;
64
   private final WindGust mGust;
65
   private final Static3D mMove, mScale;
66
   private final float mObjWidth, mObjHeight;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
   WindRenderer(GLSurfaceView view)
71
      { 
72
      mView = view;
73
      mResources = view.getResources();
74

    
75
      MeshCubes cubes = new MeshCubes(X,Y,Z);
76
      DistortedEffects effects = new DistortedEffects();
77

    
78
      mTexture = new DistortedTexture();
79
      mManager = new WindEffectsManager(X,Y);
80
      mGust    = new WindGust();
81
      mScreen  = new DistortedScreen();
82

    
83
      mScreen.attach(mTexture,effects,cubes);
84

    
85
      mObjWidth = X;
86
      mObjHeight= Y;
87

    
88
      effects.apply( new VertexEffectScale( new Static3D(X,Y,Z) ) );
89
      mManager.apply(effects);
90

    
91
      mMove = new Static3D(0,0,0);
92
      mScale= new Static3D(1,1,1);
93

    
94
      Static1D angle = new Static1D(-45);
95
      Static3D axis  = new Static3D(0,0,1);
96
      Static3D center= new Static3D(-mObjWidth*0.5f,0,0);
97

    
98
      effects.apply( new MatrixEffectRotate(angle, axis, center) );
99
      effects.apply( new MatrixEffectScale(mScale));
100
      effects.apply( new MatrixEffectMove(mMove));
101
      }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
   void setWind(int wind)
106
      {
107
      mGust.setAverageWind(wind);
108
      }
109
   
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
   void pauseWind()
113
      {
114
      if( mGust   !=null ) mGust.pauseWind();
115
      if( mManager!=null ) mManager.pauseWind();
116
      }
117

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

    
120
   void resumeWind()
121
      {
122
      if( mGust   !=null ) mGust.resumeWind();
123
      if( mManager!=null ) mManager.resumeWind();
124
      }
125

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

    
128
   public void onDrawFrame(GL10 glUnused) 
129
      {
130
      long time = System.currentTimeMillis();
131

    
132
      if( mGust.isWindBlowingNow(time) ) mManager.increaseWind(time);
133
      else                               mManager.decreaseWind(time);
134

    
135
      mScreen.render(time);
136
      }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
141
      {
142
      int min = Math.min(width,height);
143

    
144
      float factor = ((float)min)/(mObjHeight + 1.4f*mObjWidth);
145
      mMove.set( factor*mObjHeight*1.28f -min*0.5f, min*0.5f - factor*mObjHeight*0.58f, 0 );
146
      mScale.set(factor,factor,factor);
147
      mScreen.resize(width, height);
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
    
152
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
153
      {
154
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
155
      Bitmap bitmap;
156

    
157
      try
158
        {
159
        bitmap = BitmapFactory.decodeStream(is);
160
        }
161
      finally
162
        {
163
        try
164
          {
165
          is.close();
166
          }
167
        catch(IOException ignored) { }
168
        }
169

    
170
      mTexture.setTexture(bitmap);
171

    
172
      VertexEffectScale.enable();
173
      VertexEffectDeform.enable();
174
      VertexEffectWave.enable();
175
      DistortedLibrary.onSurfaceCreated(this);
176
      }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
    public void distortedException(Exception ex)
181
      {
182
      android.util.Log.e("Wind", ex.getMessage() );
183
      }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
    public int openGlVersion()
188
      {
189
      Context context = mView.getContext();
190
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
191
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
192
      int glESversion = configurationInfo.reqGlEsVersion;
193
      int major = glESversion >> 16;
194
      int minor = glESversion & 0xff;
195

    
196
      return 100*major + 10*minor;
197
      }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
    public InputStream localFile(int fileID)
202
      {
203
      return mResources.openRawResource(fileID);
204
      }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
    public void logMessage(String message)
209
      {
210
      android.util.Log.e("Wind", message );
211
      }
212
}
(4-4/5)