Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 6979a0e0

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

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

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

    
54
   private GLSurfaceView mView;
55
   private DistortedEffects mEffects;
56
   private DistortedScreen mScreen;
57
   private DistortedTexture mTexture;
58
   private MeshSquare mMesh;
59
   private Random mRnd;
60
   private Static3D mScale;
61
   private float mBmpRatio;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
   ListenerRenderer(GLSurfaceView v)
66
      {
67
      mView = v;
68

    
69
      mTexture = new DistortedTexture();
70
      mScreen  = new DistortedScreen();
71
      mRnd     = new Random(0);
72
      mScale   = new Static3D(1,1,1);
73
      mEffects = new DistortedEffects();
74
      mEffects.apply(new MatrixEffectScale(mScale));
75

    
76
      for(int i=0; i<NUM_CONCURRENT_BUBBLES; i++)
77
        {
78
        VertexEffectDistort newEffect = createNewBubble();
79

    
80
        if( !mEffects.apply(newEffect) )
81
          {
82
          android.util.Log.e("listener", "failed to add bubble - this should never happen!");
83
          }
84
        }
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

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

    
97
      // '1.0f' i.e. from (0,0,0) to (0,0,height) and back to (0,0,0) - full circle.
98
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
99
      dDistort.add(new Static3D(0,0,     0));
100
      dDistort.add(new Static3D(0,0,height));
101

    
102
      Static3D center = new Static3D(pointx,pointy,0);
103
      Static4D region = new Static4D(0,0,0,radius);
104
      VertexEffectDistort distort = new VertexEffectDistort(dDistort,center,region);
105

    
106
      // We want to know when this effect finishes so that we can add a new one.
107
      distort.notifyWhenFinished(this);
108

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

    
118
   public void effectFinished(final long effectID)
119
     {
120
     mEffects.abortById(effectID);
121

    
122
     VertexEffectDistort newEffect = createNewBubble();
123

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

    
132
   public void onDrawFrame(GL10 glUnused)
133
      {
134
      mScreen.render( System.currentTimeMillis() );
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
    
139
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
140
     {
141
     if( width<height ) mScale.set( width,   width*mBmpRatio, width  );
142
     else               mScale.set( height/mBmpRatio, height, height );
143

    
144
     mScreen.resize(width, height);
145
     }
146

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

    
167
     mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
168
     mTexture.setTexture(bitmap);
169

    
170
     if( mMesh==null ) mMesh = new MeshSquare(50, (int)(50*mBmpRatio) );
171

    
172
     mScreen.detachAll();
173
     mScreen.attach(mTexture,mEffects,mMesh);
174

    
175
     VertexEffectDistort.enable();
176

    
177
     DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
178
     }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
   public void distortedException(Exception ex)
183
     {
184
     android.util.Log.e("Listener", ex.getMessage() );
185
     }
186
}
(2-2/3)