Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindRenderer.java @ 10b7e588

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.GridCubes;
30
import org.distorted.library.DistortedObject;
31
import org.distorted.library.type.Static1D;
32
import org.distorted.library.type.Static3D;
33

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

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

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
class WindRenderer implements GLSurfaceView.Renderer
43
{
44
   private GLSurfaceView mView;
45
   private DistortedObject mObject;
46
   private GridCubes mGrid;
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
      mGrid    = new GridCubes(50,30,false);
58
      mObject  = new DistortedObject(50,30,1);
59
      mEffects = new WindEffectsManager(mObject);
60

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

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

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

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

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

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

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

    
98
      mEffects.apply(mObject, mWind);
99

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

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
    
105
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
106
      {
107
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
108

    
109
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
110
      Bitmap bitmap;
111

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

    
125
      mObject.setTexture(bitmap);
126

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