Project

General

Profile

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

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

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

    
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedCubes;
30
import org.distorted.library.DistortedObject;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.Static1D;
33
import org.distorted.library.type.Static3D;
34

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

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

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
class WindRenderer implements GLSurfaceView.Renderer
44
{
45
   private GLSurfaceView mView;
46
   private DistortedObject mObject;
47
   private WindEffectsManager mEffects;
48
   private int mObjWidth, mObjHeight;
49
   private int mWind;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
   WindRenderer(GLSurfaceView view)
54
      { 
55
      mView = view;
56

    
57
      mObject = new DistortedCubes(50,30,10,false);
58
      mEffects = new WindEffectsManager(mObject);
59

    
60
      mObjWidth = mObject.getWidth();
61
      mObjHeight= mObject.getHeight();
62
      }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
   void setWind(int wind)
67
      {
68
      mWind = wind;
69
      mEffects.setWind(mWind);
70
      }
71
   
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
   
74
   public void onDrawFrame(GL10 glUnused) 
75
      {
76
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
77
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
78
     
79
      mObject.draw(System.currentTimeMillis());
80
      }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
    
84
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
85
      {
86
      mObject.abortAllEffects();
87

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

    
90
      mObject.move( new Static3D( factor*mObjHeight*0.58f , factor*mObjHeight*0.08f , 0) );
91
      mObject.scale(factor);
92

    
93
      Static1D angle = new Static1D(-45);
94
      Static3D axis  = new Static3D(0,0,1);
95
      Static3D center= new Static3D(0,mObjHeight/2,0);
96

    
97
      mObject.rotate(angle, axis, center);
98

    
99
      mEffects.apply(mObject, mWind);
100

    
101
      Distorted.onSurfaceChanged(width, height);
102
      }
103

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

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

    
124
      mObject.setBitmap(bitmap);
125

    
126
      try
127
        {
128
        Distorted.onSurfaceCreated(mView.getContext());
129
        }
130
      catch(Exception ex)
131
        {
132
        android.util.Log.e("Wind", ex.getMessage() );
133
        }
134
      }
135
}
(3-3/4)