Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ c7a31368

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 c7a31368 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
34 01cef452 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDeform;
35 885b9cca leszek
import org.distorted.library.effect.VertexEffectSink;
36
import org.distorted.library.effect.VertexEffectSwirl;
37 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
38 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.main.DistortedEffects;
41 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
42 c7a31368 Leszek Koltunski
import org.distorted.library.type.Static3D;
43 427ab7bf Leszek Koltunski
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46 97dadfe5 Leszek Koltunski
class MovingEffectsRenderer implements GLSurfaceView.Renderer
47 427ab7bf Leszek Koltunski
   {  
48 4562f295 Leszek Koltunski
   private MovingEffectsSurfaceView mView;
49 427ab7bf Leszek Koltunski
   private Canvas mCanvas;
50
   private Bitmap mBitmap;
51
   private Paint mPaint;
52 97dadfe5 Leszek Koltunski
   private int texW, texH;
53
54 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
55 f6d884d5 Leszek Koltunski
   private DistortedTexture mTexture;
56 d218d64e leszek
   private DistortedScreen mScreen;
57 f3ca895f Leszek Koltunski
   private MeshRectangles mMesh;
58 3c8b1903 Leszek Koltunski
   private boolean mRefresh;
59 c7a31368 Leszek Koltunski
   private Static3D mScale;
60 97dadfe5 Leszek Koltunski
61 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 4562f295 Leszek Koltunski
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
64 427ab7bf Leszek Koltunski
     {    
65
     mPaint = new Paint();
66
     mPaint.setAntiAlias(true);
67
     mPaint.setFakeBoldText(true);
68
     mPaint.setStyle(Style.FILL);
69 f6d884d5 Leszek Koltunski
70 392e16fd Leszek Koltunski
     mView   = v;
71 698ad0a8 Leszek Koltunski
     mEffects= new DistortedEffects();
72 e4330c89 Leszek Koltunski
     mScreen = new DistortedScreen();
73 c7a31368 Leszek Koltunski
     mTexture= new DistortedTexture();
74 392e16fd Leszek Koltunski
     mRefresh= true;
75 c7a31368 Leszek Koltunski
76
     mScale = new Static3D(1,1,1);
77
     mEffects.apply( new MatrixEffectScale(mScale));
78 427ab7bf Leszek Koltunski
     }
79
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
82
   private void drawBackground()
83
     {
84 97dadfe5 Leszek Koltunski
     int NW, NH;
85
86
     if( texH<texW )
87
       {
88
       NH = 10;
89
       NW = NH*texW/texH;
90
       }
91
     else
92
       {
93
       NW = 10;
94
       NH = NW*texH/texW;
95
       }
96
97 427ab7bf Leszek Koltunski
     mPaint.setColor(0xff008800);
98 97dadfe5 Leszek Koltunski
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
99 427ab7bf Leszek Koltunski
     mPaint.setColor(0xffffffff);
100
         
101 97dadfe5 Leszek Koltunski
     for(int i=0; i<=NH ; i++ ) mCanvas.drawRect( 0, texH*i/NH -2,  texW,  texH*i/NH + 2, mPaint);
102
     for(int i=0; i<=NW ; i++ ) mCanvas.drawRect( texW*i/NW - 2, 0, texW*i/NW + 2,  texH, mPaint);
103 427ab7bf Leszek Koltunski
     }
104
   
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106 f6d884d5 Leszek Koltunski
107 d04a4886 Leszek Koltunski
   DistortedEffects getEffects()
108 f6d884d5 Leszek Koltunski
     {
109 392e16fd Leszek Koltunski
     return mEffects;
110 f6d884d5 Leszek Koltunski
     }
111
112 3c8b1903 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
113
114
   void setRefresh()
115
     {
116
     mRefresh = true;
117
     }
118
119 f6d884d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121 427ab7bf Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
122 97dadfe5 Leszek Koltunski
     {
123 885b9cca leszek
     VertexEffectSwirl.enable();
124 01cef452 Leszek Koltunski
     VertexEffectDeform.enable();
125 885b9cca leszek
     VertexEffectSink.enable();
126
     FragmentEffectChroma.enable();
127
     FragmentEffectAlpha.enable();
128 6637d0f2 Leszek Koltunski
129 427ab7bf Leszek Koltunski
     try
130 97dadfe5 Leszek Koltunski
       {
131 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
132 97dadfe5 Leszek Koltunski
       }
133
     catch(Exception ex)
134
       {
135 9e7b6dbd Leszek Koltunski
       android.util.Log.e("MovingEffects", ex.getMessage() );
136 97dadfe5 Leszek Koltunski
       }
137 427ab7bf Leszek Koltunski
     }
138
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
141
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
142
     {
143 97dadfe5 Leszek Koltunski
     texW = width;
144
     texH = height;
145
146 c7a31368 Leszek Koltunski
     mScale.set(width,height,width);
147
148 f6d884d5 Leszek Koltunski
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
149
     mCanvas  = new Canvas(mBitmap);
150 97dadfe5 Leszek Koltunski
151 5f2a53b6 leszek
     if( mMesh!=null ) mMesh.markForDeletion();
152 f3ca895f Leszek Koltunski
     mMesh = new MeshRectangles(80,80*texH/texW);
153 5f2a53b6 leszek
154 fe59d375 Leszek Koltunski
     mScreen.detachAll();
155 5f2a53b6 leszek
     mScreen.attach(mTexture,mEffects,mMesh);
156 392e16fd Leszek Koltunski
     mScreen.resize(texW, texH);
157 4562f295 Leszek Koltunski
     mView.onSurfaceChanged(texW,texH);
158 fd89ecb6 Leszek Koltunski
159
     mRefresh = true;
160 427ab7bf Leszek Koltunski
     }
161
   
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
   
164
   public void onDrawFrame(GL10 glUnused)
165
     {   
166
     long time = System.currentTimeMillis();
167
      
168 3c8b1903 Leszek Koltunski
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
169 427ab7bf Leszek Koltunski
       {
170 fd89ecb6 Leszek Koltunski
       drawBackground();
171 4562f295 Leszek Koltunski
       mView.drawCurve(mCanvas,time);
172 f6d884d5 Leszek Koltunski
       mTexture.setTexture(mBitmap);
173 3c8b1903 Leszek Koltunski
       mRefresh = false;
174 427ab7bf Leszek Koltunski
       }
175
      
176 fe59d375 Leszek Koltunski
     mScreen.render(time);
177 427ab7bf Leszek Koltunski
     }
178
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
}