Project

General

Profile

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

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

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