Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 849e0034

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.EffectType;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectDistort;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.mesh.MeshRectangles;
37
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.type.Dynamic3D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42
import org.distorted.library.message.EffectListener;
43

    
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46
import android.opengl.GLSurfaceView;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class ListenerRenderer implements GLSurfaceView.Renderer,EffectListener 
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 MeshRectangles mMesh;
59
   private int bmpHeight, bmpWidth;
60
   private Random mRnd;
61
   private Static3D mScale;
62

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

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

    
69
      mScreen = new DistortedScreen();
70
      mRnd = new Random(0);
71
      mScale= new Static3D(1,1,1);
72
      }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
   private boolean addNewBubble()
77
      {
78
      int radius   = (int)(( 0.10f + 0.70f*mRnd.nextFloat())*bmpWidth); // pop up a bubble of size (radius,height)
79
      int height   = (int)((-0.10f + 0.20f*mRnd.nextFloat())*bmpWidth); //
80
      int pointx   = (int)(0.8f * bmpWidth * (mRnd.nextFloat()-0.5f));  // at a random place on the bitmap (but not near the edge)
81
      int pointy   = (int)(0.8f * bmpHeight* (mRnd.nextFloat()-0.5f));  //
82
      int duration = 1000 + mRnd.nextInt(3000);                         // for anytime from 1 to 4 seconds
83

    
84
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
85
      dDistort.add(new Static3D(0,0,     0));
86
      dDistort.add(new Static3D(0,0,height));
87

    
88
      VertexEffectDistort distort = new VertexEffectDistort(dDistort, new Static3D(pointx,pointy,0), new Static4D(0,0,0,radius));
89
      distort.notifyWhenFinished(this);
90

    
91
      return mEffects.apply(distort);
92
      }
93
   
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
   public void effectFinished(final long effectID)
97
     {
98
     mEffects.abortById(effectID);
99

    
100
     if( !addNewBubble() )
101
       {
102
       android.util.Log.e("Listener", "failed to add new bubble - this should never happen!");
103
       }
104
     }
105
   
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
    public void onDrawFrame(GL10 glUnused) 
109
      {
110
      mScreen.render( System.currentTimeMillis() );
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
116
     {
117
     float horiRatio = (float)width / mTexture.getWidth();
118
     float vertRatio = (float)height/ mTexture.getHeight();
119
     float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
120

    
121
     mScale.set( factor,factor,factor );
122
     mScreen.resize(width, height);
123
     }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
    
127
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
128
     {
129
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
130
     Bitmap bitmap;
131
        
132
     try
133
       {
134
       bitmap = BitmapFactory.decodeStream(is);
135
       }
136
     finally
137
       {
138
       try
139
         {
140
         is.close();
141
         }
142
       catch(IOException e) { }
143
       }
144

    
145
     bmpHeight = bitmap.getHeight();
146
     bmpWidth  = bitmap.getWidth();
147

    
148
     if( mTexture==null ) mTexture = new DistortedTexture();
149
     mTexture.setTexture(bitmap);
150

    
151
     if( mMesh==null )
152
       {
153
       mMesh = new MeshRectangles(50,50*bmpHeight/bmpWidth);
154
       mMesh.setStretch(bmpWidth,bmpHeight,0);
155
       }
156

    
157
     if( mEffects==null )
158
       {
159
       mEffects = new DistortedEffects();
160
       mEffects.apply(new MatrixEffectScale(mScale));
161
       }
162

    
163
     mScreen.detachAll();
164
     mScreen.attach(mTexture,mEffects,mMesh);
165

    
166
     for(int i = 0; i< NUM_CONCURRENT_BUBBLES; i++) addNewBubble();
167

    
168
     // one more than we have bubbles at any given time because it can sometimes
169
     // happen that the old bubble is not yet removed when we add a new one
170
     DistortedLibrary.setMax(EffectType.VERTEX,NUM_CONCURRENT_BUBBLES+1);
171
     VertexEffectDistort.enable();
172

    
173
     try
174
       {
175
       DistortedLibrary.onCreate(mView.getContext());
176
       }
177
     catch(Exception ex)
178
       {
179
       android.util.Log.e("Listener", ex.getMessage() );
180
       }
181
     }
182
}
(2-2/3)