Project

General

Profile

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

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

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

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

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

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

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