Project

General

Profile

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

examples / src / main / java / org / distorted / examples / movingeffects / MovingEffectsRenderer.java @ 77e66c58

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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
26
import android.content.Context;
27
import android.content.pm.ConfigurationInfo;
28
import android.content.res.Resources;
29 427ab7bf Leszek Koltunski
import android.graphics.Bitmap;
30
import android.graphics.Canvas;
31
import android.graphics.Paint;
32
import android.graphics.Paint.Style;
33
import android.opengl.GLSurfaceView;
34
35 42d910b3 Leszek Koltunski
import org.distorted.library.effect.Effect;
36
import org.distorted.library.effect.EffectType;
37 885b9cca leszek
import org.distorted.library.effect.FragmentEffectAlpha;
38
import org.distorted.library.effect.FragmentEffectChroma;
39 c7a31368 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
40 01cef452 Leszek Koltunski
import org.distorted.library.effect.VertexEffectDeform;
41 42d910b3 Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
42 885b9cca leszek
import org.distorted.library.effect.VertexEffectSink;
43
import org.distorted.library.effect.VertexEffectSwirl;
44 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
45 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
46
import org.distorted.library.main.DistortedTexture;
47
import org.distorted.library.main.DistortedEffects;
48 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
49 c7a31368 Leszek Koltunski
import org.distorted.library.type.Static3D;
50 427ab7bf Leszek Koltunski
51 dc10a48d Leszek Koltunski
import java.io.InputStream;
52
53 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 dc10a48d Leszek Koltunski
class MovingEffectsRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
56 427ab7bf Leszek Koltunski
   {  
57 dc10a48d Leszek Koltunski
   private final MovingEffectsSurfaceView mView;
58
   private final DistortedEffects mEffects;
59
   private final DistortedTexture mTexture;
60
   private final DistortedScreen mScreen;
61
   private final Static3D mScaleMatrix, mScaleVertex;
62
   private final Paint mPaint;
63
   private final Resources mResources;
64
65
   private MeshSquare mMesh;
66 427ab7bf Leszek Koltunski
   private Canvas mCanvas;
67
   private Bitmap mBitmap;
68 97dadfe5 Leszek Koltunski
   private int texW, texH;
69 3c8b1903 Leszek Koltunski
   private boolean mRefresh;
70 97dadfe5 Leszek Koltunski
71 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
72
73 4562f295 Leszek Koltunski
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
74 dc10a48d Leszek Koltunski
     {
75
     mView = v;
76
     mResources = v.getResources();
77
78 427ab7bf Leszek Koltunski
     mPaint = new Paint();
79
     mPaint.setAntiAlias(true);
80
     mPaint.setFakeBoldText(true);
81
     mPaint.setStyle(Style.FILL);
82 f6d884d5 Leszek Koltunski
83 698ad0a8 Leszek Koltunski
     mEffects= new DistortedEffects();
84 e4330c89 Leszek Koltunski
     mScreen = new DistortedScreen();
85 c7a31368 Leszek Koltunski
     mTexture= new DistortedTexture();
86 392e16fd Leszek Koltunski
     mRefresh= true;
87 c7a31368 Leszek Koltunski
88 42d910b3 Leszek Koltunski
     mScaleMatrix = new Static3D(1,1,1);
89
     mScaleVertex = new Static3D(1,1,1);
90
     mEffects.apply( new MatrixEffectScale(mScaleMatrix));
91
     mEffects.apply( new VertexEffectScale(mScaleVertex));
92 427ab7bf Leszek Koltunski
     }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96
   private void drawBackground()
97
     {
98 97dadfe5 Leszek Koltunski
     int NW, NH;
99
100
     if( texH<texW )
101
       {
102
       NH = 10;
103
       NW = NH*texW/texH;
104
       }
105
     else
106
       {
107
       NW = 10;
108
       NH = NW*texH/texW;
109
       }
110
111 427ab7bf Leszek Koltunski
     mPaint.setColor(0xff008800);
112 97dadfe5 Leszek Koltunski
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
113 427ab7bf Leszek Koltunski
     mPaint.setColor(0xffffffff);
114
         
115 97dadfe5 Leszek Koltunski
     for(int i=0; i<=NH ; i++ ) mCanvas.drawRect( 0, texH*i/NH -2,  texW,  texH*i/NH + 2, mPaint);
116
     for(int i=0; i<=NW ; i++ ) mCanvas.drawRect( texW*i/NW - 2, 0, texW*i/NW + 2,  texH, mPaint);
117 427ab7bf Leszek Koltunski
     }
118
   
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120 f6d884d5 Leszek Koltunski
121 42d910b3 Leszek Koltunski
   void apply(Effect effect)
122 f6d884d5 Leszek Koltunski
     {
123 42d910b3 Leszek Koltunski
     mEffects.abortByType(EffectType.VERTEX);
124
     mEffects.abortByType(EffectType.FRAGMENT);
125
126
     mEffects.apply( new VertexEffectScale(mScaleVertex));
127
128
     if( effect!=null ) mEffects.apply(effect);
129 f6d884d5 Leszek Koltunski
     }
130
131 3c8b1903 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
132
133
   void setRefresh()
134
     {
135
     mRefresh = true;
136
     }
137
138 f6d884d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
139
140 061449ed Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
141 97dadfe5 Leszek Koltunski
     {
142 061449ed Leszek Koltunski
     long time = System.currentTimeMillis();
143 6637d0f2 Leszek Koltunski
144 061449ed Leszek Koltunski
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
145 97dadfe5 Leszek Koltunski
       {
146 061449ed Leszek Koltunski
       drawBackground();
147
       mView.drawCurve(mCanvas,time);
148
       mTexture.setTexture(mBitmap);
149
       mRefresh = false;
150 97dadfe5 Leszek Koltunski
       }
151 061449ed Leszek Koltunski
152
     mScreen.render(time);
153 427ab7bf Leszek Koltunski
     }
154
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
158
     {
159 97dadfe5 Leszek Koltunski
     texW = width;
160
     texH = height;
161
162 42d910b3 Leszek Koltunski
     mScaleMatrix.set(width,width,width);
163
     mScaleVertex.set(1.0f, (float)texH/texW, 1.0f);
164 c7a31368 Leszek Koltunski
165 f6d884d5 Leszek Koltunski
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
166
     mCanvas  = new Canvas(mBitmap);
167 97dadfe5 Leszek Koltunski
168 5f2a53b6 leszek
     if( mMesh!=null ) mMesh.markForDeletion();
169 f2085b96 Leszek Koltunski
     mMesh = new MeshSquare(80,80*texH/texW);
170 5f2a53b6 leszek
171 fe59d375 Leszek Koltunski
     mScreen.detachAll();
172 5f2a53b6 leszek
     mScreen.attach(mTexture,mEffects,mMesh);
173 392e16fd Leszek Koltunski
     mScreen.resize(texW, texH);
174 4562f295 Leszek Koltunski
     mView.onSurfaceChanged(texW,texH);
175 fd89ecb6 Leszek Koltunski
176
     mRefresh = true;
177 427ab7bf Leszek Koltunski
     }
178 061449ed Leszek Koltunski
179 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
180 061449ed Leszek Koltunski
181
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
182
     {
183
     VertexEffectScale.enable();
184
     VertexEffectSwirl.enable();
185
     VertexEffectDeform.enable();
186
     VertexEffectSink.enable();
187
     FragmentEffectChroma.enable();
188
     FragmentEffectAlpha.enable();
189
190 dc10a48d Leszek Koltunski
     DistortedLibrary.onSurfaceCreated(this);
191 427ab7bf Leszek Koltunski
     }
192
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194 061449ed Leszek Koltunski
195
   public void distortedException(Exception ex)
196
     {
197
     android.util.Log.e("MovingEffects", ex.getMessage() );
198
     }
199 dc10a48d Leszek Koltunski
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202
   public InputStream localFile(int fileID)
203
      {
204
      return mResources.openRawResource(fileID);
205
      }
206
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
209
   public void logMessage(String message)
210
      {
211
      android.util.Log.e("MovingEffects", message );
212
      }
213 427ab7bf Leszek Koltunski
}