Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ 698ad0a8

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 885b9cca leszek
import org.distorted.library.effect.FragmentEffectAlpha;
32
import org.distorted.library.effect.FragmentEffectChroma;
33 01cef452 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDeform;
34 885b9cca leszek
import org.distorted.library.effect.VertexEffectSink;
35
import org.distorted.library.effect.VertexEffectSwirl;
36 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
37 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.main.DistortedEffects;
40 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
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 f3ca895f Leszek Koltunski
   private MeshRectangles 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 698ad0a8 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 01cef452 Leszek Koltunski
     VertexEffectDeform.enable();
118 885b9cca leszek
     VertexEffectSink.enable();
119
     FragmentEffectChroma.enable();
120
     FragmentEffectAlpha.enable();
121 6637d0f2 Leszek Koltunski
122 427ab7bf Leszek Koltunski
     try
123 97dadfe5 Leszek Koltunski
       {
124 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
125 97dadfe5 Leszek Koltunski
       }
126
     catch(Exception ex)
127
       {
128 9e7b6dbd Leszek Koltunski
       android.util.Log.e("MovingEffects", ex.getMessage() );
129 97dadfe5 Leszek Koltunski
       }
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 687263cc Leszek Koltunski
     mTexture = new DistortedTexture();
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 f3ca895f Leszek Koltunski
     mMesh = new MeshRectangles(80,80*texH/texW);
146 698ad0a8 Leszek Koltunski
     mMesh.setStretch(texW,texH,0);
147 5f2a53b6 leszek
148 fe59d375 Leszek Koltunski
     mScreen.detachAll();
149 5f2a53b6 leszek
     mScreen.attach(mTexture,mEffects,mMesh);
150 392e16fd Leszek Koltunski
     mScreen.resize(texW, texH);
151 4562f295 Leszek Koltunski
     mView.onSurfaceChanged(texW,texH);
152 fd89ecb6 Leszek Koltunski
153
     mRefresh = true;
154 427ab7bf Leszek Koltunski
     }
155
   
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
   
158
   public void onDrawFrame(GL10 glUnused)
159
     {   
160
     long time = System.currentTimeMillis();
161
      
162 3c8b1903 Leszek Koltunski
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
163 427ab7bf Leszek Koltunski
       {
164 fd89ecb6 Leszek Koltunski
       drawBackground();
165 4562f295 Leszek Koltunski
       mView.drawCurve(mCanvas,time);
166 f6d884d5 Leszek Koltunski
       mTexture.setTexture(mBitmap);
167 3c8b1903 Leszek Koltunski
       mRefresh = false;
168 427ab7bf Leszek Koltunski
       }
169
      
170 fe59d375 Leszek Koltunski
     mScreen.render(time);
171 427ab7bf Leszek Koltunski
     }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
}