Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ 107e4b72

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