Project

General

Profile

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

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

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 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
32 d218d64e leszek
import org.distorted.library.DistortedScreen;
33 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
34 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
35 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
36 c5a28eb8 Leszek Koltunski
import org.distorted.library.EffectNames;
37 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
38 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
39 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41 13efa930 Leszek Koltunski
import org.distorted.library.message.EffectListener;
42
import org.distorted.library.message.EffectMessage;
43 427ab7bf Leszek Koltunski
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
47 427ab7bf Leszek Koltunski
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 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
59
   private Random mRnd;
60 e7a4ef16 Leszek Koltunski
61 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 e7a4ef16 Leszek Koltunski
   ListenerRenderer(GLSurfaceView v)
64 427ab7bf Leszek Koltunski
      {
65 76f9798b Leszek Koltunski
      DistortedEffects.setMaxVertex(NUM_BUBBLES);
66 427ab7bf Leszek Koltunski
      mView = v;
67 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
68 392e16fd Leszek Koltunski
      mEffects.registerForMessages(this);
69 d218d64e leszek
      mScreen = new DistortedScreen();
70 f6d884d5 Leszek Koltunski
      mRnd = new Random(0);
71 427ab7bf Leszek Koltunski
      }
72
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75
   private long randomizeNewBubble()
76
      {
77 a8c3ada7 Leszek Koltunski
      int radius   = (int)(( 0.10f + 0.70f*mRnd.nextFloat())*bmpWidth);           // pop up a bubble of size (radius,height)
78 427ab7bf Leszek Koltunski
      int height   = (int)((-0.10f + 0.20f*mRnd.nextFloat())*bmpWidth);           // 
79 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)
80 427ab7bf Leszek Koltunski
      int pointy   = mRnd.nextInt( (int)(0.8f*bmpHeight))+ (int)(0.1f*bmpHeight); // 
81
      int duration = 1000 + mRnd.nextInt(3000);                                   // for anytime from 3 to 4 seconds 
82 7bf107f7 Leszek Koltunski
83 f988589e Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(duration,1.0f);
84 7bf107f7 Leszek Koltunski
      dDistort.add(new Static3D(0,0,     0));
85
      dDistort.add(new Static3D(0,0,height));
86
87 392e16fd Leszek Koltunski
      return mEffects.distort(dDistort, new Static3D(pointx,pointy,0), new Static4D(0,0,radius,radius));
88 427ab7bf Leszek Koltunski
      }
89
   
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91 c5a28eb8 Leszek Koltunski
// the library sending messages to us. This is running on a library 'MessageSender' thread.
92 427ab7bf Leszek Koltunski
93 9d12dd4a Leszek Koltunski
   public void effectMessage(final EffectMessage em, final long effectID, final EffectNames effectName, final long objectID)
94 427ab7bf Leszek Koltunski
     {
95
     switch(em)
96
        {
97
        case EFFECT_REMOVED: randomizeNewBubble();
98
        default            : break;
99
        }
100
     }
101
   
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104
    public void onDrawFrame(GL10 glUnused) 
105
      {
106 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
107 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
108 427ab7bf Leszek Koltunski
      }
109
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
113
      { 
114 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
115 427ab7bf Leszek Koltunski
         
116 5055b5d4 Leszek Koltunski
      if( (float)bmpHeight/bmpWidth > (float)height/width )
117 427ab7bf Leszek Koltunski
        {
118
        int w = (height*bmpWidth)/bmpHeight;
119 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
120
121 392e16fd Leszek Koltunski
        mEffects.move( new Static3D((width-w)/2,0,0) );
122
        mEffects.scale(factor);
123 427ab7bf Leszek Koltunski
        }
124
      else
125
        {
126
        int h = (width*bmpHeight)/bmpWidth;
127 7589635e Leszek Koltunski
        float factor = (float)width/bmpWidth;
128
129 392e16fd Leszek Koltunski
        mEffects.move( new Static3D(0,(height-h)/2,0) );
130
        mEffects.scale(factor);
131 427ab7bf Leszek Koltunski
        }
132
      
133 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
134 427ab7bf Leszek Koltunski
      }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
    
138
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
139
      {
140 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
141 e7a4ef16 Leszek Koltunski
142 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.water);
143
      Bitmap bitmap;
144
        
145
      try 
146
        {
147
        bitmap = BitmapFactory.decodeStream(is);
148
        } 
149
      finally 
150
        {
151
        try 
152
          {
153
          is.close();
154
          } 
155
        catch(IOException e) { }
156
        }  
157 f6d884d5 Leszek Koltunski
158 427ab7bf Leszek Koltunski
      bmpHeight = bitmap.getHeight();
159
      bmpWidth  = bitmap.getWidth();
160 e8b6aa95 Leszek Koltunski
161 fe59d375 Leszek Koltunski
      MeshFlat mesh = new MeshFlat(50,50*bmpHeight/bmpWidth);
162
      DistortedTexture texture = new DistortedTexture(bmpWidth,bmpHeight);
163
      texture.setTexture(bitmap);
164
165
      mScreen.detachAll();
166
      mScreen.attach(texture,mEffects,mesh);
167 f6d884d5 Leszek Koltunski
168 427ab7bf Leszek Koltunski
      for(int i=0; i<NUM_BUBBLES; i++) randomizeNewBubble();
169
      
170
      try
171
        {
172 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
173 427ab7bf Leszek Koltunski
        }
174
      catch(Exception ex)
175
        {
176
        android.util.Log.e("Renderer", ex.getMessage() );
177
        }
178
      }
179
}