Project

General

Profile

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

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

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.DistortedEffectQueues;
31
import org.distorted.library.DistortedTexture;
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 DistortedEffectQueues mQueues;
47
   private DistortedTexture mTexture;
48
   private GridCubes mGrid;
49
   private WindEffectsManager mEffects;
50
   private int mObjWidth, mObjHeight;
51
   private int mWind;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

    
59
      mGrid    = new GridCubes(50,30,false);
60
      mQueues  = new DistortedEffectQueues();
61
      mTexture = new DistortedTexture(50,30);
62
      mEffects = new WindEffectsManager(mTexture);
63

    
64
      mObjWidth = mTexture.getWidth();
65
      mObjHeight= mTexture.getHeight();
66
      }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
    
86
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
87
      {
88
      mQueues.abortAllEffects();
89

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

    
92
      mQueues.move( new Static3D( factor*mObjHeight*0.58f , factor*mObjHeight*0.08f , 0) );
93
      mQueues.scale(factor);
94

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

    
99
      mQueues.rotate(angle, axis, center);
100

    
101
      mEffects.apply(mQueues,mWind);
102

    
103
      Distorted.onSurfaceChanged(width, height);
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
109
      {
110
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
111

    
112
      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
      mTexture.setTexture(bitmap);
129

    
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
}
(3-3/4)