Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 5055b5d4

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 c5a28eb8 Leszek Koltunski
import org.distorted.library.EffectNames;
34 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
35 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
36 7589635e Leszek Koltunski
import org.distorted.library.type.Static2D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39 13efa930 Leszek Koltunski
import org.distorted.library.message.EffectListener;
40
import org.distorted.library.message.EffectMessage;
41 427ab7bf Leszek Koltunski
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLES20;
45
import android.opengl.GLSurfaceView;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
class ListenerRenderer implements GLSurfaceView.Renderer,EffectListener 
50
{
51 e7a4ef16 Leszek Koltunski
   private final int NUM_BUBBLES = 12;
52
53 427ab7bf Leszek Koltunski
   private GLSurfaceView mView;
54
   private DistortedBitmap water;
55
   private int bmpHeight, bmpWidth;
56
   private Random mRnd;
57 e7a4ef16 Leszek Koltunski
58 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 e7a4ef16 Leszek Koltunski
   ListenerRenderer(GLSurfaceView v)
61 427ab7bf Leszek Koltunski
      {
62
      Distorted.setMaxVertex(NUM_BUBBLES);   
63
      mView = v;
64
      }
65
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68
   private long randomizeNewBubble()
69
      {
70 a8c3ada7 Leszek Koltunski
      int radius   = (int)(( 0.10f + 0.70f*mRnd.nextFloat())*bmpWidth);           // pop up a bubble of size (radius,height)
71 427ab7bf Leszek Koltunski
      int height   = (int)((-0.10f + 0.20f*mRnd.nextFloat())*bmpWidth);           // 
72 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)
73 427ab7bf Leszek Koltunski
      int pointy   = mRnd.nextInt( (int)(0.8f*bmpHeight))+ (int)(0.1f*bmpHeight); // 
74
      int duration = 1000 + mRnd.nextInt(3000);                                   // for anytime from 3 to 4 seconds 
75 7bf107f7 Leszek Koltunski
76 f988589e Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
77 7bf107f7 Leszek Koltunski
      dDistort.add(new Static3D(0,0,     0));
78
      dDistort.add(new Static3D(0,0,height));
79
80 334c13fa Leszek Koltunski
      return water.distort(dDistort, new Static3D(pointx,pointy,0), new Static4D(0,0,radius,radius));
81 427ab7bf Leszek Koltunski
      }
82
   
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84 c5a28eb8 Leszek Koltunski
// the library sending messages to us. This is running on a library 'MessageSender' thread.
85 427ab7bf Leszek Koltunski
86 c5a28eb8 Leszek Koltunski
   public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID, final String message)
87 427ab7bf Leszek Koltunski
     {
88
     switch(em)
89
        {
90
        case EFFECT_REMOVED: randomizeNewBubble();
91
        default            : break;
92
        }
93
     }
94
   
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
97
    public void onDrawFrame(GL10 glUnused) 
98
      {
99
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
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 5055b5d4 Leszek Koltunski
      if( (float)bmpHeight/bmpWidth > (float)height/width )
110 427ab7bf Leszek Koltunski
        {
111
        int w = (height*bmpWidth)/bmpHeight;
112 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
113
114
        water.move( new Static3D((width-w)/2,0,0) );
115 f988589e Leszek Koltunski
        water.scale(factor);
116 427ab7bf Leszek Koltunski
        }
117
      else
118
        {
119
        int h = (width*bmpHeight)/bmpWidth;
120 7589635e Leszek Koltunski
        float factor = (float)width/bmpWidth;
121
122
        water.move( new Static3D(0,(height-h)/2,0) );
123 f988589e Leszek Koltunski
        water.scale(factor);
124 427ab7bf Leszek Koltunski
        }
125
      
126
      Distorted.onSurfaceChanged(width, height); 
127
      }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
    
131
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
132
      {
133 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
134
135 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
136
      Bitmap bitmap;
137
        
138
      try 
139
        {
140
        bitmap = BitmapFactory.decodeStream(is);
141
        } 
142
      finally 
143
        {
144
        try 
145
          {
146
          is.close();
147
          } 
148
        catch(IOException e) { }
149
        }  
150
      
151
      mRnd = new Random(0);
152
      
153
      bmpHeight = bitmap.getHeight();
154
      bmpWidth  = bitmap.getWidth();
155
      
156
      water = new DistortedBitmap(bitmap, 50);
157
      water.addEventListener(this);
158
      
159
      for(int i=0; i<NUM_BUBBLES; i++) randomizeNewBubble();
160
      
161
      try
162
        {
163 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
164 427ab7bf Leszek Koltunski
        }
165
      catch(Exception ex)
166
        {
167
        android.util.Log.e("Renderer", ex.getMessage() );
168
        }
169
      }
170
}