Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindRenderer.java @ 8664ea2e

1 14122c52 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 14122c52 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 14122c52 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 14122c52 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 14122c52 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 8664ea2e Leszek Koltunski
import org.distorted.library.effect.MatrixEffectShear;
31 885b9cca leszek
import org.distorted.library.effect.VertexEffectDeform;
32
import org.distorted.library.effect.VertexEffectWave;
33 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
34 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
35 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshCubes;
36 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedTexture;
38 815687bb Leszek Koltunski
import org.distorted.library.type.Static1D;
39 14122c52 Leszek Koltunski
import org.distorted.library.type.Static3D;
40 8664ea2e Leszek Koltunski
import org.distorted.library.type.Static4D;
41 14122c52 Leszek Koltunski
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 GLSurfaceView mView;
53 40eef4b9 Leszek Koltunski
   private DistortedTexture mTexture;
54 d218d64e leszek
   private DistortedScreen mScreen;
55 392e16fd Leszek Koltunski
   private WindEffectsManager mManager;
56 318dd77b Leszek Koltunski
   private WindGust mGust;
57 a9716c0f Leszek Koltunski
   private Static3D mMove, mScale;
58 698ad0a8 Leszek Koltunski
   private float mObjWidth, mObjHeight;
59 14122c52 Leszek Koltunski
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62
   WindRenderer(GLSurfaceView view)
63
      { 
64
      mView = view;
65
66 687263cc Leszek Koltunski
      MeshCubes cubes = new MeshCubes(50,30,1);
67 698ad0a8 Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
68
      cubes.setStretch(50,30,1);
69 687263cc Leszek Koltunski
70
      mTexture = new DistortedTexture();
71
      mManager = new WindEffectsManager(50,30);
72 318dd77b Leszek Koltunski
      mGust    = new WindGust();
73 e4330c89 Leszek Koltunski
      mScreen  = new DistortedScreen();
74 14122c52 Leszek Koltunski
75 687263cc Leszek Koltunski
      mScreen.attach(mTexture,effects,cubes);
76 fe59d375 Leszek Koltunski
77 698ad0a8 Leszek Koltunski
      mObjWidth = cubes.getStretchX();
78
      mObjHeight= cubes.getStretchY();
79 a9716c0f Leszek Koltunski
80 dae661e9 Leszek Koltunski
      mManager.apply(effects);
81
82 a9716c0f Leszek Koltunski
      mMove = new Static3D(0,0,0);
83
      mScale= new Static3D(1,1,1);
84
85
      Static1D angle = new Static1D(-45);
86
      Static3D axis  = new Static3D(0,0,1);
87 0de83d97 Leszek Koltunski
      Static3D center= new Static3D(-mObjWidth*0.5f,0,0);
88 a9716c0f Leszek Koltunski
89 e4330c89 Leszek Koltunski
      effects.apply( new MatrixEffectRotate(angle, axis, center) );
90 dae661e9 Leszek Koltunski
      effects.apply( new MatrixEffectScale(mScale));
91
      effects.apply( new MatrixEffectMove(mMove));
92 14122c52 Leszek Koltunski
      }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96
   void setWind(int wind)
97
      {
98 318dd77b Leszek Koltunski
      mGust.setAverageWind(wind);
99 14122c52 Leszek Koltunski
      }
100
   
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
   
103
   public void onDrawFrame(GL10 glUnused) 
104
      {
105 318dd77b Leszek Koltunski
      long time = System.currentTimeMillis();
106
107
      if( mGust.isWindBlowingNow(time) ) mManager.increaseWind(time);
108
      else                               mManager.decreaseWind(time);
109
110
      mScreen.render(time);
111 14122c52 Leszek Koltunski
      }
112
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114 fce25d04 leszek
    
115 14122c52 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
116
      {
117 318dd77b Leszek Koltunski
      int min = width<height? width:height;
118
119
      float factor = ((float)min)/(mObjHeight + 1.4f*mObjWidth);
120 0de83d97 Leszek Koltunski
      mMove.set( factor*mObjHeight*1.28f -min*0.5f, min*0.5f - factor*mObjHeight*0.58f, 0 );
121 a9716c0f Leszek Koltunski
      mScale.set(factor,factor,factor);
122 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
123 14122c52 Leszek Koltunski
      }
124
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
    
127
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
128 e7a4ef16 Leszek Koltunski
      {
129 14122c52 Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
130
      Bitmap bitmap;
131
132
      try
133
        {
134
        bitmap = BitmapFactory.decodeStream(is);
135
        }
136
      finally
137
        {
138
        try
139
          {
140
          is.close();
141
          }
142
        catch(IOException e) { }
143
        }
144
145 40eef4b9 Leszek Koltunski
      mTexture.setTexture(bitmap);
146 14122c52 Leszek Koltunski
147 885b9cca leszek
      VertexEffectDeform.enable();
148
      VertexEffectWave.enable();
149 6637d0f2 Leszek Koltunski
150 14122c52 Leszek Koltunski
      try
151
        {
152 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
153 14122c52 Leszek Koltunski
        }
154
      catch(Exception ex)
155
        {
156
        android.util.Log.e("Wind", ex.getMessage() );
157
        }
158
      }
159
}