Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 d76638f1 leszek
import org.distorted.library.effect.EffectType;
32 20e888ea Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectDistort;
35 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
36 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
37 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
38 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.main.DistortedEffects;
40 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
41 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43 13efa930 Leszek Koltunski
import org.distorted.library.message.EffectListener;
44 427ab7bf Leszek Koltunski
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 e7a4ef16 Leszek Koltunski
   private final int NUM_BUBBLES = 12;
54
55 427ab7bf Leszek Koltunski
   private GLSurfaceView mView;
56 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
57 d218d64e leszek
   private DistortedScreen mScreen;
58 b0ebdf5e Leszek Koltunski
   private DistortedTexture mTexture;
59 f3ca895f Leszek Koltunski
   private MeshRectangles mMesh;
60 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
61
   private Random mRnd;
62 20e888ea Leszek Koltunski
   private Static3D mMove, mScale;
63 e7a4ef16 Leszek Koltunski
64 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 e7a4ef16 Leszek Koltunski
   ListenerRenderer(GLSurfaceView v)
67 427ab7bf Leszek Koltunski
      {
68
      mView = v;
69 687263cc Leszek Koltunski
70 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
71 f6d884d5 Leszek Koltunski
      mRnd = new Random(0);
72 20e888ea Leszek Koltunski
73
      mMove = new Static3D(0,0,0);
74
      mScale= new Static3D(1,1,1);
75 427ab7bf Leszek Koltunski
      }
76
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79 a4d59c0b Leszek Koltunski
   private boolean addNewBubble()
80 427ab7bf Leszek Koltunski
      {
81 a8c3ada7 Leszek Koltunski
      int radius   = (int)(( 0.10f + 0.70f*mRnd.nextFloat())*bmpWidth);           // pop up a bubble of size (radius,height)
82 427ab7bf Leszek Koltunski
      int height   = (int)((-0.10f + 0.20f*mRnd.nextFloat())*bmpWidth);           // 
83 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)
84 427ab7bf Leszek Koltunski
      int pointy   = mRnd.nextInt( (int)(0.8f*bmpHeight))+ (int)(0.1f*bmpHeight); // 
85 a4d59c0b Leszek Koltunski
      int duration = 1000 + mRnd.nextInt(3000);                                   // for anytime from 1 to 4 seconds
86 7bf107f7 Leszek Koltunski
87 f988589e Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
88 7bf107f7 Leszek Koltunski
      dDistort.add(new Static3D(0,0,     0));
89
      dDistort.add(new Static3D(0,0,height));
90
91 8d5a8e06 Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      }
96
   
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99 8d5a8e06 Leszek Koltunski
   public void effectFinished(final long effectID)
100 427ab7bf Leszek Koltunski
     {
101 8d5a8e06 Leszek Koltunski
     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 427ab7bf Leszek Koltunski
     }
108
   
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111
    public void onDrawFrame(GL10 glUnused) 
112
      {
113 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
114 427ab7bf Leszek Koltunski
      }
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
    
118 3a91bfe1 Leszek Koltunski
   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 8d5a8e06 Leszek Koltunski
       mMove.set((width-w)*0.5f,0,0);
126 20e888ea Leszek Koltunski
       mScale.set(factor,factor,factor);
127 3a91bfe1 Leszek Koltunski
       }
128
     else
129
       {
130
       int h = (width*bmpHeight)/bmpWidth;
131
       float factor = (float)width/bmpWidth;
132
133 8d5a8e06 Leszek Koltunski
       mMove.set(0,(height-h)*0.5f,0);
134 20e888ea Leszek Koltunski
       mScale.set(factor,factor,factor);
135 3a91bfe1 Leszek Koltunski
       }
136 427ab7bf Leszek Koltunski
      
137 3a91bfe1 Leszek Koltunski
     mScreen.resize(width, height);
138
     }
139 427ab7bf Leszek Koltunski
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
    
142 3a91bfe1 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
143
     {
144
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
145
     Bitmap bitmap;
146 427ab7bf Leszek Koltunski
        
147 3a91bfe1 Leszek Koltunski
     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 687263cc Leszek Koltunski
     if( mTexture==null ) mTexture = new DistortedTexture();
164 3a91bfe1 Leszek Koltunski
     mTexture.setTexture(bitmap);
165
166 698ad0a8 Leszek Koltunski
     if( mMesh==null )
167
       {
168
       mMesh = new MeshRectangles(50,50*bmpHeight/bmpWidth);
169
       mMesh.setStretch(bmpWidth,bmpHeight,0);
170
       }
171 3a91bfe1 Leszek Koltunski
172 687263cc Leszek Koltunski
     if( mEffects==null )
173
       {
174 698ad0a8 Leszek Koltunski
       mEffects = new DistortedEffects();
175 687263cc Leszek Koltunski
       mEffects.apply(new MatrixEffectScale(mScale));
176
       mEffects.apply(new MatrixEffectMove(mMove));
177
       }
178
179 3a91bfe1 Leszek Koltunski
     mScreen.detachAll();
180
     mScreen.attach(mTexture,mEffects,mMesh);
181
182 a4d59c0b Leszek Koltunski
     for(int i=0; i<NUM_BUBBLES; i++) addNewBubble();
183 3a91bfe1 Leszek Koltunski
184 676c14da Leszek Koltunski
     // 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 e3900503 Leszek Koltunski
     DistortedLibrary.setMax(EffectType.VERTEX,NUM_BUBBLES+1);
187 885b9cca leszek
     VertexEffectDistort.enable();
188 3a91bfe1 Leszek Koltunski
189
     try
190
       {
191 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
192 3a91bfe1 Leszek Koltunski
       }
193
     catch(Exception ex)
194
       {
195 9e7b6dbd Leszek Koltunski
       android.util.Log.e("Listener", ex.getMessage() );
196 3a91bfe1 Leszek Koltunski
       }
197
     }
198 427ab7bf Leszek Koltunski
}