Project

General

Profile

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

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

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