Project

General

Profile

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

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

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
   public void onDrawFrame(GL10 glUnused) 
107
      {
108
      long time = System.currentTimeMillis();
109

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

    
113
      mScreen.render(time);
114
      }
115

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

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

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
    
130
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
131
      {
132
      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
      mTexture.setTexture(bitmap);
149

    
150
      VertexEffectScale.enable();
151
      VertexEffectDeform.enable();
152
      VertexEffectWave.enable();
153
      DistortedLibrary.onCreate(mView.getContext(), this);
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
    public void distortedException(Exception ex)
159
      {
160
      android.util.Log.e("Wind", ex.getMessage() );
161
      }
162
}
(4-4/5)