Project

General

Profile

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

examples / src / main / java / org / distorted / examples / listener / ListenerRenderer.java @ 10b7e588

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.GridFlat;
33
import org.distorted.library.DistortedObject;
34
import org.distorted.library.EffectNames;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39
import org.distorted.library.message.EffectListener;
40
import org.distorted.library.message.EffectMessage;
41

    
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLES20;
45
import android.opengl.GLSurfaceView;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

    
53
   private GLSurfaceView mView;
54
   private DistortedObject mWater;
55
   private GridFlat mGrid;
56
   private int bmpHeight, bmpWidth;
57
   private Random mRnd;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
   ListenerRenderer(GLSurfaceView v)
62
      {
63
      Distorted.setMaxVertex(NUM_BUBBLES);   
64
      mView = v;
65
      }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

    
77
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
78
      dDistort.add(new Static3D(0,0,     0));
79
      dDistort.add(new Static3D(0,0,height));
80

    
81
      return mWater.distort(dDistort, new Static3D(pointx,pointy,0), new Static4D(0,0,radius,radius));
82
      }
83
   
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
// the library sending messages to us. This is running on a library 'MessageSender' thread.
86

    
87
   public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID, final String message)
88
     {
89
     switch(em)
90
        {
91
        case EFFECT_REMOVED: randomizeNewBubble();
92
        default            : break;
93
        }
94
     }
95
   
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
    public void onDrawFrame(GL10 glUnused) 
99
      {
100
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
101
      mWater.draw(System.currentTimeMillis(), mGrid);
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
    
106
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
107
      { 
108
      mWater.abortEffects(EffectTypes.MATRIX);
109
         
110
      if( (float)bmpHeight/bmpWidth > (float)height/width )
111
        {
112
        int w = (height*bmpWidth)/bmpHeight;
113
        float factor = (float)height/bmpHeight;
114

    
115
        mWater.move( new Static3D((width-w)/2,0,0) );
116
        mWater.scale(factor);
117
        }
118
      else
119
        {
120
        int h = (width*bmpHeight)/bmpWidth;
121
        float factor = (float)width/bmpWidth;
122

    
123
        mWater.move( new Static3D(0,(height-h)/2,0) );
124
        mWater.scale(factor);
125
        }
126
      
127
      Distorted.onSurfaceChanged(width, height); 
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
    
132
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
133
      {
134
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
135

    
136
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
137
      Bitmap bitmap;
138
        
139
      try 
140
        {
141
        bitmap = BitmapFactory.decodeStream(is);
142
        } 
143
      finally 
144
        {
145
        try 
146
          {
147
          is.close();
148
          } 
149
        catch(IOException e) { }
150
        }  
151
      
152
      mRnd = new Random(0);
153
      
154
      bmpHeight = bitmap.getHeight();
155
      bmpWidth  = bitmap.getWidth();
156

    
157
      mGrid  = new GridFlat(50,50*bmpHeight/bmpWidth);
158
      mWater = new DistortedObject(bmpWidth,bmpHeight,1);
159
      mWater.setTexture(bitmap);
160
      mWater.addEventListener(this);
161
      
162
      for(int i=0; i<NUM_BUBBLES; i++) randomizeNewBubble();
163
      
164
      try
165
        {
166
        Distorted.onSurfaceCreated(mView.getContext());
167
        }
168
      catch(Exception ex)
169
        {
170
        android.util.Log.e("Renderer", ex.getMessage() );
171
        }
172
      }
173
}
(2-2/3)