Project

General

Profile

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

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

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.GLES30;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedScreen;
30
import org.distorted.library.EffectNames;
31
import org.distorted.library.MeshCubes;
32
import org.distorted.library.DistortedEffects;
33
import org.distorted.library.DistortedTexture;
34
import org.distorted.library.type.Static1D;
35
import org.distorted.library.type.Static3D;
36

    
37
import java.io.IOException;
38
import java.io.InputStream;
39

    
40
import javax.microedition.khronos.egl.EGLConfig;
41
import javax.microedition.khronos.opengles.GL10;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
class WindRenderer implements GLSurfaceView.Renderer
46
{
47
   private GLSurfaceView mView;
48
   private DistortedEffects mEffects;
49
   private DistortedTexture mTexture;
50
   private DistortedScreen mScreen;
51
   private WindEffectsManager mManager;
52
   private int mObjWidth, mObjHeight;
53
   private int mWind;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
   WindRenderer(GLSurfaceView view)
58
      { 
59
      mView = view;
60

    
61
      mEffects = new DistortedEffects();
62
      mTexture = new DistortedTexture(50,30);
63
      mManager = new WindEffectsManager(mTexture);
64
      mScreen  = new DistortedScreen();
65

    
66
      mScreen.attach(mTexture,mEffects,new MeshCubes(50,30,false));
67

    
68
      mObjWidth = mTexture.getWidth();
69
      mObjHeight= mTexture.getHeight();
70
      }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
   void setWind(int wind)
75
      {
76
      mWind = wind;
77
      mManager.setWind(mWind);
78
      }
79
   
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
   
82
   public void onDrawFrame(GL10 glUnused) 
83
      {
84
      mScreen.render( System.currentTimeMillis() );
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
    
89
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
90
      {
91
      mEffects.abortAllEffects();
92

    
93
      float factor = ( (float)(width<height? width:height) )/(mObjHeight + 1.4f*mObjWidth);
94

    
95
      mEffects.move( new Static3D( factor*mObjHeight*0.58f , factor*mObjHeight*0.08f , 0) );
96
      mEffects.scale(factor);
97

    
98
      Static1D angle = new Static1D(-45);
99
      Static3D axis  = new Static3D(0,0,1);
100
      Static3D center= new Static3D(0,mObjHeight/2,0);
101

    
102
      mEffects.rotate(angle, axis, center);
103
      mManager.apply(mEffects,mWind);
104
      mScreen.resize(width, height);
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
110
      {
111
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
112
      Bitmap bitmap;
113

    
114
      try
115
        {
116
        bitmap = BitmapFactory.decodeStream(is);
117
        }
118
      finally
119
        {
120
        try
121
          {
122
          is.close();
123
          }
124
        catch(IOException e) { }
125
        }
126

    
127
      mTexture.setTexture(bitmap);
128

    
129
      DistortedEffects.enableEffect(EffectNames.DEFORM);
130
      DistortedEffects.enableEffect(EffectNames.WAVE);
131

    
132
      try
133
        {
134
        Distorted.onCreate(mView.getContext());
135
        }
136
      catch(Exception ex)
137
        {
138
        android.util.Log.e("Wind", ex.getMessage() );
139
        }
140
      }
141
}
(3-3/4)