Project

General

Profile

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

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

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.Distorted;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.mesh.MeshFlat;
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
import org.distorted.library.message.EffectMessage;
45

    
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
   private final int NUM_BUBBLES = 12;
55

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
   ListenerRenderer(GLSurfaceView v)
68
      {
69
      // one more than we have bubbles at any given time because it can sometimes happen that
70
      // the old bubble is not yet removed when we add a new one
71
      DistortedEffects.setMax(EffectType.VERTEX,NUM_BUBBLES+1);
72

    
73
      mView = v;
74
      mEffects = new DistortedEffects();
75
      mEffects.registerForMessages(this);
76
      mScreen = new DistortedScreen();
77
      mRnd = new Random(0);
78

    
79
      mMove = new Static3D(0,0,0);
80
      mScale= new Static3D(1,1,1);
81
      mEffects.apply(new MatrixEffectMove(mMove));
82
      mEffects.apply(new MatrixEffectScale(mScale));
83
      }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
   private boolean addNewBubble()
88
      {
89
      int radius   = (int)(( 0.10f + 0.70f*mRnd.nextFloat())*bmpWidth);           // pop up a bubble of size (radius,height)
90
      int height   = (int)((-0.10f + 0.20f*mRnd.nextFloat())*bmpWidth);           // 
91
      int pointx   = mRnd.nextInt( (int)(0.8f*bmpWidth ))+ (int)(0.1f*bmpWidth ); // at a random place on the bitmap (but not near the edge)
92
      int pointy   = mRnd.nextInt( (int)(0.8f*bmpHeight))+ (int)(0.1f*bmpHeight); // 
93
      int duration = 1000 + mRnd.nextInt(3000);                                   // for anytime from 1 to 4 seconds
94

    
95
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
96
      dDistort.add(new Static3D(0,0,     0));
97
      dDistort.add(new Static3D(0,0,height));
98

    
99
      return mEffects.apply( new VertexEffectDistort(dDistort, new Static3D(pointx,pointy,0), new Static4D(0,0,0,radius)) );
100
      }
101
   
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
// the library sending messages to us. This is running on a library 'MessageSender' thread.
104

    
105
   public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
106
     {
107
     switch(em)
108
        {
109
        case EFFECT_REMOVED: if( !addNewBubble() )
110
                               {
111
                               android.util.Log.e("Listener", "failed to add new bubble - this should never happen!");
112
                               }
113
                             break;
114
        }
115
     }
116
   
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
    public void onDrawFrame(GL10 glUnused) 
120
      {
121
      mScreen.render( System.currentTimeMillis() );
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
    
126
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
127
     {
128
     if( (float)bmpHeight/bmpWidth > (float)height/width )
129
       {
130
       int w = (height*bmpWidth)/bmpHeight;
131
       float factor = (float)height/bmpHeight;
132

    
133
       mMove.set((width-w)/2,0,0);
134
       mScale.set(factor,factor,factor);
135
       }
136
     else
137
       {
138
       int h = (width*bmpHeight)/bmpWidth;
139
       float factor = (float)width/bmpWidth;
140

    
141
       mMove.set(0,(height-h)/2,0);
142
       mScale.set(factor,factor,factor);
143
       }
144
      
145
     mScreen.resize(width, height);
146
     }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
    
150
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
151
     {
152
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
153
     Bitmap bitmap;
154
        
155
     try
156
       {
157
       bitmap = BitmapFactory.decodeStream(is);
158
       }
159
     finally
160
       {
161
       try
162
         {
163
         is.close();
164
         }
165
       catch(IOException e) { }
166
       }
167

    
168
     bmpHeight = bitmap.getHeight();
169
     bmpWidth  = bitmap.getWidth();
170

    
171
     if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
172
     mTexture.setTexture(bitmap);
173

    
174
     if( mMesh==null ) mMesh = new MeshFlat(50,50*bmpHeight/bmpWidth);
175

    
176
     mScreen.detachAll();
177
     mScreen.attach(mTexture,mEffects,mMesh);
178

    
179
     for(int i=0; i<NUM_BUBBLES; i++) addNewBubble();
180

    
181
     VertexEffectDistort.enable();
182

    
183
     try
184
       {
185
       Distorted.onCreate(mView.getContext());
186
       }
187
     catch(Exception ex)
188
       {
189
       android.util.Log.e("Listener", ex.getMessage() );
190
       }
191
     }
192
}
(2-2/3)