Project

General

Profile

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

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

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();
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.abortEffects(EffectTypes.MATRIX);
87
      mObject.abortEffects(EffectTypes.VERTEX);
88
      mObject.abortEffects(EffectTypes.FRAGMENT);
89

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

    
92
      mObject.move( new Static3D( (width-factor*mObjWidth)/2 + width/8 , (height-factor*mObjHeight)/2 -height/4 , 0) );
93
      mObject.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
      mObject.rotate(angle, axis, center);
100

    
101
      mEffects.apply(mObject, mWind);
102

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

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

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

    
126
      mObject.setBitmap(bitmap);
127

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