Project

General

Profile

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

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

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.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

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

    
41
import java.io.IOException;
42
import java.io.InputStream;
43

    
44
import javax.microedition.khronos.egl.EGLConfig;
45
import javax.microedition.khronos.opengles.GL10;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
class WindRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
50
{
51
   private static final int X = 50;
52
   private static final int Y = 30;
53
   private static final int Z =  1;
54

    
55
   private GLSurfaceView mView;
56
   private DistortedTexture mTexture;
57
   private DistortedScreen mScreen;
58
   private WindEffectsManager mManager;
59
   private WindGust mGust;
60
   private Static3D mMove, mScale;
61
   private float mObjWidth, mObjHeight;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
   WindRenderer(GLSurfaceView view)
66
      { 
67
      mView = view;
68

    
69
      MeshCubes cubes = new MeshCubes(X,Y,Z);
70
      DistortedEffects effects = new DistortedEffects();
71

    
72
      mTexture = new DistortedTexture();
73
      mManager = new WindEffectsManager(X,Y);
74
      mGust    = new WindGust();
75
      mScreen  = new DistortedScreen();
76

    
77
      mScreen.attach(mTexture,effects,cubes);
78

    
79
      mObjWidth = X;
80
      mObjHeight= Y;
81

    
82
      effects.apply( new VertexEffectScale( new Static3D(X,Y,Z) ) );
83
      mManager.apply(effects);
84

    
85
      mMove = new Static3D(0,0,0);
86
      mScale= new Static3D(1,1,1);
87

    
88
      Static1D angle = new Static1D(-45);
89
      Static3D axis  = new Static3D(0,0,1);
90
      Static3D center= new Static3D(-mObjWidth*0.5f,0,0);
91

    
92
      effects.apply( new MatrixEffectRotate(angle, axis, center) );
93
      effects.apply( new MatrixEffectScale(mScale));
94
      effects.apply( new MatrixEffectMove(mMove));
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
   void setWind(int wind)
100
      {
101
      mGust.setAverageWind(wind);
102
      }
103
   
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

    
106
   void pauseWind()
107
      {
108
      if( mGust   !=null ) mGust.pauseWind();
109
      if( mManager!=null ) mManager.pauseWind();
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
   void resumeWind()
115
      {
116
      if( mGust   !=null ) mGust.resumeWind();
117
      if( mManager!=null ) mManager.resumeWind();
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121

    
122
   public void onDrawFrame(GL10 glUnused) 
123
      {
124
      long time = System.currentTimeMillis();
125

    
126
      if( mGust.isWindBlowingNow(time) ) mManager.increaseWind(time);
127
      else                               mManager.decreaseWind(time);
128

    
129
      mScreen.render(time);
130
      }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
    
134
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
135
      {
136
      int min = width<height? width:height;
137

    
138
      float factor = ((float)min)/(mObjHeight + 1.4f*mObjWidth);
139
      mMove.set( factor*mObjHeight*1.28f -min*0.5f, min*0.5f - factor*mObjHeight*0.58f, 0 );
140
      mScale.set(factor,factor,factor);
141
      mScreen.resize(width, height);
142
      }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
    
146
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
147
      {
148
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
149
      Bitmap bitmap;
150

    
151
      try
152
        {
153
        bitmap = BitmapFactory.decodeStream(is);
154
        }
155
      finally
156
        {
157
        try
158
          {
159
          is.close();
160
          }
161
        catch(IOException e) { }
162
        }
163

    
164
      mTexture.setTexture(bitmap);
165

    
166
      VertexEffectScale.enable();
167
      VertexEffectDeform.enable();
168
      VertexEffectWave.enable();
169
      DistortedLibrary.onCreate(mView.getContext(), this);
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
    public void distortedException(Exception ex)
175
      {
176
      android.util.Log.e("Wind", ex.getMessage() );
177
      }
178
}
(4-4/5)