Project

General

Profile

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

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

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