Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 1f9a52f1

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.Distorted;
32
import org.distorted.library.DistortedScreen;
33
import org.distorted.library.MeshFlat;
34
import org.distorted.library.DistortedTexture;
35
import org.distorted.library.DistortedEffects;
36
import org.distorted.library.EffectNames;
37
import org.distorted.library.EffectTypes;
38
import org.distorted.library.type.Dynamic3D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41
import org.distorted.library.message.EffectListener;
42
import org.distorted.library.message.EffectMessage;
43

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

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

    
54
   private GLSurfaceView mView;
55
   private DistortedEffects mEffects;
56
   private DistortedScreen mScreen;
57
   private DistortedTexture mTexture;
58
   private MeshFlat mMesh;
59
   private int bmpHeight, bmpWidth;
60
   private Random mRnd;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
   ListenerRenderer(GLSurfaceView v)
65
      {
66
      DistortedEffects.setMaxVertex(NUM_BUBBLES);
67
      mView = v;
68
      mEffects = new DistortedEffects();
69
      mEffects.registerForMessages(this);
70
      mScreen = new DistortedScreen(mView);
71
      mRnd = new Random(0);
72
      }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

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

    
84
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
85
      dDistort.add(new Static3D(0,0,     0));
86
      dDistort.add(new Static3D(0,0,height));
87

    
88
      return mEffects.distort(dDistort, new Static3D(pointx,pointy,0), new Static4D(0,0,radius,radius));
89
      }
90
   
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
// the library sending messages to us. This is running on a library 'MessageSender' thread.
93

    
94
   public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID)
95
     {
96
     switch(em)
97
        {
98
        case EFFECT_REMOVED: randomizeNewBubble();
99
        default            : break;
100
        }
101
     }
102
   
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
    public void onDrawFrame(GL10 glUnused) 
106
      {
107
      mScreen.render( System.currentTimeMillis() );
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
113
      { 
114
      mEffects.abortEffects(EffectTypes.MATRIX);
115
         
116
      if( (float)bmpHeight/bmpWidth > (float)height/width )
117
        {
118
        int w = (height*bmpWidth)/bmpHeight;
119
        float factor = (float)height/bmpHeight;
120

    
121
        mEffects.move( new Static3D((width-w)/2,0,0) );
122
        mEffects.scale(factor);
123
        }
124
      else
125
        {
126
        int h = (width*bmpHeight)/bmpWidth;
127
        float factor = (float)width/bmpWidth;
128

    
129
        mEffects.move( new Static3D(0,(height-h)/2,0) );
130
        mEffects.scale(factor);
131
        }
132
      
133
      mScreen.resize(width, height);
134
      }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
139
      {
140
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
141
      Bitmap bitmap;
142
        
143
      try 
144
        {
145
        bitmap = BitmapFactory.decodeStream(is);
146
        } 
147
      finally 
148
        {
149
        try 
150
          {
151
          is.close();
152
          } 
153
        catch(IOException e) { }
154
        }  
155

    
156
      bmpHeight = bitmap.getHeight();
157
      bmpWidth  = bitmap.getWidth();
158

    
159
      if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
160
      mTexture.setTexture(bitmap);
161

    
162
      if( mMesh==null ) mMesh = new MeshFlat(50,50*bmpHeight/bmpWidth);
163

    
164
      mScreen.detachAll();
165
      mScreen.attach(mTexture,mEffects,mMesh);
166

    
167
      for(int i=0; i<NUM_BUBBLES; i++) randomizeNewBubble();
168

    
169
      DistortedEffects.enableEffect(EffectNames.DISTORT);
170

    
171
      try
172
        {
173
        Distorted.onCreate(mView.getContext());
174
        }
175
      catch(Exception ex)
176
        {
177
        android.util.Log.e("Renderer", ex.getMessage() );
178
        }
179
      }
180
}
(2-2/3)