Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 392e16fd

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