Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 30f337e5

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.content.res.Resources;
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46
import android.opengl.GLSurfaceView;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
// Show how to use the notifyWhenFinished / effectFinished APIs
50

    
51
class ListenerRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.LibraryUser
52
{
53
   private final int NUM_CONCURRENT_BUBBLES = 12;
54

    
55
   private final GLSurfaceView mView;
56
   private final Resources mResources;
57
   private final DistortedEffects mEffects;
58
   private final DistortedScreen mScreen;
59
   private final DistortedTexture mTexture;
60
   private final Random mRnd;
61
   private final Static3D mScale;
62

    
63
   private MeshSquare mMesh;
64
   private float mBmpRatio;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
   ListenerRenderer(GLSurfaceView v)
69
      {
70
      mView = v;
71
      mResources = v.getResources();
72

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

    
80
      for(int i=0; i<NUM_CONCURRENT_BUBBLES; i++)
81
        {
82
        VertexEffectDistort newEffect = createNewBubble();
83

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

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

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

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

    
106
      Static3D center = new Static3D(pointx,pointy,0);
107
      Static4D region = new Static4D(0,0,0,radius);
108
      VertexEffectDistort distort = new VertexEffectDistort(dDistort,center,region);
109

    
110
      // We want to know when this effect finishes so that we can add a new one.
111
      distort.notifyWhenFinished(this);
112

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

    
122
   public void effectFinished(final long effectID)
123
     {
124
     mEffects.abortById(effectID);
125

    
126
     VertexEffectDistort newEffect = createNewBubble();
127

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

    
136
   public void onDrawFrame(GL10 glUnused)
137
      {
138
      mScreen.render( System.currentTimeMillis() );
139
      }
140

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

    
148
     mScreen.resize(width, height);
149
     }
150

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

    
171
     mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
172
     mTexture.setTexture(bitmap);
173

    
174
     if( mMesh==null ) mMesh = new MeshSquare(50, (int)(50*mBmpRatio) );
175

    
176
     mScreen.detachAll();
177
     mScreen.attach(mTexture,mEffects,mMesh);
178

    
179
     VertexEffectDistort.enable();
180

    
181
     DistortedLibrary.onSurfaceCreated(this);
182
     }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
   public void distortedException(Exception ex)
187
     {
188
     android.util.Log.e("Listener", ex.getMessage() );
189
     }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    public InputStream localFile(int fileID)
194
      {
195
      return mResources.openRawResource(fileID);
196
      }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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