Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ 42d910b3

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.Effect;
32
import org.distorted.library.effect.EffectType;
33
import org.distorted.library.effect.FragmentEffectAlpha;
34
import org.distorted.library.effect.FragmentEffectChroma;
35
import org.distorted.library.effect.MatrixEffectScale;
36
import org.distorted.library.effect.VertexEffectDeform;
37
import org.distorted.library.effect.VertexEffectScale;
38
import org.distorted.library.effect.VertexEffectSink;
39
import org.distorted.library.effect.VertexEffectSwirl;
40
import org.distorted.library.main.DistortedLibrary;
41
import org.distorted.library.main.DistortedScreen;
42
import org.distorted.library.main.DistortedTexture;
43
import org.distorted.library.main.DistortedEffects;
44
import org.distorted.library.mesh.MeshRectangles;
45
import org.distorted.library.type.Static3D;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
class MovingEffectsRenderer implements GLSurfaceView.Renderer
50
   {  
51
   private MovingEffectsSurfaceView mView;
52
   private Canvas mCanvas;
53
   private Bitmap mBitmap;
54
   private Paint mPaint;
55
   private int texW, texH;
56

    
57
   private DistortedEffects mEffects;
58
   private DistortedTexture mTexture;
59
   private DistortedScreen mScreen;
60
   private MeshRectangles mMesh;
61
   private boolean mRefresh;
62
   private Static3D mScaleMatrix, mScaleVertex;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
67
     {    
68
     mPaint = new Paint();
69
     mPaint.setAntiAlias(true);
70
     mPaint.setFakeBoldText(true);
71
     mPaint.setStyle(Style.FILL);
72

    
73
     mView   = v;
74
     mEffects= new DistortedEffects();
75
     mScreen = new DistortedScreen();
76
     mTexture= new DistortedTexture();
77
     mRefresh= true;
78

    
79
     mScaleMatrix = new Static3D(1,1,1);
80
     mScaleVertex = new Static3D(1,1,1);
81
     mEffects.apply( new MatrixEffectScale(mScaleMatrix));
82
     mEffects.apply( new VertexEffectScale(mScaleVertex));
83
     }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
   private void drawBackground()
88
     {
89
     int NW, NH;
90

    
91
     if( texH<texW )
92
       {
93
       NH = 10;
94
       NW = NH*texW/texH;
95
       }
96
     else
97
       {
98
       NW = 10;
99
       NH = NW*texH/texW;
100
       }
101

    
102
     mPaint.setColor(0xff008800);
103
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
104
     mPaint.setColor(0xffffffff);
105
         
106
     for(int i=0; i<=NH ; i++ ) mCanvas.drawRect( 0, texH*i/NH -2,  texW,  texH*i/NH + 2, mPaint);
107
     for(int i=0; i<=NW ; i++ ) mCanvas.drawRect( texW*i/NW - 2, 0, texW*i/NW + 2,  texH, mPaint);
108
     }
109
   
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
   void apply(Effect effect)
113
     {
114
     mEffects.abortByType(EffectType.VERTEX);
115
     mEffects.abortByType(EffectType.FRAGMENT);
116

    
117
     mEffects.apply( new VertexEffectScale(mScaleVertex));
118

    
119
     if( effect!=null ) mEffects.apply(effect);
120
     }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
   void setRefresh()
125
     {
126
     mRefresh = true;
127
     }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
132
     {
133
     VertexEffectScale.enable();
134
     VertexEffectSwirl.enable();
135
     VertexEffectDeform.enable();
136
     VertexEffectSink.enable();
137
     FragmentEffectChroma.enable();
138
     FragmentEffectAlpha.enable();
139

    
140
     try
141
       {
142
       DistortedLibrary.onCreate(mView.getContext());
143
       }
144
     catch(Exception ex)
145
       {
146
       android.util.Log.e("MovingEffects", ex.getMessage() );
147
       }
148
     }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
153
     {
154
     texW = width;
155
     texH = height;
156

    
157
     mScaleMatrix.set(width,width,width);
158
     mScaleVertex.set(1.0f, (float)texH/texW, 1.0f);
159

    
160
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
161
     mCanvas  = new Canvas(mBitmap);
162

    
163
     if( mMesh!=null ) mMesh.markForDeletion();
164
     mMesh = new MeshRectangles(80,80*texH/texW);
165

    
166
     mScreen.detachAll();
167
     mScreen.attach(mTexture,mEffects,mMesh);
168
     mScreen.resize(texW, texH);
169
     mView.onSurfaceChanged(texW,texH);
170

    
171
     mRefresh = true;
172
     }
173
   
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
   
176
   public void onDrawFrame(GL10 glUnused)
177
     {   
178
     long time = System.currentTimeMillis();
179
      
180
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
181
       {
182
       drawBackground();
183
       mView.drawCurve(mCanvas,time);
184
       mTexture.setTexture(mBitmap);
185
       mRefresh = false;
186
       }
187
      
188
     mScreen.render(time);
189
     }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
}
(2-2/3)