Project

General

Profile

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

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

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 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
25 14122c52 Leszek Koltunski
import android.opengl.GLSurfaceView;
26
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
30 d218d64e leszek
import org.distorted.library.DistortedScreen;
31 b01acdaf Leszek Koltunski
import org.distorted.library.MeshCubes;
32 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
33 40eef4b9 Leszek Koltunski
import org.distorted.library.DistortedTexture;
34 815687bb Leszek Koltunski
import org.distorted.library.type.Static1D;
35 14122c52 Leszek Koltunski
import org.distorted.library.type.Static3D;
36
37
import java.io.IOException;
38
import java.io.InputStream;
39
40
import javax.microedition.khronos.egl.EGLConfig;
41
import javax.microedition.khronos.opengles.GL10;
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45
class WindRenderer implements GLSurfaceView.Renderer
46
{
47
   private GLSurfaceView mView;
48 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
49 40eef4b9 Leszek Koltunski
   private DistortedTexture mTexture;
50 d218d64e leszek
   private DistortedScreen mScreen;
51 b01acdaf Leszek Koltunski
   private MeshCubes mMesh;
52 392e16fd Leszek Koltunski
   private WindEffectsManager mManager;
53 14122c52 Leszek Koltunski
   private int mObjWidth, mObjHeight;
54 21819119 Leszek Koltunski
   private int mWind;
55 14122c52 Leszek Koltunski
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58
   WindRenderer(GLSurfaceView view)
59
      { 
60
      mView = view;
61
62 b01acdaf Leszek Koltunski
      mMesh    = new MeshCubes(50,30,false);
63 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
64 7451c98a Leszek Koltunski
      mTexture = new DistortedTexture(50,30);
65 392e16fd Leszek Koltunski
      mManager = new WindEffectsManager(mTexture);
66 d218d64e leszek
      mScreen  = new DistortedScreen();
67 14122c52 Leszek Koltunski
68 40eef4b9 Leszek Koltunski
      mObjWidth = mTexture.getWidth();
69
      mObjHeight= mTexture.getHeight();
70 14122c52 Leszek Koltunski
      }
71
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74
   void setWind(int wind)
75
      {
76 21819119 Leszek Koltunski
      mWind = wind;
77 392e16fd Leszek Koltunski
      mManager.setWind(mWind);
78 14122c52 Leszek Koltunski
      }
79
   
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
   
82
   public void onDrawFrame(GL10 glUnused) 
83
      {
84 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
85 b01acdaf Leszek Koltunski
      mScreen.renderTo(mTexture, mMesh, mEffects, System.currentTimeMillis() );
86 14122c52 Leszek Koltunski
      }
87
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
    
90
   public void onSurfaceChanged(GL10 glUnused, int width, int height) 
91
      {
92 392e16fd Leszek Koltunski
      mEffects.abortAllEffects();
93 21819119 Leszek Koltunski
94 b041d424 Leszek Koltunski
      float factor = ( (float)(width<height? width:height) )/(mObjHeight + 1.4f*mObjWidth);
95 14122c52 Leszek Koltunski
96 392e16fd Leszek Koltunski
      mEffects.move( new Static3D( factor*mObjHeight*0.58f , factor*mObjHeight*0.08f , 0) );
97
      mEffects.scale(factor);
98 14122c52 Leszek Koltunski
99 815687bb Leszek Koltunski
      Static1D angle = new Static1D(-45);
100
      Static3D axis  = new Static3D(0,0,1);
101
      Static3D center= new Static3D(0,mObjHeight/2,0);
102
103 392e16fd Leszek Koltunski
      mEffects.rotate(angle, axis, center);
104
      mManager.apply(mEffects,mWind);
105
      mScreen.resize(width, height);
106 14122c52 Leszek Koltunski
      }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
111 e7a4ef16 Leszek Koltunski
      {
112 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
113 e7a4ef16 Leszek Koltunski
114 14122c52 Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.iceland);
115
      Bitmap bitmap;
116
117
      try
118
        {
119
        bitmap = BitmapFactory.decodeStream(is);
120
        }
121
      finally
122
        {
123
        try
124
          {
125
          is.close();
126
          }
127
        catch(IOException e) { }
128
        }
129
130 40eef4b9 Leszek Koltunski
      mTexture.setTexture(bitmap);
131 14122c52 Leszek Koltunski
132
      try
133
        {
134 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
135 14122c52 Leszek Koltunski
        }
136
      catch(Exception ex)
137
        {
138
        android.util.Log.e("Wind", ex.getMessage() );
139
        }
140
      }
141
}