Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 13efa930

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 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 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
32
import org.distorted.library.DistortedBitmap;
33 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
34 08eabc44 Leszek Koltunski
import org.distorted.library.type.Float2D;
35
import org.distorted.library.type.Float3D;
36
import org.distorted.library.type.Float4D;
37 13efa930 Leszek Koltunski
import org.distorted.library.message.EffectListener;
38
import org.distorted.library.message.EffectMessage;
39 427ab7bf Leszek Koltunski
40
import android.graphics.Bitmap;
41
import android.graphics.BitmapFactory;
42
import android.opengl.GLES20;
43
import android.opengl.GLSurfaceView;
44
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47
class ListenerRenderer implements GLSurfaceView.Renderer,EffectListener 
48
{
49
   private GLSurfaceView mView;
50
   private DistortedBitmap water;
51
   private int bmpHeight, bmpWidth;
52
   private Random mRnd;
53
    
54
   private final int NUM_BUBBLES = 12;
55
    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58
   public ListenerRenderer(GLSurfaceView v) 
59
      {
60
      Distorted.setMaxVertex(NUM_BUBBLES);   
61
      mView = v;
62
      }
63
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66
   private long randomizeNewBubble()
67
      {
68 a8c3ada7 Leszek Koltunski
      int radius   = (int)(( 0.10f + 0.70f*mRnd.nextFloat())*bmpWidth);           // pop up a bubble of size (radius,height)
69 427ab7bf Leszek Koltunski
      int height   = (int)((-0.10f + 0.20f*mRnd.nextFloat())*bmpWidth);           // 
70 a8c3ada7 Leszek Koltunski
      int pointx   = mRnd.nextInt( (int)(0.8f*bmpWidth ))+ (int)(0.1f*bmpWidth ); // at a random place on the bitmap (but not near the edge)
71 427ab7bf Leszek Koltunski
      int pointy   = mRnd.nextInt( (int)(0.8f*bmpHeight))+ (int)(0.1f*bmpHeight); // 
72
      int duration = 1000 + mRnd.nextInt(3000);                                   // for anytime from 3 to 4 seconds 
73
        
74
      Float3D dp3d = new Float3D(0,0,height);
75
      Float2D dp2d = new Float2D(pointx,pointy);
76
      Float4D  dr  = new Float4D(0,0,radius,radius);
77
     
78
      return water.distort(dp3d, dr, dp2d, duration, 1.0f);
79
      }
80
   
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82 b9615af9 Leszek Koltunski
// the library sending messages to us
83 427ab7bf Leszek Koltunski
84 b9615af9 Leszek Koltunski
   public void effectMessage(final EffectMessage em, final long effectID, final int effectName, final long bitmapID, final String message)
85 427ab7bf Leszek Koltunski
     {
86
     switch(em)
87
        {
88
        case EFFECT_REMOVED: randomizeNewBubble();
89
        default            : break;
90
        }
91
     }
92
   
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95
    public void onDrawFrame(GL10 glUnused) 
96
      {
97
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
98
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
99
      
100
      water.draw(System.currentTimeMillis());
101
      }
102
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
    
105
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
106
      { 
107 a8c3ada7 Leszek Koltunski
      water.abortEffects(EffectTypes.MATRIX);
108 427ab7bf Leszek Koltunski
         
109
      if( bmpHeight/bmpWidth > height/width )
110
        {
111
        int w = (height*bmpWidth)/bmpHeight;
112
        water.move((width-w)/2 ,0, 0);
113
        water.scale((float)height/bmpHeight);
114
        }
115
      else
116
        {
117
        int h = (width*bmpHeight)/bmpWidth;
118
        water.move(0 ,(height-h)/2, 0);
119
        water.scale((float)width/bmpWidth);
120
        }
121
      
122
      Distorted.onSurfaceChanged(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
      mRnd = new Random(0);
146
      
147
      bmpHeight = bitmap.getHeight();
148
      bmpWidth  = bitmap.getWidth();
149
      
150
      water = new DistortedBitmap(bitmap, 50);
151
      water.addEventListener(this);
152
      
153
      for(int i=0; i<NUM_BUBBLES; i++) randomizeNewBubble();
154
      
155
      try
156
        {
157 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
158 427ab7bf Leszek Koltunski
        }
159
      catch(Exception ex)
160
        {
161
        android.util.Log.e("Renderer", ex.getMessage() );
162
        }
163
      }
164
}