Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ 29b5cd0d

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
20 5068fa06 Leszek Koltunski
package org.distorted.examples.movingeffects;
21 427ab7bf Leszek Koltunski
22
import javax.microedition.khronos.egl.EGLConfig;
23
import javax.microedition.khronos.opengles.GL10;
24
25
import android.graphics.Bitmap;
26
import android.graphics.Canvas;
27
import android.graphics.Paint;
28
import android.graphics.Paint.Style;
29
import android.opengl.GLSurfaceView;
30
31 6173632c Leszek Koltunski
import org.distorted.library.effect.EffectName;
32 885b9cca leszek
import org.distorted.library.effect.FragmentEffectAlpha;
33
import org.distorted.library.effect.FragmentEffectChroma;
34
import org.distorted.library.effect.VertexEffectDistort;
35
import org.distorted.library.effect.VertexEffectSink;
36
import org.distorted.library.effect.VertexEffectSwirl;
37 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.main.DistortedEffects;
40
import org.distorted.library.main.MeshFlat;
41
import org.distorted.library.main.Distorted;
42 427ab7bf Leszek Koltunski
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 97dadfe5 Leszek Koltunski
class MovingEffectsRenderer implements GLSurfaceView.Renderer
46 427ab7bf Leszek Koltunski
   {  
47 4562f295 Leszek Koltunski
   private MovingEffectsSurfaceView mView;
48 427ab7bf Leszek Koltunski
   private Canvas mCanvas;
49
   private Bitmap mBitmap;
50
   private Paint mPaint;
51 97dadfe5 Leszek Koltunski
   private int texW, texH;
52
53 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
54 f6d884d5 Leszek Koltunski
   private DistortedTexture mTexture;
55 d218d64e leszek
   private DistortedScreen mScreen;
56 5f2a53b6 leszek
   private MeshFlat mMesh;
57 3c8b1903 Leszek Koltunski
   private boolean mRefresh;
58 97dadfe5 Leszek Koltunski
59 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 4562f295 Leszek Koltunski
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
62 427ab7bf Leszek Koltunski
     {    
63
     mPaint = new Paint();
64
     mPaint.setAntiAlias(true);
65
     mPaint.setFakeBoldText(true);
66
     mPaint.setStyle(Style.FILL);
67 f6d884d5 Leszek Koltunski
68 392e16fd Leszek Koltunski
     mView   = v;
69 d04a4886 Leszek Koltunski
     mEffects= new DistortedEffects();
70 e4330c89 Leszek Koltunski
     mScreen = new DistortedScreen();
71 392e16fd Leszek Koltunski
     mRefresh= true;
72 427ab7bf Leszek Koltunski
     }
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76
   private void drawBackground()
77
     {
78 97dadfe5 Leszek Koltunski
     int NW, NH;
79
80
     if( texH<texW )
81
       {
82
       NH = 10;
83
       NW = NH*texW/texH;
84
       }
85
     else
86
       {
87
       NW = 10;
88
       NH = NW*texH/texW;
89
       }
90
91 427ab7bf Leszek Koltunski
     mPaint.setColor(0xff008800);
92 97dadfe5 Leszek Koltunski
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
93 427ab7bf Leszek Koltunski
     mPaint.setColor(0xffffffff);
94
         
95 97dadfe5 Leszek Koltunski
     for(int i=0; i<=NH ; i++ ) mCanvas.drawRect( 0, texH*i/NH -2,  texW,  texH*i/NH + 2, mPaint);
96
     for(int i=0; i<=NW ; i++ ) mCanvas.drawRect( texW*i/NW - 2, 0, texW*i/NW + 2,  texH, mPaint);
97 427ab7bf Leszek Koltunski
     }
98
   
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100 f6d884d5 Leszek Koltunski
101 d04a4886 Leszek Koltunski
   DistortedEffects getEffects()
102 f6d884d5 Leszek Koltunski
     {
103 392e16fd Leszek Koltunski
     return mEffects;
104 f6d884d5 Leszek Koltunski
     }
105
106 3c8b1903 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108
   void setRefresh()
109
     {
110
     mRefresh = true;
111
     }
112
113 f6d884d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
114
115 427ab7bf Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
116 97dadfe5 Leszek Koltunski
     {
117 885b9cca leszek
     VertexEffectSwirl.enable();
118
     VertexEffectDistort.enable();
119
     VertexEffectSink.enable();
120
     FragmentEffectChroma.enable();
121
     FragmentEffectAlpha.enable();
122 6637d0f2 Leszek Koltunski
123 427ab7bf Leszek Koltunski
     try
124 97dadfe5 Leszek Koltunski
       {
125 76f9798b Leszek Koltunski
       Distorted.onCreate(mView.getContext());
126 97dadfe5 Leszek Koltunski
       }
127
     catch(Exception ex)
128
       {
129
       android.util.Log.e("Renderer", ex.getMessage() );
130
       }
131 427ab7bf Leszek Koltunski
     }
132
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
135
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
136
     {
137 97dadfe5 Leszek Koltunski
     texW = width;
138
     texH = height;
139
140 b0ebdf5e Leszek Koltunski
     if( mTexture!=null ) mTexture.markForDeletion();
141 7451c98a Leszek Koltunski
     mTexture = new DistortedTexture(texW,texH);
142 f6d884d5 Leszek Koltunski
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
143
     mCanvas  = new Canvas(mBitmap);
144 97dadfe5 Leszek Koltunski
145 5f2a53b6 leszek
     if( mMesh!=null ) mMesh.markForDeletion();
146
     mMesh = new MeshFlat(80,80*texH/texW);
147
148 fe59d375 Leszek Koltunski
     mScreen.detachAll();
149 5f2a53b6 leszek
     mScreen.attach(mTexture,mEffects,mMesh);
150 392e16fd Leszek Koltunski
     mScreen.resize(texW, texH);
151 4562f295 Leszek Koltunski
     mView.onSurfaceChanged(texW,texH);
152 fd89ecb6 Leszek Koltunski
153
     mRefresh = true;
154 427ab7bf Leszek Koltunski
     }
155
   
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
   
158
   public void onDrawFrame(GL10 glUnused)
159
     {   
160
     long time = System.currentTimeMillis();
161
      
162 3c8b1903 Leszek Koltunski
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
163 427ab7bf Leszek Koltunski
       {
164 fd89ecb6 Leszek Koltunski
       drawBackground();
165 4562f295 Leszek Koltunski
       mView.drawCurve(mCanvas,time);
166 f6d884d5 Leszek Koltunski
       mTexture.setTexture(mBitmap);
167 3c8b1903 Leszek Koltunski
       mRefresh = false;
168 427ab7bf Leszek Koltunski
       }
169
      
170 fe59d375 Leszek Koltunski
     mScreen.render(time);
171 427ab7bf Leszek Koltunski
     }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
}