Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindRenderer.java @ 318dd77b

1 14122c52 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.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 a9716c0f Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
28
import org.distorted.library.effect.MatrixEffectRotate;
29
import org.distorted.library.effect.MatrixEffectScale;
30 885b9cca leszek
import org.distorted.library.effect.VertexEffectDeform;
31
import org.distorted.library.effect.VertexEffectWave;
32 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
33
import org.distorted.library.main.DistortedScreen;
34 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
35 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
36
import org.distorted.library.main.DistortedTexture;
37 815687bb Leszek Koltunski
import org.distorted.library.type.Static1D;
38 14122c52 Leszek Koltunski
import org.distorted.library.type.Static3D;
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 WindRenderer implements GLSurfaceView.Renderer
49
{
50
   private GLSurfaceView mView;
51 40eef4b9 Leszek Koltunski
   private DistortedTexture mTexture;
52 d218d64e leszek
   private DistortedScreen mScreen;
53 392e16fd Leszek Koltunski
   private WindEffectsManager mManager;
54 318dd77b Leszek Koltunski
   private WindGust mGust;
55 a9716c0f Leszek Koltunski
   private Static3D mMove, mScale;
56 14122c52 Leszek Koltunski
   private int mObjWidth, mObjHeight;
57
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60
   WindRenderer(GLSurfaceView view)
61
      { 
62
      mView = view;
63
64 7451c98a Leszek Koltunski
      mTexture = new DistortedTexture(50,30);
65 392e16fd Leszek Koltunski
      mManager = new WindEffectsManager(mTexture);
66 318dd77b Leszek Koltunski
      mGust    = new WindGust();
67 e4330c89 Leszek Koltunski
      mScreen  = new DistortedScreen();
68 14122c52 Leszek Koltunski
69 e4330c89 Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
70
71
      mScreen.attach(mTexture,effects,new MeshCubes(50,30,1));
72 fe59d375 Leszek Koltunski
73 40eef4b9 Leszek Koltunski
      mObjWidth = mTexture.getWidth();
74
      mObjHeight= mTexture.getHeight();
75 a9716c0f Leszek Koltunski
76
      mMove = new Static3D(0,0,0);
77
      mScale= new Static3D(1,1,1);
78 e4330c89 Leszek Koltunski
      effects.apply( new MatrixEffectMove(mMove));
79
      effects.apply( new MatrixEffectScale(mScale));
80 a9716c0f Leszek Koltunski
81
      Static1D angle = new Static1D(-45);
82
      Static3D axis  = new Static3D(0,0,1);
83
      Static3D center= new Static3D(0,mObjHeight/2,0);
84
85 e4330c89 Leszek Koltunski
      effects.apply( new MatrixEffectRotate(angle, axis, center) );
86 318dd77b Leszek Koltunski
      mManager.apply(effects);
87 14122c52 Leszek Koltunski
      }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91
   void setWind(int wind)
92
      {
93 318dd77b Leszek Koltunski
      mGust.setAverageWind(wind);
94 14122c52 Leszek Koltunski
      }
95
   
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
   
98
   public void onDrawFrame(GL10 glUnused) 
99
      {
100 318dd77b Leszek Koltunski
      long time = System.currentTimeMillis();
101
102
      if( mGust.isWindBlowingNow(time) ) mManager.increaseWind(time);
103
      else                               mManager.decreaseWind(time);
104
105
      mScreen.render(time);
106 14122c52 Leszek Koltunski
      }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109 fce25d04 leszek
    
110 14122c52 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
111
      {
112 318dd77b Leszek Koltunski
      int min = width<height? width:height;
113
114
      float factor = ((float)min)/(mObjHeight + 1.4f*mObjWidth);
115
      mMove.set( factor*mObjHeight*0.58f +(width-min)/2, height - factor*mObjHeight*1.08f -(height-min)/2, 0 );
116 a9716c0f Leszek Koltunski
      mScale.set(factor,factor,factor);
117 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
118 14122c52 Leszek Koltunski
      }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
123 e7a4ef16 Leszek Koltunski
      {
124 14122c52 Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
125
      Bitmap bitmap;
126
127
      try
128
        {
129
        bitmap = BitmapFactory.decodeStream(is);
130
        }
131
      finally
132
        {
133
        try
134
          {
135
          is.close();
136
          }
137
        catch(IOException e) { }
138
        }
139
140 40eef4b9 Leszek Koltunski
      mTexture.setTexture(bitmap);
141 14122c52 Leszek Koltunski
142 885b9cca leszek
      VertexEffectDeform.enable();
143
      VertexEffectWave.enable();
144 6637d0f2 Leszek Koltunski
145 14122c52 Leszek Koltunski
      try
146
        {
147 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
148 14122c52 Leszek Koltunski
        }
149
      catch(Exception ex)
150
        {
151
        android.util.Log.e("Wind", ex.getMessage() );
152
        }
153
      }
154
}