Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 42d910b3 Leszek Koltunski
import org.distorted.library.effect.Effect;
32
import org.distorted.library.effect.EffectType;
33 885b9cca leszek
import org.distorted.library.effect.FragmentEffectAlpha;
34
import org.distorted.library.effect.FragmentEffectChroma;
35 c7a31368 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
36 01cef452 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDeform;
37 42d910b3 Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
38 885b9cca leszek
import org.distorted.library.effect.VertexEffectSink;
39
import org.distorted.library.effect.VertexEffectSwirl;
40 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
41 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
42
import org.distorted.library.main.DistortedTexture;
43
import org.distorted.library.main.DistortedEffects;
44 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
45 c7a31368 Leszek Koltunski
import org.distorted.library.type.Static3D;
46 427ab7bf Leszek Koltunski
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 97dadfe5 Leszek Koltunski
class MovingEffectsRenderer implements GLSurfaceView.Renderer
50 427ab7bf Leszek Koltunski
   {  
51 4562f295 Leszek Koltunski
   private MovingEffectsSurfaceView mView;
52 427ab7bf Leszek Koltunski
   private Canvas mCanvas;
53
   private Bitmap mBitmap;
54
   private Paint mPaint;
55 97dadfe5 Leszek Koltunski
   private int texW, texH;
56
57 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
58 f6d884d5 Leszek Koltunski
   private DistortedTexture mTexture;
59 d218d64e leszek
   private DistortedScreen mScreen;
60 f3ca895f Leszek Koltunski
   private MeshRectangles mMesh;
61 3c8b1903 Leszek Koltunski
   private boolean mRefresh;
62 42d910b3 Leszek Koltunski
   private Static3D mScaleMatrix, mScaleVertex;
63 97dadfe5 Leszek Koltunski
64 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 4562f295 Leszek Koltunski
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
67 427ab7bf Leszek Koltunski
     {    
68
     mPaint = new Paint();
69
     mPaint.setAntiAlias(true);
70
     mPaint.setFakeBoldText(true);
71
     mPaint.setStyle(Style.FILL);
72 f6d884d5 Leszek Koltunski
73 392e16fd Leszek Koltunski
     mView   = v;
74 698ad0a8 Leszek Koltunski
     mEffects= new DistortedEffects();
75 e4330c89 Leszek Koltunski
     mScreen = new DistortedScreen();
76 c7a31368 Leszek Koltunski
     mTexture= new DistortedTexture();
77 392e16fd Leszek Koltunski
     mRefresh= true;
78 c7a31368 Leszek Koltunski
79 42d910b3 Leszek Koltunski
     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 427ab7bf Leszek Koltunski
     }
84
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87
   private void drawBackground()
88
     {
89 97dadfe5 Leszek Koltunski
     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 427ab7bf Leszek Koltunski
     mPaint.setColor(0xff008800);
103 97dadfe5 Leszek Koltunski
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
104 427ab7bf Leszek Koltunski
     mPaint.setColor(0xffffffff);
105
         
106 97dadfe5 Leszek Koltunski
     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 427ab7bf Leszek Koltunski
     }
109
   
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111 f6d884d5 Leszek Koltunski
112 42d910b3 Leszek Koltunski
   void apply(Effect effect)
113 f6d884d5 Leszek Koltunski
     {
114 42d910b3 Leszek Koltunski
     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 f6d884d5 Leszek Koltunski
     }
121
122 3c8b1903 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
   void setRefresh()
125
     {
126
     mRefresh = true;
127
     }
128
129 f6d884d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
130
131 427ab7bf Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
132 97dadfe5 Leszek Koltunski
     {
133 42d910b3 Leszek Koltunski
     VertexEffectScale.enable();
134 885b9cca leszek
     VertexEffectSwirl.enable();
135 01cef452 Leszek Koltunski
     VertexEffectDeform.enable();
136 885b9cca leszek
     VertexEffectSink.enable();
137
     FragmentEffectChroma.enable();
138
     FragmentEffectAlpha.enable();
139 6637d0f2 Leszek Koltunski
140 427ab7bf Leszek Koltunski
     try
141 97dadfe5 Leszek Koltunski
       {
142 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
143 97dadfe5 Leszek Koltunski
       }
144
     catch(Exception ex)
145
       {
146 9e7b6dbd Leszek Koltunski
       android.util.Log.e("MovingEffects", ex.getMessage() );
147 97dadfe5 Leszek Koltunski
       }
148 427ab7bf Leszek Koltunski
     }
149
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
152
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
153
     {
154 97dadfe5 Leszek Koltunski
     texW = width;
155
     texH = height;
156
157 42d910b3 Leszek Koltunski
     mScaleMatrix.set(width,width,width);
158
     mScaleVertex.set(1.0f, (float)texH/texW, 1.0f);
159 c7a31368 Leszek Koltunski
160 f6d884d5 Leszek Koltunski
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
161
     mCanvas  = new Canvas(mBitmap);
162 97dadfe5 Leszek Koltunski
163 5f2a53b6 leszek
     if( mMesh!=null ) mMesh.markForDeletion();
164 f3ca895f Leszek Koltunski
     mMesh = new MeshRectangles(80,80*texH/texW);
165 5f2a53b6 leszek
166 fe59d375 Leszek Koltunski
     mScreen.detachAll();
167 5f2a53b6 leszek
     mScreen.attach(mTexture,mEffects,mMesh);
168 392e16fd Leszek Koltunski
     mScreen.resize(texW, texH);
169 4562f295 Leszek Koltunski
     mView.onSurfaceChanged(texW,texH);
170 fd89ecb6 Leszek Koltunski
171
     mRefresh = true;
172 427ab7bf Leszek Koltunski
     }
173
   
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
   
176
   public void onDrawFrame(GL10 glUnused)
177
     {   
178
     long time = System.currentTimeMillis();
179
      
180 3c8b1903 Leszek Koltunski
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
181 427ab7bf Leszek Koltunski
       {
182 fd89ecb6 Leszek Koltunski
       drawBackground();
183 4562f295 Leszek Koltunski
       mView.drawCurve(mCanvas,time);
184 f6d884d5 Leszek Koltunski
       mTexture.setTexture(mBitmap);
185 3c8b1903 Leszek Koltunski
       mRefresh = false;
186 427ab7bf Leszek Koltunski
       }
187
      
188 fe59d375 Leszek Koltunski
     mScreen.render(time);
189 427ab7bf Leszek Koltunski
     }
190
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192
}