Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 46a0999e

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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
44
import android.content.Context;
45
import android.content.pm.ConfigurationInfo;
46
import android.content.res.Resources;
47 427ab7bf Leszek Koltunski
import android.graphics.Bitmap;
48
import android.graphics.BitmapFactory;
49
import android.opengl.GLSurfaceView;
50
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52 6979a0e0 Leszek Koltunski
// Show how to use the notifyWhenFinished / effectFinished APIs
53 427ab7bf Leszek Koltunski
54 dc10a48d Leszek Koltunski
class ListenerRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.LibraryUser
55 427ab7bf Leszek Koltunski
{
56 d381aa80 Leszek Koltunski
   private final int NUM_CONCURRENT_BUBBLES = 12;
57 e7a4ef16 Leszek Koltunski
58 dc10a48d Leszek Koltunski
   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 f2085b96 Leszek Koltunski
   private MeshSquare mMesh;
67 e052248f Leszek Koltunski
   private float mBmpRatio;
68 e7a4ef16 Leszek Koltunski
69 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 e7a4ef16 Leszek Koltunski
   ListenerRenderer(GLSurfaceView v)
72 427ab7bf Leszek Koltunski
      {
73
      mView = v;
74 dc10a48d Leszek Koltunski
      mResources = v.getResources();
75 687263cc Leszek Koltunski
76 e052248f Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96 e052248f Leszek Koltunski
   private VertexEffectDistort createNewBubble()
97 427ab7bf Leszek Koltunski
      {
98 e052248f Leszek Koltunski
      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 7bf107f7 Leszek Koltunski
104 e052248f Leszek Koltunski
      // '1.0f' i.e. from (0,0,0) to (0,0,height) and back to (0,0,0) - full circle.
105 f988589e Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
106 7bf107f7 Leszek Koltunski
      dDistort.add(new Static3D(0,0,     0));
107
      dDistort.add(new Static3D(0,0,height));
108
109 e052248f Leszek Koltunski
      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 8d5a8e06 Leszek Koltunski
      distort.notifyWhenFinished(this);
115
116 e052248f Leszek Koltunski
      return distort;
117 427ab7bf Leszek Koltunski
      }
118
   
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120 e052248f Leszek Koltunski
// 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 427ab7bf Leszek Koltunski
125 8d5a8e06 Leszek Koltunski
   public void effectFinished(final long effectID)
126 427ab7bf Leszek Koltunski
     {
127 8d5a8e06 Leszek Koltunski
     mEffects.abortById(effectID);
128
129 e052248f Leszek Koltunski
     VertexEffectDistort newEffect = createNewBubble();
130
131
     if( !mEffects.apply(newEffect) )
132 8d5a8e06 Leszek Koltunski
       {
133
       android.util.Log.e("Listener", "failed to add new bubble - this should never happen!");
134
       }
135 427ab7bf Leszek Koltunski
     }
136
   
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
139 e052248f Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
140 427ab7bf Leszek Koltunski
      {
141 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
142 427ab7bf Leszek Koltunski
      }
143
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
    
146 3a91bfe1 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
147
     {
148 c7a31368 Leszek Koltunski
     if( width<height ) mScale.set( width,   width*mBmpRatio, width  );
149
     else               mScale.set( height/mBmpRatio, height, height );
150 3a91bfe1 Leszek Koltunski
151
     mScreen.resize(width, height);
152
     }
153 427ab7bf Leszek Koltunski
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
    
156 3a91bfe1 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
157
     {
158
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
159
     Bitmap bitmap;
160 427ab7bf Leszek Koltunski
        
161 3a91bfe1 Leszek Koltunski
     try
162
       {
163
       bitmap = BitmapFactory.decodeStream(is);
164
       }
165
     finally
166
       {
167
       try
168
         {
169
         is.close();
170
         }
171 6979a0e0 Leszek Koltunski
       catch(IOException ignored) { }
172 3a91bfe1 Leszek Koltunski
       }
173
174 e052248f Leszek Koltunski
     mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
175 3a91bfe1 Leszek Koltunski
     mTexture.setTexture(bitmap);
176
177 f2085b96 Leszek Koltunski
     if( mMesh==null ) mMesh = new MeshSquare(50, (int)(50*mBmpRatio) );
178 687263cc Leszek Koltunski
179 3a91bfe1 Leszek Koltunski
     mScreen.detachAll();
180
     mScreen.attach(mTexture,mEffects,mMesh);
181
182 885b9cca leszek
     VertexEffectDistort.enable();
183 3a91bfe1 Leszek Koltunski
184 dc10a48d Leszek Koltunski
     DistortedLibrary.onSurfaceCreated(this);
185 061449ed Leszek Koltunski
     }
186
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
189
   public void distortedException(Exception ex)
190
     {
191
     android.util.Log.e("Listener", ex.getMessage() );
192 3a91bfe1 Leszek Koltunski
     }
193 dc10a48d Leszek Koltunski
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 427ab7bf Leszek Koltunski
}