Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 5068fa06

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.Float2D;
16
import org.distorted.library.Float3D;
17
import org.distorted.library.Float4D;
18
import org.distorted.library.EffectListener;
19
import org.distorted.library.EffectMessage;
20

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

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

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

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

    
64
   public void effectMessage(EffectMessage em, long effectID, int effectType, long bitmapID)
65
     {
66
     switch(em)
67
        {
68
        case EFFECT_REMOVED: randomizeNewBubble();
69
        default            : break;
70
        }
71
     }
72
   
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

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

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

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