Project

General

Profile

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

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

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 20e888ea Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.effect.VertexEffectDistort;
33 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
34 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
35 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
36 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.main.DistortedEffects;
38 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
39 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41 13efa930 Leszek Koltunski
import org.distorted.library.message.EffectListener;
42 427ab7bf Leszek Koltunski
43
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
45
import android.opengl.GLSurfaceView;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48 6979a0e0 Leszek Koltunski
// Show how to use the notifyWhenFinished / effectFinished APIs
49 427ab7bf Leszek Koltunski
50 061449ed Leszek Koltunski
class ListenerRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.ExceptionListener
51 427ab7bf Leszek Koltunski
{
52 d381aa80 Leszek Koltunski
   private final int NUM_CONCURRENT_BUBBLES = 12;
53 e7a4ef16 Leszek Koltunski
54 427ab7bf Leszek Koltunski
   private GLSurfaceView mView;
55 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
56 d218d64e leszek
   private DistortedScreen mScreen;
57 b0ebdf5e Leszek Koltunski
   private DistortedTexture mTexture;
58 f2085b96 Leszek Koltunski
   private MeshSquare mMesh;
59 427ab7bf Leszek Koltunski
   private Random mRnd;
60 d381aa80 Leszek Koltunski
   private Static3D mScale;
61 e052248f Leszek Koltunski
   private float mBmpRatio;
62 e7a4ef16 Leszek Koltunski
63 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65 e7a4ef16 Leszek Koltunski
   ListenerRenderer(GLSurfaceView v)
66 427ab7bf Leszek Koltunski
      {
67
      mView = v;
68 687263cc Leszek Koltunski
69 e052248f Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89 e052248f Leszek Koltunski
   private VertexEffectDistort createNewBubble()
90 427ab7bf Leszek Koltunski
      {
91 e052248f Leszek Koltunski
      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 7bf107f7 Leszek Koltunski
97 e052248f Leszek Koltunski
      // '1.0f' i.e. from (0,0,0) to (0,0,height) and back to (0,0,0) - full circle.
98 f988589e Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
99 7bf107f7 Leszek Koltunski
      dDistort.add(new Static3D(0,0,     0));
100
      dDistort.add(new Static3D(0,0,height));
101
102 e052248f Leszek Koltunski
      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 8d5a8e06 Leszek Koltunski
      distort.notifyWhenFinished(this);
108
109 e052248f Leszek Koltunski
      return distort;
110 427ab7bf Leszek Koltunski
      }
111
   
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113 e052248f Leszek Koltunski
// 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 427ab7bf Leszek Koltunski
118 8d5a8e06 Leszek Koltunski
   public void effectFinished(final long effectID)
119 427ab7bf Leszek Koltunski
     {
120 8d5a8e06 Leszek Koltunski
     mEffects.abortById(effectID);
121
122 e052248f Leszek Koltunski
     VertexEffectDistort newEffect = createNewBubble();
123
124
     if( !mEffects.apply(newEffect) )
125 8d5a8e06 Leszek Koltunski
       {
126
       android.util.Log.e("Listener", "failed to add new bubble - this should never happen!");
127
       }
128 427ab7bf Leszek Koltunski
     }
129
   
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
132 e052248f Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
133 427ab7bf Leszek Koltunski
      {
134 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
135 427ab7bf Leszek Koltunski
      }
136
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
    
139 3a91bfe1 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
140
     {
141 c7a31368 Leszek Koltunski
     if( width<height ) mScale.set( width,   width*mBmpRatio, width  );
142
     else               mScale.set( height/mBmpRatio, height, height );
143 3a91bfe1 Leszek Koltunski
144
     mScreen.resize(width, height);
145
     }
146 427ab7bf Leszek Koltunski
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
    
149 3a91bfe1 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
150
     {
151
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
152
     Bitmap bitmap;
153 427ab7bf Leszek Koltunski
        
154 3a91bfe1 Leszek Koltunski
     try
155
       {
156
       bitmap = BitmapFactory.decodeStream(is);
157
       }
158
     finally
159
       {
160
       try
161
         {
162
         is.close();
163
         }
164 6979a0e0 Leszek Koltunski
       catch(IOException ignored) { }
165 3a91bfe1 Leszek Koltunski
       }
166
167 e052248f Leszek Koltunski
     mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
168 3a91bfe1 Leszek Koltunski
     mTexture.setTexture(bitmap);
169
170 f2085b96 Leszek Koltunski
     if( mMesh==null ) mMesh = new MeshSquare(50, (int)(50*mBmpRatio) );
171 687263cc Leszek Koltunski
172 3a91bfe1 Leszek Koltunski
     mScreen.detachAll();
173
     mScreen.attach(mTexture,mEffects,mMesh);
174
175 885b9cca leszek
     VertexEffectDistort.enable();
176 3a91bfe1 Leszek Koltunski
177 7c72e798 Leszek Koltunski
     DistortedLibrary.onSurfaceCreated(mView.getContext(), this);
178 061449ed Leszek Koltunski
     }
179
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
182
   public void distortedException(Exception ex)
183
     {
184
     android.util.Log.e("Listener", ex.getMessage() );
185 3a91bfe1 Leszek Koltunski
     }
186 427ab7bf Leszek Koltunski
}