Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindRenderer.java @ 513b2e9c

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.MatrixEffectShear;
31
import org.distorted.library.effect.VertexEffectDeform;
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
import org.distorted.library.type.Static4D;
41

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

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

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

    
70
      MeshCubes cubes = new MeshCubes(X,Y,Z);
71
      DistortedEffects effects = new DistortedEffects();
72
      cubes.setStretch(X,Y,Z);
73

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

    
79
      mScreen.attach(mTexture,effects,cubes);
80

    
81
      mObjWidth = cubes.getStretchX();
82
      mObjHeight= cubes.getStretchY();
83

    
84
      mManager.apply(effects);
85

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

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

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

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
   void setWind(int wind)
101
      {
102
      mGust.setAverageWind(wind);
103
      }
104
   
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
   
107
   public void onDrawFrame(GL10 glUnused) 
108
      {
109
      long time = System.currentTimeMillis();
110

    
111
      if( mGust.isWindBlowingNow(time) ) mManager.increaseWind(time);
112
      else                               mManager.decreaseWind(time);
113

    
114
      mScreen.render(time);
115
      }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
120
      {
121
      int min = width<height? width:height;
122

    
123
      float factor = ((float)min)/(mObjHeight + 1.4f*mObjWidth);
124
      mMove.set( factor*mObjHeight*1.28f -min*0.5f, min*0.5f - factor*mObjHeight*0.58f, 0 );
125
      mScale.set(factor,factor,factor);
126
      mScreen.resize(width, height);
127
      }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
    
131
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
132
      {
133
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
134
      Bitmap bitmap;
135

    
136
      try
137
        {
138
        bitmap = BitmapFactory.decodeStream(is);
139
        }
140
      finally
141
        {
142
        try
143
          {
144
          is.close();
145
          }
146
        catch(IOException e) { }
147
        }
148

    
149
      mTexture.setTexture(bitmap);
150

    
151
      VertexEffectDeform.enable();
152
      VertexEffectWave.enable();
153

    
154
      try
155
        {
156
        DistortedLibrary.onCreate(mView.getContext());
157
        }
158
      catch(Exception ex)
159
        {
160
        android.util.Log.e("Wind", ex.getMessage() );
161
        }
162
      }
163
}
(4-4/5)