Project

General

Profile

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

examples / src / main / java / org / distorted / examples / wind / WindRenderer.java @ 7451c98a

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 40eef4b9 Leszek Koltunski
import org.distorted.library.DistortedEffectQueues;
31
import org.distorted.library.DistortedTexture;
32 815687bb Leszek Koltunski
import org.distorted.library.type.Static1D;
33 14122c52 Leszek Koltunski
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 40eef4b9 Leszek Koltunski
   private DistortedEffectQueues mQueues;
47
   private DistortedTexture mTexture;
48 10b7e588 Leszek Koltunski
   private GridCubes mGrid;
49 815687bb Leszek Koltunski
   private WindEffectsManager mEffects;
50 14122c52 Leszek Koltunski
   private int mObjWidth, mObjHeight;
51 21819119 Leszek Koltunski
   private int mWind;
52 14122c52 Leszek Koltunski
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55
   WindRenderer(GLSurfaceView view)
56
      { 
57
      mView = view;
58
59 10b7e588 Leszek Koltunski
      mGrid    = new GridCubes(50,30,false);
60 40eef4b9 Leszek Koltunski
      mQueues  = new DistortedEffectQueues();
61 7451c98a Leszek Koltunski
      mTexture = new DistortedTexture(50,30);
62 40eef4b9 Leszek Koltunski
      mEffects = new WindEffectsManager(mTexture);
63 14122c52 Leszek Koltunski
64 40eef4b9 Leszek Koltunski
      mObjWidth = mTexture.getWidth();
65
      mObjHeight= mTexture.getHeight();
66 14122c52 Leszek Koltunski
      }
67
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70
   void setWind(int wind)
71
      {
72 21819119 Leszek Koltunski
      mWind = wind;
73
      mEffects.setWind(mWind);
74 14122c52 Leszek Koltunski
      }
75
   
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
   
78
   public void onDrawFrame(GL10 glUnused) 
79
      {
80
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
81 40eef4b9 Leszek Koltunski
      mQueues.draw(System.currentTimeMillis(),mTexture, mGrid);
82 14122c52 Leszek Koltunski
      }
83
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
    
86
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
87
      {
88 40eef4b9 Leszek Koltunski
      mQueues.abortAllEffects();
89 21819119 Leszek Koltunski
90 b041d424 Leszek Koltunski
      float factor = ( (float)(width<height? width:height) )/(mObjHeight + 1.4f*mObjWidth);
91 14122c52 Leszek Koltunski
92 40eef4b9 Leszek Koltunski
      mQueues.move( new Static3D( factor*mObjHeight*0.58f , factor*mObjHeight*0.08f , 0) );
93
      mQueues.scale(factor);
94 14122c52 Leszek Koltunski
95 815687bb Leszek Koltunski
      Static1D angle = new Static1D(-45);
96
      Static3D axis  = new Static3D(0,0,1);
97
      Static3D center= new Static3D(0,mObjHeight/2,0);
98
99 40eef4b9 Leszek Koltunski
      mQueues.rotate(angle, axis, center);
100 14122c52 Leszek Koltunski
101 40eef4b9 Leszek Koltunski
      mEffects.apply(mQueues,mWind);
102 21819119 Leszek Koltunski
103 14122c52 Leszek Koltunski
      Distorted.onSurfaceChanged(width, height);
104
      }
105
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
109 e7a4ef16 Leszek Koltunski
      {
110
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
111
112 14122c52 Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
113
      Bitmap bitmap;
114
115
      try
116
        {
117
        bitmap = BitmapFactory.decodeStream(is);
118
        }
119
      finally
120
        {
121
        try
122
          {
123
          is.close();
124
          }
125
        catch(IOException e) { }
126
        }
127
128 40eef4b9 Leszek Koltunski
      mTexture.setTexture(bitmap);
129 14122c52 Leszek Koltunski
130
      try
131
        {
132
        Distorted.onSurfaceCreated(mView.getContext());
133
        }
134
      catch(Exception ex)
135
        {
136
        android.util.Log.e("Wind", ex.getMessage() );
137
        }
138
      }
139
}