Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ 885b9cca

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.movingeffects;
21

    
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
import org.distorted.library.effect.EffectName;
32
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
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

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
class MovingEffectsRenderer implements GLSurfaceView.Renderer
46
   {  
47
   private MovingEffectsSurfaceView mView;
48
   private Canvas mCanvas;
49
   private Bitmap mBitmap;
50
   private Paint mPaint;
51
   private int texW, texH;
52

    
53
   private DistortedEffects mEffects;
54
   private DistortedTexture mTexture;
55
   private DistortedScreen mScreen;
56
   private MeshFlat mMesh;
57
   private boolean mRefresh;
58

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

    
61
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
62
     {    
63
     mPaint = new Paint();
64
     mPaint.setAntiAlias(true);
65
     mPaint.setFakeBoldText(true);
66
     mPaint.setStyle(Style.FILL);
67

    
68
     mView   = v;
69
     mEffects= new DistortedEffects();
70
     mScreen = new DistortedScreen();
71
     mRefresh= true;
72
     }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
   private void drawBackground()
77
     {
78
     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
     mPaint.setColor(0xff008800);
92
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
93
     mPaint.setColor(0xffffffff);
94
         
95
     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
     }
98
   
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
   DistortedEffects getEffects()
102
     {
103
     return mEffects;
104
     }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
   void setRefresh()
109
     {
110
     mRefresh = true;
111
     }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

    
115
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
116
     {
117
     VertexEffectSwirl.enable();
118
     VertexEffectDistort.enable();
119
     VertexEffectSink.enable();
120
     FragmentEffectChroma.enable();
121
     FragmentEffectAlpha.enable();
122

    
123
     try
124
       {
125
       Distorted.onCreate(mView.getContext());
126
       }
127
     catch(Exception ex)
128
       {
129
       android.util.Log.e("Renderer", ex.getMessage() );
130
       }
131
     }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
136
     {
137
     texW = width;
138
     texH = height;
139

    
140
     if( mTexture!=null ) mTexture.markForDeletion();
141
     mTexture = new DistortedTexture(texW,texH);
142
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
143
     mCanvas  = new Canvas(mBitmap);
144

    
145
     if( mMesh!=null ) mMesh.markForDeletion();
146
     mMesh = new MeshFlat(80,80*texH/texW);
147

    
148
     mScreen.detachAll();
149
     mScreen.attach(mTexture,mEffects,mMesh);
150
     mScreen.resize(texW, texH);
151
     mView.onSurfaceChanged(texW,texH);
152

    
153
     mRefresh = true;
154
     }
155
   
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
   
158
   public void onDrawFrame(GL10 glUnused)
159
     {   
160
     long time = System.currentTimeMillis();
161
      
162
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
163
       {
164
       drawBackground();
165
       mView.drawCurve(mCanvas,time);
166
       mTexture.setTexture(mBitmap);
167
       mRefresh = false;
168
       }
169
      
170
     mScreen.render(time);
171
     }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
}
(2-2/3)