Project

General

Profile

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

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

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.FragmentEffectAlpha;
32
import org.distorted.library.effect.FragmentEffectChroma;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectDeform;
35
import org.distorted.library.effect.VertexEffectSink;
36
import org.distorted.library.effect.VertexEffectSwirl;
37
import org.distorted.library.main.DistortedLibrary;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.main.DistortedEffects;
41
import org.distorted.library.mesh.MeshRectangles;
42
import org.distorted.library.type.Static3D;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
class MovingEffectsRenderer implements GLSurfaceView.Renderer
47
   {  
48
   private MovingEffectsSurfaceView mView;
49
   private Canvas mCanvas;
50
   private Bitmap mBitmap;
51
   private Paint mPaint;
52
   private int texW, texH;
53

    
54
   private DistortedEffects mEffects;
55
   private DistortedTexture mTexture;
56
   private DistortedScreen mScreen;
57
   private MeshRectangles mMesh;
58
   private boolean mRefresh;
59
   private Static3D mScale;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
64
     {    
65
     mPaint = new Paint();
66
     mPaint.setAntiAlias(true);
67
     mPaint.setFakeBoldText(true);
68
     mPaint.setStyle(Style.FILL);
69

    
70
     mView   = v;
71
     mEffects= new DistortedEffects();
72
     mScreen = new DistortedScreen();
73
     mTexture= new DistortedTexture();
74
     mRefresh= true;
75

    
76
     mScale = new Static3D(1,1,1);
77
     mEffects.apply( new MatrixEffectScale(mScale));
78
     }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
   private void drawBackground()
83
     {
84
     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
     mPaint.setColor(0xff008800);
98
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
99
     mPaint.setColor(0xffffffff);
100
         
101
     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
     }
104
   
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
   DistortedEffects getEffects()
108
     {
109
     return mEffects;
110
     }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
   void setRefresh()
115
     {
116
     mRefresh = true;
117
     }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
122
     {
123
     VertexEffectSwirl.enable();
124
     VertexEffectDeform.enable();
125
     VertexEffectSink.enable();
126
     FragmentEffectChroma.enable();
127
     FragmentEffectAlpha.enable();
128

    
129
     try
130
       {
131
       DistortedLibrary.onCreate(mView.getContext());
132
       }
133
     catch(Exception ex)
134
       {
135
       android.util.Log.e("MovingEffects", ex.getMessage() );
136
       }
137
     }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
142
     {
143
     texW = width;
144
     texH = height;
145

    
146
     mScale.set(width,height,width);
147

    
148
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
149
     mCanvas  = new Canvas(mBitmap);
150

    
151
     if( mMesh!=null ) mMesh.markForDeletion();
152
     mMesh = new MeshRectangles(80,80*texH/texW);
153

    
154
     mScreen.detachAll();
155
     mScreen.attach(mTexture,mEffects,mMesh);
156
     mScreen.resize(texW, texH);
157
     mView.onSurfaceChanged(texW,texH);
158

    
159
     mRefresh = true;
160
     }
161
   
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
   
164
   public void onDrawFrame(GL10 glUnused)
165
     {   
166
     long time = System.currentTimeMillis();
167
      
168
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
169
       {
170
       drawBackground();
171
       mView.drawCurve(mCanvas,time);
172
       mTexture.setTexture(mBitmap);
173
       mRefresh = false;
174
       }
175
      
176
     mScreen.render(time);
177
     }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
}
(2-2/3)