Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 698ad0a8

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
20
package org.distorted.examples.listener;
21

    
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
import org.distorted.examples.R;
30

    
31
import org.distorted.library.effect.EffectType;
32
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectDistort;
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.mesh.MeshRectangles;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.main.DistortedEffects;
40
import org.distorted.library.type.Dynamic3D;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43
import org.distorted.library.message.EffectListener;
44

    
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLSurfaceView;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class ListenerRenderer implements GLSurfaceView.Renderer,EffectListener 
52
{
53
   private final int NUM_BUBBLES = 12;
54

    
55
   private GLSurfaceView mView;
56
   private DistortedEffects mEffects;
57
   private DistortedScreen mScreen;
58
   private DistortedTexture mTexture;
59
   private MeshRectangles mMesh;
60
   private int bmpHeight, bmpWidth;
61
   private Random mRnd;
62
   private Static3D mMove, mScale;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
   ListenerRenderer(GLSurfaceView v)
67
      {
68
      mView = v;
69

    
70
      mScreen = new DistortedScreen();
71
      mRnd = new Random(0);
72

    
73
      mMove = new Static3D(0,0,0);
74
      mScale= new Static3D(1,1,1);
75
      }
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
   private boolean addNewBubble()
80
      {
81
      int radius   = (int)(( 0.10f + 0.70f*mRnd.nextFloat())*bmpWidth);           // pop up a bubble of size (radius,height)
82
      int height   = (int)((-0.10f + 0.20f*mRnd.nextFloat())*bmpWidth);           // 
83
      int pointx   = mRnd.nextInt( (int)(0.8f*bmpWidth ))+ (int)(0.1f*bmpWidth ); // at a random place on the bitmap (but not near the edge)
84
      int pointy   = mRnd.nextInt( (int)(0.8f*bmpHeight))+ (int)(0.1f*bmpHeight); // 
85
      int duration = 1000 + mRnd.nextInt(3000);                                   // for anytime from 1 to 4 seconds
86

    
87
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
88
      dDistort.add(new Static3D(0,0,     0));
89
      dDistort.add(new Static3D(0,0,height));
90

    
91
      VertexEffectDistort distort = new VertexEffectDistort(dDistort, new Static3D(pointx,pointy,0), new Static4D(0,0,0,radius));
92
      distort.notifyWhenFinished(this);
93

    
94
      return mEffects.apply(distort);
95
      }
96
   
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
   public void effectFinished(final long effectID)
100
     {
101
     mEffects.abortById(effectID);
102

    
103
     if( !addNewBubble() )
104
       {
105
       android.util.Log.e("Listener", "failed to add new bubble - this should never happen!");
106
       }
107
     }
108
   
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
    public void onDrawFrame(GL10 glUnused) 
112
      {
113
      mScreen.render( System.currentTimeMillis() );
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
    
118
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
119
     {
120
     if( (float)bmpHeight/bmpWidth > (float)height/width )
121
       {
122
       int w = (height*bmpWidth)/bmpHeight;
123
       float factor = (float)height/bmpHeight;
124

    
125
       mMove.set((width-w)*0.5f,0,0);
126
       mScale.set(factor,factor,factor);
127
       }
128
     else
129
       {
130
       int h = (width*bmpHeight)/bmpWidth;
131
       float factor = (float)width/bmpWidth;
132

    
133
       mMove.set(0,(height-h)*0.5f,0);
134
       mScale.set(factor,factor,factor);
135
       }
136
      
137
     mScreen.resize(width, height);
138
     }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
    
142
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
143
     {
144
     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

    
160
     bmpHeight = bitmap.getHeight();
161
     bmpWidth  = bitmap.getWidth();
162

    
163
     if( mTexture==null ) mTexture = new DistortedTexture();
164
     mTexture.setTexture(bitmap);
165

    
166
     if( mMesh==null )
167
       {
168
       mMesh = new MeshRectangles(50,50*bmpHeight/bmpWidth);
169
       mMesh.setStretch(bmpWidth,bmpHeight,0);
170
       }
171

    
172
     if( mEffects==null )
173
       {
174
       mEffects = new DistortedEffects();
175
       mEffects.apply(new MatrixEffectScale(mScale));
176
       mEffects.apply(new MatrixEffectMove(mMove));
177
       }
178

    
179
     mScreen.detachAll();
180
     mScreen.attach(mTexture,mEffects,mMesh);
181

    
182
     for(int i=0; i<NUM_BUBBLES; i++) addNewBubble();
183

    
184
     // one more than we have bubbles at any given time because it can sometimes
185
     // happen that the old bubble is not yet removed when we add a new one
186
     DistortedLibrary.setMax(EffectType.VERTEX,NUM_BUBBLES+1);
187
     VertexEffectDistort.enable();
188

    
189
     try
190
       {
191
       DistortedLibrary.onCreate(mView.getContext());
192
       }
193
     catch(Exception ex)
194
       {
195
       android.util.Log.e("Listener", ex.getMessage() );
196
       }
197
     }
198
}
(2-2/3)