Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ b9615af9

1

    
2
package org.distorted.examples.listener;
3

    
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.util.Random;
7

    
8
import javax.microedition.khronos.egl.EGLConfig;
9
import javax.microedition.khronos.opengles.GL10;
10

    
11
import org.distorted.examples.R;
12

    
13
import org.distorted.library.Distorted;
14
import org.distorted.library.DistortedBitmap;
15
import org.distorted.library.EffectTypes;
16
import org.distorted.library.Float2D;
17
import org.distorted.library.Float3D;
18
import org.distorted.library.Float4D;
19
import org.distorted.library.EffectListener;
20
import org.distorted.library.EffectMessage;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLES20;
25
import android.opengl.GLSurfaceView;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
class ListenerRenderer implements GLSurfaceView.Renderer,EffectListener 
30
{
31
   private GLSurfaceView mView;
32
   private DistortedBitmap water;
33
   private int bmpHeight, bmpWidth;
34
   private Random mRnd;
35
    
36
   private final int NUM_BUBBLES = 12;
37
    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
   public ListenerRenderer(GLSurfaceView v) 
41
      {
42
      Distorted.setMaxVertex(NUM_BUBBLES);   
43
      mView = v;
44
      }
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
   private long randomizeNewBubble()
49
      {
50
      int radius   = (int)(( 0.10f + 0.70f*mRnd.nextFloat())*bmpWidth);           // pop up a bubble of size (radius,height)
51
      int height   = (int)((-0.10f + 0.20f*mRnd.nextFloat())*bmpWidth);           // 
52
      int pointx   = mRnd.nextInt( (int)(0.8f*bmpWidth ))+ (int)(0.1f*bmpWidth ); // at a random place on the bitmap (but not near the edge)
53
      int pointy   = mRnd.nextInt( (int)(0.8f*bmpHeight))+ (int)(0.1f*bmpHeight); // 
54
      int duration = 1000 + mRnd.nextInt(3000);                                   // for anytime from 3 to 4 seconds 
55
        
56
      Float3D dp3d = new Float3D(0,0,height);
57
      Float2D dp2d = new Float2D(pointx,pointy);
58
      Float4D  dr  = new Float4D(0,0,radius,radius);
59
     
60
      return water.distort(dp3d, dr, dp2d, duration, 1.0f);
61
      }
62
   
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
// the library sending messages to us
65

    
66
   public void effectMessage(final EffectMessage em, final long effectID, final int effectName, final long bitmapID, final String message)
67
     {
68
     switch(em)
69
        {
70
        case EFFECT_REMOVED: randomizeNewBubble();
71
        default            : break;
72
        }
73
     }
74
   
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
    public void onDrawFrame(GL10 glUnused) 
78
      {
79
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
80
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
81
      
82
      water.draw(System.currentTimeMillis());
83
      }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
    
87
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
88
      { 
89
      water.abortEffects(EffectTypes.MATRIX);
90
         
91
      if( bmpHeight/bmpWidth > height/width )
92
        {
93
        int w = (height*bmpWidth)/bmpHeight;
94
        water.move((width-w)/2 ,0, 0);
95
        water.scale((float)height/bmpHeight);
96
        }
97
      else
98
        {
99
        int h = (width*bmpHeight)/bmpWidth;
100
        water.move(0 ,(height-h)/2, 0);
101
        water.scale((float)width/bmpWidth);
102
        }
103
      
104
      Distorted.onSurfaceChanged(width, height); 
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
110
      {
111
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
112
      Bitmap bitmap;
113
        
114
      try 
115
        {
116
        bitmap = BitmapFactory.decodeStream(is);
117
        } 
118
      finally 
119
        {
120
        try 
121
          {
122
          is.close();
123
          } 
124
        catch(IOException e) { }
125
        }  
126
      
127
      mRnd = new Random(0);
128
      
129
      bmpHeight = bitmap.getHeight();
130
      bmpWidth  = bitmap.getWidth();
131
      
132
      water = new DistortedBitmap(bitmap, 50);
133
      water.addEventListener(this);
134
      
135
      for(int i=0; i<NUM_BUBBLES; i++) randomizeNewBubble();
136
      
137
      try
138
        {
139
        Distorted.onSurfaceCreated(mView.getContext());
140
        }
141
      catch(Exception ex)
142
        {
143
        android.util.Log.e("Renderer", ex.getMessage() );
144
        }
145
      }
146
}
(2-2/3)