Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindRenderer.java @ 752c6b57

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.Distorted;
28
import org.distorted.library.DistortedScreen;
29
import org.distorted.library.EffectNames;
30
import org.distorted.library.MeshCubes;
31
import org.distorted.library.DistortedEffects;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.type.Static1D;
34
import org.distorted.library.type.Static3D;
35

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

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

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

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

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

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

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

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

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
89
      {
90
      float q= ((float)width/height)*((float)mObjHeight/mObjWidth);
91
      float scale = 0.5f;
92
      Static1D angle = new Static1D(-45);
93
      Static3D axis  = new Static3D(0,0,1);
94
      Static3D center= new Static3D( -scale/2,0,0 );
95
      Static3D move  = new Static3D( 0,((float)width/height)*(1-scale/2)-0.5f,0 );
96

    
97
      mEffects.abortAllEffects();
98
      //mEffects.move( move );
99
      //mEffects.rotate(angle, axis, center);
100
      //mManager.apply(mEffects,mWind);
101
      mEffects.scale(q<1 ? (new Static3D(scale,scale*q,scale)) : (new Static3D(scale/q,scale,scale/q)));
102

    
103
      mScreen.resize(width, height);
104
      }
105

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

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

    
126
      mTexture.setTexture(bitmap);
127

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

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