Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 676c14da

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 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 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
36
import org.distorted.library.main.DistortedScreen;
37 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshFlat;
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
import org.distorted.library.message.EffectMessage;
45 427ab7bf Leszek Koltunski
46
import android.graphics.Bitmap;
47
import android.graphics.BitmapFactory;
48
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 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
58 d218d64e leszek
   private DistortedScreen mScreen;
59 b0ebdf5e Leszek Koltunski
   private DistortedTexture mTexture;
60 5f2a53b6 leszek
   private MeshFlat mMesh;
61 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
62
   private Random mRnd;
63 20e888ea Leszek Koltunski
   private Static3D mMove, mScale;
64 e7a4ef16 Leszek Koltunski
65 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67 e7a4ef16 Leszek Koltunski
   ListenerRenderer(GLSurfaceView v)
68 427ab7bf Leszek Koltunski
      {
69
      mView = v;
70 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
71 392e16fd Leszek Koltunski
      mEffects.registerForMessages(this);
72 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
73 f6d884d5 Leszek Koltunski
      mRnd = new Random(0);
74 20e888ea Leszek Koltunski
75
      mMove = new Static3D(0,0,0);
76
      mScale= new Static3D(1,1,1);
77
      mEffects.apply(new MatrixEffectMove(mMove));
78
      mEffects.apply(new MatrixEffectScale(mScale));
79 427ab7bf Leszek Koltunski
      }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
83 a4d59c0b Leszek Koltunski
   private boolean addNewBubble()
84 427ab7bf Leszek Koltunski
      {
85 a8c3ada7 Leszek Koltunski
      int radius   = (int)(( 0.10f + 0.70f*mRnd.nextFloat())*bmpWidth);           // pop up a bubble of size (radius,height)
86 427ab7bf Leszek Koltunski
      int height   = (int)((-0.10f + 0.20f*mRnd.nextFloat())*bmpWidth);           // 
87 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)
88 427ab7bf Leszek Koltunski
      int pointy   = mRnd.nextInt( (int)(0.8f*bmpHeight))+ (int)(0.1f*bmpHeight); // 
89 a4d59c0b Leszek Koltunski
      int duration = 1000 + mRnd.nextInt(3000);                                   // for anytime from 1 to 4 seconds
90 7bf107f7 Leszek Koltunski
91 f988589e Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
92 7bf107f7 Leszek Koltunski
      dDistort.add(new Static3D(0,0,     0));
93
      dDistort.add(new Static3D(0,0,height));
94
95 9e7b6dbd Leszek Koltunski
      return mEffects.apply( new VertexEffectDistort(dDistort, new Static3D(pointx,pointy,0), new Static4D(0,0,0,radius)) );
96 427ab7bf Leszek Koltunski
      }
97
   
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99 c5a28eb8 Leszek Koltunski
// the library sending messages to us. This is running on a library 'MessageSender' thread.
100 427ab7bf Leszek Koltunski
101 20e888ea Leszek Koltunski
   public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
102 427ab7bf Leszek Koltunski
     {
103
     switch(em)
104
        {
105 a4d59c0b Leszek Koltunski
        case EFFECT_REMOVED: if( !addNewBubble() )
106
                               {
107
                               android.util.Log.e("Listener", "failed to add new bubble - this should never happen!");
108
                               }
109
                             break;
110 427ab7bf Leszek Koltunski
        }
111
     }
112
   
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115
    public void onDrawFrame(GL10 glUnused) 
116
      {
117 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
118 427ab7bf Leszek Koltunski
      }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122 3a91bfe1 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
123
     {
124
     if( (float)bmpHeight/bmpWidth > (float)height/width )
125
       {
126
       int w = (height*bmpWidth)/bmpHeight;
127
       float factor = (float)height/bmpHeight;
128
129 20e888ea Leszek Koltunski
       mMove.set((width-w)/2,0,0);
130
       mScale.set(factor,factor,factor);
131 3a91bfe1 Leszek Koltunski
       }
132
     else
133
       {
134
       int h = (width*bmpHeight)/bmpWidth;
135
       float factor = (float)width/bmpWidth;
136
137 20e888ea Leszek Koltunski
       mMove.set(0,(height-h)/2,0);
138
       mScale.set(factor,factor,factor);
139 3a91bfe1 Leszek Koltunski
       }
140 427ab7bf Leszek Koltunski
      
141 3a91bfe1 Leszek Koltunski
     mScreen.resize(width, height);
142
     }
143 427ab7bf Leszek Koltunski
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145
    
146 3a91bfe1 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
147
     {
148
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
149
     Bitmap bitmap;
150 427ab7bf Leszek Koltunski
        
151 3a91bfe1 Leszek Koltunski
     try
152
       {
153
       bitmap = BitmapFactory.decodeStream(is);
154
       }
155
     finally
156
       {
157
       try
158
         {
159
         is.close();
160
         }
161
       catch(IOException e) { }
162
       }
163
164
     bmpHeight = bitmap.getHeight();
165
     bmpWidth  = bitmap.getWidth();
166
167
     if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
168
     mTexture.setTexture(bitmap);
169
170
     if( mMesh==null ) mMesh = new MeshFlat(50,50*bmpHeight/bmpWidth);
171
172
     mScreen.detachAll();
173
     mScreen.attach(mTexture,mEffects,mMesh);
174
175 a4d59c0b Leszek Koltunski
     for(int i=0; i<NUM_BUBBLES; i++) addNewBubble();
176 3a91bfe1 Leszek Koltunski
177 676c14da Leszek Koltunski
     // one more than we have bubbles at any given time because it can sometimes
178
     // happen that the old bubble is not yet removed when we add a new one
179
     DistortedEffects.setMax(EffectType.VERTEX,NUM_BUBBLES+1);
180 885b9cca leszek
     VertexEffectDistort.enable();
181 3a91bfe1 Leszek Koltunski
182
     try
183
       {
184
       Distorted.onCreate(mView.getContext());
185
       }
186
     catch(Exception ex)
187
       {
188 9e7b6dbd Leszek Koltunski
       android.util.Log.e("Listener", ex.getMessage() );
189 3a91bfe1 Leszek Koltunski
       }
190
     }
191 427ab7bf Leszek Koltunski
}