Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 061449ed

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.listener;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
30 427ab7bf Leszek Koltunski
31 d76638f1 leszek
import org.distorted.library.effect.EffectType;
32 20e888ea Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectDistort;
34 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
35 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
36 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
37 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.main.DistortedEffects;
39 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
40 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42 13efa930 Leszek Koltunski
import org.distorted.library.message.EffectListener;
43 427ab7bf Leszek Koltunski
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46
import android.opengl.GLSurfaceView;
47
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49 e052248f Leszek Koltunski
// Show how to use the setMax, notifyWhenFinished / effectFinished APIs
50 427ab7bf Leszek Koltunski
51 061449ed Leszek Koltunski
class ListenerRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.ExceptionListener
52 427ab7bf Leszek Koltunski
{
53 d381aa80 Leszek Koltunski
   private final int NUM_CONCURRENT_BUBBLES = 12;
54 e7a4ef16 Leszek Koltunski
55 427ab7bf Leszek Koltunski
   private GLSurfaceView mView;
56 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
57 d218d64e leszek
   private DistortedScreen mScreen;
58 b0ebdf5e Leszek Koltunski
   private DistortedTexture mTexture;
59 f3ca895f Leszek Koltunski
   private MeshRectangles mMesh;
60 427ab7bf Leszek Koltunski
   private Random mRnd;
61 d381aa80 Leszek Koltunski
   private Static3D mScale;
62 e052248f Leszek Koltunski
   private float mBmpRatio;
63 e7a4ef16 Leszek Koltunski
64 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 e7a4ef16 Leszek Koltunski
   ListenerRenderer(GLSurfaceView v)
67 427ab7bf Leszek Koltunski
      {
68
      mView = v;
69 687263cc Leszek Koltunski
70 e052248f Leszek Koltunski
      mTexture = new DistortedTexture();
71
      mScreen  = new DistortedScreen();
72
      mRnd     = new Random(0);
73
      mScale   = new Static3D(1,1,1);
74
      mEffects = new DistortedEffects();
75
      mEffects.apply(new MatrixEffectScale(mScale));
76
77
      for(int i=0; i<NUM_CONCURRENT_BUBBLES; i++)
78
        {
79
        VertexEffectDistort newEffect = createNewBubble();
80
81
        if( !mEffects.apply(newEffect) )
82
          {
83
          android.util.Log.e("listener", "failed to add bubble - this should never happen!");
84
          }
85
        }
86 427ab7bf Leszek Koltunski
      }
87
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
90 e052248f Leszek Koltunski
   private VertexEffectDistort createNewBubble()
91 427ab7bf Leszek Koltunski
      {
92 e052248f Leszek Koltunski
      float radius =  0.10f + 0.70f*mRnd.nextFloat() ; // pop up a bubble of size (radius,height)
93
      float height = -0.10f + 0.20f*mRnd.nextFloat() ; //
94
      float pointx =  0.80f * (mRnd.nextFloat()-0.5f); // at a random place on the bitmap (but not near the edge)
95
      float pointy =  0.80f * (mRnd.nextFloat()-0.5f); //
96
      int duration =   1000 + mRnd.nextInt(3000);      // for anytime from 1 to 4 seconds
97 7bf107f7 Leszek Koltunski
98 e052248f Leszek Koltunski
      // '1.0f' i.e. from (0,0,0) to (0,0,height) and back to (0,0,0) - full circle.
99 f988589e Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
100 7bf107f7 Leszek Koltunski
      dDistort.add(new Static3D(0,0,     0));
101
      dDistort.add(new Static3D(0,0,height));
102
103 e052248f Leszek Koltunski
      Static3D center = new Static3D(pointx,pointy,0);
104
      Static4D region = new Static4D(0,0,0,radius);
105
      VertexEffectDistort distort = new VertexEffectDistort(dDistort,center,region);
106
107
      // We want to know when this effect finishes so that we can add a new one.
108 8d5a8e06 Leszek Koltunski
      distort.notifyWhenFinished(this);
109
110 e052248f Leszek Koltunski
      return distort;
111 427ab7bf Leszek Koltunski
      }
112
   
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114 e052248f Leszek Koltunski
// An effect just finished, i.e. its Dynamic 'dDistort' reached its end point.
115
// The ID of the finished effect is 'effectID' but here we know it must be one of the Distorts
116
// and we don't care which one it is - just remove the finished effect from the Effect queues and
117
// add a new one right back in.
118 427ab7bf Leszek Koltunski
119 8d5a8e06 Leszek Koltunski
   public void effectFinished(final long effectID)
120 427ab7bf Leszek Koltunski
     {
121 8d5a8e06 Leszek Koltunski
     mEffects.abortById(effectID);
122
123 e052248f Leszek Koltunski
     VertexEffectDistort newEffect = createNewBubble();
124
125
     if( !mEffects.apply(newEffect) )
126 8d5a8e06 Leszek Koltunski
       {
127
       android.util.Log.e("Listener", "failed to add new bubble - this should never happen!");
128
       }
129 427ab7bf Leszek Koltunski
     }
130
   
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
133 e052248f Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
134 427ab7bf Leszek Koltunski
      {
135 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
136 427ab7bf Leszek Koltunski
      }
137
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140 3a91bfe1 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
141
     {
142 c7a31368 Leszek Koltunski
     if( width<height ) mScale.set( width,   width*mBmpRatio, width  );
143
     else               mScale.set( height/mBmpRatio, height, height );
144 3a91bfe1 Leszek Koltunski
145
     mScreen.resize(width, height);
146
     }
147 427ab7bf Leszek Koltunski
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150 3a91bfe1 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
151
     {
152
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
153
     Bitmap bitmap;
154 427ab7bf Leszek Koltunski
        
155 3a91bfe1 Leszek Koltunski
     try
156
       {
157
       bitmap = BitmapFactory.decodeStream(is);
158
       }
159
     finally
160
       {
161
       try
162
         {
163
         is.close();
164
         }
165
       catch(IOException e) { }
166
       }
167
168 e052248f Leszek Koltunski
     mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
169 3a91bfe1 Leszek Koltunski
     mTexture.setTexture(bitmap);
170
171 e052248f Leszek Koltunski
     if( mMesh==null ) mMesh = new MeshRectangles(50, (int)(50*mBmpRatio) );
172 687263cc Leszek Koltunski
173 3a91bfe1 Leszek Koltunski
     mScreen.detachAll();
174
     mScreen.attach(mTexture,mEffects,mMesh);
175
176 e052248f Leszek Koltunski
     // Normally we can only hold no more than 5 (see EffectType.reset()) Vertex
177
     // effects at any given time. Here we want 12, so we need to increase the default.
178
     // We need to call this before we call onCreate; best done here.
179
     // After onCreate we can also call this but only to decrease this value!
180
     //
181
     // One more than we have bubbles at any given time because it can sometimes
182
     // happen that the old bubble is not yet removed when we add a new one.
183 386fd702 Leszek Koltunski
     DistortedLibrary.setMax(EffectType.VERTEX,NUM_CONCURRENT_BUBBLES+1);
184 885b9cca leszek
     VertexEffectDistort.enable();
185 3a91bfe1 Leszek Koltunski
186 061449ed Leszek Koltunski
     DistortedLibrary.onCreate(mView.getContext(), this);
187
     }
188
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
191
   public void distortedException(Exception ex)
192
     {
193
     android.util.Log.e("Listener", ex.getMessage() );
194 3a91bfe1 Leszek Koltunski
     }
195 427ab7bf Leszek Koltunski
}