Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ 061449ed

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, DistortedLibrary.ExceptionListener
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 onDrawFrame(GL10 glUnused)
132
     {
133
     long time = System.currentTimeMillis();
134

    
135
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
136
       {
137
       drawBackground();
138
       mView.drawCurve(mCanvas,time);
139
       mTexture.setTexture(mBitmap);
140
       mRefresh = false;
141
       }
142

    
143
     mScreen.render(time);
144
     }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
149
     {
150
     texW = width;
151
     texH = height;
152

    
153
     mScaleMatrix.set(width,width,width);
154
     mScaleVertex.set(1.0f, (float)texH/texW, 1.0f);
155

    
156
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
157
     mCanvas  = new Canvas(mBitmap);
158

    
159
     if( mMesh!=null ) mMesh.markForDeletion();
160
     mMesh = new MeshRectangles(80,80*texH/texW);
161

    
162
     mScreen.detachAll();
163
     mScreen.attach(mTexture,mEffects,mMesh);
164
     mScreen.resize(texW, texH);
165
     mView.onSurfaceChanged(texW,texH);
166

    
167
     mRefresh = true;
168
     }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
173
     {
174
     VertexEffectScale.enable();
175
     VertexEffectSwirl.enable();
176
     VertexEffectDeform.enable();
177
     VertexEffectSink.enable();
178
     FragmentEffectChroma.enable();
179
     FragmentEffectAlpha.enable();
180

    
181
     DistortedLibrary.onCreate(mView.getContext(), this);
182
     }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
   public void distortedException(Exception ex)
187
     {
188
     android.util.Log.e("MovingEffects", ex.getMessage() );
189
     }
190
}
(2-2/3)