Project

General

Profile

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

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

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.GLES20;
25
import android.opengl.GLSurfaceView;
26
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29 10b7e588 Leszek Koltunski
import org.distorted.library.GridCubes;
30 14122c52 Leszek Koltunski
import org.distorted.library.DistortedObject;
31 815687bb Leszek Koltunski
import org.distorted.library.type.Static1D;
32 14122c52 Leszek Koltunski
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 10b7e588 Leszek Koltunski
   private GridCubes mGrid;
47 815687bb Leszek Koltunski
   private WindEffectsManager mEffects;
48 14122c52 Leszek Koltunski
   private int mObjWidth, mObjHeight;
49 21819119 Leszek Koltunski
   private int mWind;
50 14122c52 Leszek Koltunski
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53
   WindRenderer(GLSurfaceView view)
54
      { 
55
      mView = view;
56
57 10b7e588 Leszek Koltunski
      mGrid    = new GridCubes(50,30,false);
58 e8b6aa95 Leszek Koltunski
      mObject  = new DistortedObject(50,30,1);
59 b041d424 Leszek Koltunski
      mEffects = new WindEffectsManager(mObject);
60 14122c52 Leszek Koltunski
61
      mObjWidth = mObject.getWidth();
62
      mObjHeight= mObject.getHeight();
63
      }
64
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67
   void setWind(int wind)
68
      {
69 21819119 Leszek Koltunski
      mWind = wind;
70
      mEffects.setWind(mWind);
71 14122c52 Leszek Koltunski
      }
72
   
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
   
75
   public void onDrawFrame(GL10 glUnused) 
76
      {
77
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
78 e8b6aa95 Leszek Koltunski
      mObject.draw(System.currentTimeMillis(),mGrid);
79 14122c52 Leszek Koltunski
      }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
    
83
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
84
      {
85 b041d424 Leszek Koltunski
      mObject.abortAllEffects();
86 21819119 Leszek Koltunski
87 b041d424 Leszek Koltunski
      float factor = ( (float)(width<height? width:height) )/(mObjHeight + 1.4f*mObjWidth);
88 14122c52 Leszek Koltunski
89 b041d424 Leszek Koltunski
      mObject.move( new Static3D( factor*mObjHeight*0.58f , factor*mObjHeight*0.08f , 0) );
90 14122c52 Leszek Koltunski
      mObject.scale(factor);
91
92 815687bb Leszek Koltunski
      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 14122c52 Leszek Koltunski
98 21819119 Leszek Koltunski
      mEffects.apply(mObject, mWind);
99
100 14122c52 Leszek Koltunski
      Distorted.onSurfaceChanged(width, height);
101
      }
102
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
    
105
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
106 e7a4ef16 Leszek Koltunski
      {
107
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
108
109 14122c52 Leszek Koltunski
      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 e8b6aa95 Leszek Koltunski
      mObject.setTexture(bitmap);
126 14122c52 Leszek Koltunski
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
}