Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindRenderer.java @ e4330c89

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