Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 625c67de

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.listener;
21

    
22
import java.io.IOException;
23
import java.io.InputStream;
24
import java.util.Random;
25

    
26
import javax.microedition.khronos.egl.EGLConfig;
27
import javax.microedition.khronos.opengles.GL10;
28

    
29
import org.distorted.examples.R;
30

    
31
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.effect.VertexEffectDistort;
33
import org.distorted.library.main.DistortedLibrary;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.mesh.MeshSquare;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.type.Dynamic3D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41
import org.distorted.library.message.EffectListener;
42

    
43
import android.app.ActivityManager;
44
import android.content.Context;
45
import android.content.pm.ConfigurationInfo;
46
import android.content.res.Resources;
47
import android.graphics.Bitmap;
48
import android.graphics.BitmapFactory;
49
import android.opengl.GLSurfaceView;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
// Show how to use the notifyWhenFinished / effectFinished APIs
53

    
54
class ListenerRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.LibraryUser
55
{
56
   private final int NUM_CONCURRENT_BUBBLES = 12;
57

    
58
   private final GLSurfaceView mView;
59
   private final Resources mResources;
60
   private final DistortedEffects mEffects;
61
   private final DistortedScreen mScreen;
62
   private final DistortedTexture mTexture;
63
   private final Random mRnd;
64
   private final Static3D mScale;
65

    
66
   private MeshSquare mMesh;
67
   private float mBmpRatio;
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
   ListenerRenderer(GLSurfaceView v)
72
      {
73
      mView = v;
74
      mResources = v.getResources();
75

    
76
      mTexture = new DistortedTexture();
77
      mScreen  = new DistortedScreen();
78
      mRnd     = new Random(0);
79
      mScale   = new Static3D(1,1,1);
80
      mEffects = new DistortedEffects();
81
      mEffects.apply(new MatrixEffectScale(mScale));
82

    
83
      for(int i=0; i<NUM_CONCURRENT_BUBBLES; i++)
84
        {
85
        VertexEffectDistort newEffect = createNewBubble();
86

    
87
        if( !mEffects.apply(newEffect) )
88
          {
89
          android.util.Log.e("listener", "failed to add bubble - this should never happen!");
90
          }
91
        }
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
   private VertexEffectDistort createNewBubble()
97
      {
98
      float radius =  0.10f + 0.70f*mRnd.nextFloat() ; // pop up a bubble of size (radius,height)
99
      float height = -0.10f + 0.20f*mRnd.nextFloat() ; //
100
      float pointx =  0.80f * (mRnd.nextFloat()-0.5f); // at a random place on the bitmap (but not near the edge)
101
      float pointy =  0.80f * (mRnd.nextFloat()-0.5f); //
102
      int duration =   1000 + mRnd.nextInt(3000);      // for anytime from 1 to 4 seconds
103

    
104
      // '1.0f' i.e. from (0,0,0) to (0,0,height) and back to (0,0,0) - full circle.
105
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
106
      dDistort.add(new Static3D(0,0,     0));
107
      dDistort.add(new Static3D(0,0,height));
108

    
109
      Static3D center = new Static3D(pointx,pointy,0);
110
      Static4D region = new Static4D(0,0,0,radius);
111
      VertexEffectDistort distort = new VertexEffectDistort(dDistort,center,region);
112

    
113
      // We want to know when this effect finishes so that we can add a new one.
114
      distort.notifyWhenFinished(this);
115

    
116
      return distort;
117
      }
118
   
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
// An effect just finished, i.e. its Dynamic 'dDistort' reached its end point.
121
// The ID of the finished effect is 'effectID' but here we know it must be one of the Distorts
122
// and we don't care which one it is - just remove the finished effect from the Effect queues and
123
// add a new one right back in.
124

    
125
   public void effectFinished(final long effectID)
126
     {
127
     mEffects.abortById(effectID);
128

    
129
     VertexEffectDistort newEffect = createNewBubble();
130

    
131
     if( !mEffects.apply(newEffect) )
132
       {
133
       android.util.Log.e("Listener", "failed to add new bubble - this should never happen!");
134
       }
135
     }
136
   
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
   public void onDrawFrame(GL10 glUnused)
140
      {
141
      mScreen.render( System.currentTimeMillis() );
142
      }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
    
146
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
147
     {
148
     if( width<height ) mScale.set( width,   width*mBmpRatio, width  );
149
     else               mScale.set( height/mBmpRatio, height, height );
150

    
151
     mScreen.resize(width, height);
152
     }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
    
156
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
157
     {
158
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
159
     Bitmap bitmap;
160
        
161
     try
162
       {
163
       bitmap = BitmapFactory.decodeStream(is);
164
       }
165
     finally
166
       {
167
       try
168
         {
169
         is.close();
170
         }
171
       catch(IOException ignored) { }
172
       }
173

    
174
     mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
175
     mTexture.setTexture(bitmap);
176

    
177
     if( mMesh==null ) mMesh = new MeshSquare(50, (int)(50*mBmpRatio) );
178

    
179
     mScreen.detachAll();
180
     mScreen.attach(mTexture,mEffects,mMesh);
181

    
182
     VertexEffectDistort.enable();
183

    
184
     DistortedLibrary.onSurfaceCreated(this);
185
     }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
   public void distortedException(Exception ex)
190
     {
191
     android.util.Log.e("Listener", ex.getMessage() );
192
     }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
    public InputStream localFile(int fileID)
197
      {
198
      return mResources.openRawResource(fileID);
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
    public void logMessage(String message)
204
      {
205
      android.util.Log.e("Listener", message );
206
      }
207
}
(2-2/3)