Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindRenderer.java @ 061449ed

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 885b9cca leszek
import org.distorted.library.effect.VertexEffectDeform;
31 dcd2cd41 Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
32 885b9cca leszek
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
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 061449ed Leszek Koltunski
class WindRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
50 14122c52 Leszek Koltunski
{
51 513b2e9c Leszek Koltunski
   private static final int X = 50;
52
   private static final int Y = 30;
53
   private static final int Z =  1;
54
55 14122c52 Leszek Koltunski
   private GLSurfaceView mView;
56 40eef4b9 Leszek Koltunski
   private DistortedTexture mTexture;
57 d218d64e leszek
   private DistortedScreen mScreen;
58 392e16fd Leszek Koltunski
   private WindEffectsManager mManager;
59 318dd77b Leszek Koltunski
   private WindGust mGust;
60 a9716c0f Leszek Koltunski
   private Static3D mMove, mScale;
61 698ad0a8 Leszek Koltunski
   private float mObjWidth, mObjHeight;
62 14122c52 Leszek Koltunski
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
   WindRenderer(GLSurfaceView view)
66
      { 
67
      mView = view;
68
69 513b2e9c Leszek Koltunski
      MeshCubes cubes = new MeshCubes(X,Y,Z);
70 698ad0a8 Leszek Koltunski
      DistortedEffects effects = new DistortedEffects();
71 687263cc Leszek Koltunski
72
      mTexture = new DistortedTexture();
73 513b2e9c Leszek Koltunski
      mManager = new WindEffectsManager(X,Y);
74 318dd77b Leszek Koltunski
      mGust    = new WindGust();
75 e4330c89 Leszek Koltunski
      mScreen  = new DistortedScreen();
76 14122c52 Leszek Koltunski
77 687263cc Leszek Koltunski
      mScreen.attach(mTexture,effects,cubes);
78 fe59d375 Leszek Koltunski
79 dcd2cd41 Leszek Koltunski
      mObjWidth = X;
80
      mObjHeight= Y;
81 a9716c0f Leszek Koltunski
82 dcd2cd41 Leszek Koltunski
      effects.apply( new VertexEffectScale( new Static3D(X,Y,Z) ) );
83 dae661e9 Leszek Koltunski
      mManager.apply(effects);
84
85 a9716c0f Leszek Koltunski
      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 0de83d97 Leszek Koltunski
      Static3D center= new Static3D(-mObjWidth*0.5f,0,0);
91 a9716c0f Leszek Koltunski
92 e4330c89 Leszek Koltunski
      effects.apply( new MatrixEffectRotate(angle, axis, center) );
93 dae661e9 Leszek Koltunski
      effects.apply( new MatrixEffectScale(mScale));
94
      effects.apply( new MatrixEffectMove(mMove));
95 14122c52 Leszek Koltunski
      }
96
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99
   void setWind(int wind)
100
      {
101 318dd77b Leszek Koltunski
      mGust.setAverageWind(wind);
102 14122c52 Leszek Koltunski
      }
103
   
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
   
106
   public void onDrawFrame(GL10 glUnused) 
107
      {
108 318dd77b Leszek Koltunski
      long time = System.currentTimeMillis();
109
110
      if( mGust.isWindBlowingNow(time) ) mManager.increaseWind(time);
111
      else                               mManager.decreaseWind(time);
112
113
      mScreen.render(time);
114 14122c52 Leszek Koltunski
      }
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117 fce25d04 leszek
    
118 14122c52 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
119
      {
120 318dd77b Leszek Koltunski
      int min = width<height? width:height;
121
122
      float factor = ((float)min)/(mObjHeight + 1.4f*mObjWidth);
123 0de83d97 Leszek Koltunski
      mMove.set( factor*mObjHeight*1.28f -min*0.5f, min*0.5f - factor*mObjHeight*0.58f, 0 );
124 a9716c0f Leszek Koltunski
      mScale.set(factor,factor,factor);
125 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
126 14122c52 Leszek Koltunski
      }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
    
130
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
131 e7a4ef16 Leszek Koltunski
      {
132 14122c52 Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
133
      Bitmap bitmap;
134
135
      try
136
        {
137
        bitmap = BitmapFactory.decodeStream(is);
138
        }
139
      finally
140
        {
141
        try
142
          {
143
          is.close();
144
          }
145
        catch(IOException e) { }
146
        }
147
148 40eef4b9 Leszek Koltunski
      mTexture.setTexture(bitmap);
149 14122c52 Leszek Koltunski
150 dcd2cd41 Leszek Koltunski
      VertexEffectScale.enable();
151 885b9cca leszek
      VertexEffectDeform.enable();
152
      VertexEffectWave.enable();
153 061449ed Leszek Koltunski
      DistortedLibrary.onCreate(mView.getContext(), this);
154
      }
155 6637d0f2 Leszek Koltunski
156 061449ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
157
158
    public void distortedException(Exception ex)
159
      {
160
      android.util.Log.e("Wind", ex.getMessage() );
161 14122c52 Leszek Koltunski
      }
162
}