Project

General

Profile

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

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

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.GLES20;
30
import android.opengl.GLSurfaceView;
31

    
32
import org.distorted.library.DistortedFramebuffer;
33
import org.distorted.library.DistortedTexture;
34
import org.distorted.library.DistortedEffects;
35
import org.distorted.library.MeshFlat;
36
import org.distorted.library.Distorted;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
class MovingEffectsRenderer implements GLSurfaceView.Renderer
41
   {  
42
   private MovingEffectsSurfaceView mView;
43
   private Canvas mCanvas;
44
   private Bitmap mBitmap;
45
   private Paint mPaint;
46
   private int texW, texH;
47

    
48
   private MeshFlat mMesh;
49
   private DistortedEffects mEffects;
50
   private DistortedTexture mTexture;
51
   private DistortedFramebuffer mScreen;
52
   private boolean mRefresh;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
57
     {    
58
     mPaint = new Paint();
59
     mPaint.setAntiAlias(true);
60
     mPaint.setFakeBoldText(true);
61
     mPaint.setStyle(Style.FILL);
62

    
63
     mView   = v;
64
     mEffects= new DistortedEffects();
65
     mScreen = new DistortedFramebuffer(0);
66
     mRefresh= true;
67
     }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
   private void drawBackground()
72
     {
73
     int NW, NH;
74

    
75
     if( texH<texW )
76
       {
77
       NH = 10;
78
       NW = NH*texW/texH;
79
       }
80
     else
81
       {
82
       NW = 10;
83
       NH = NW*texH/texW;
84
       }
85

    
86
     mPaint.setColor(0xff008800);
87
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
88
     mPaint.setColor(0xffffffff);
89
         
90
     for(int i=0; i<=NH ; i++ ) mCanvas.drawRect( 0, texH*i/NH -2,  texW,  texH*i/NH + 2, mPaint);
91
     for(int i=0; i<=NW ; i++ ) mCanvas.drawRect( texW*i/NW - 2, 0, texW*i/NW + 2,  texH, mPaint);
92
     }
93
   
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
   DistortedEffects getEffects()
97
     {
98
     return mEffects;
99
     }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
   void setRefresh()
104
     {
105
     mRefresh = true;
106
     }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
111
     {
112
     try
113
       {
114
       Distorted.onCreate(mView.getContext());
115
       }
116
     catch(Exception ex)
117
       {
118
       android.util.Log.e("Renderer", ex.getMessage() );
119
       }
120
     }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
125
     {
126
     texW = width;
127
     texH = height;
128

    
129
     mMesh = new MeshFlat(80,80*texH/texW);
130
     mTexture = new DistortedTexture(texW,texH);
131
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
132
     mCanvas  = new Canvas(mBitmap);
133

    
134
     mScreen.resize(texW, texH);
135
     mView.onSurfaceChanged(texW,texH);
136

    
137
     mRefresh = true;
138
     }
139
   
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
   
142
   public void onDrawFrame(GL10 glUnused)
143
     {   
144
     GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);               
145
       
146
     long time = System.currentTimeMillis();
147
      
148
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
149
       {
150
       drawBackground();
151
       mView.drawCurve(mCanvas,time);
152
       mTexture.setTexture(mBitmap);
153
       mRefresh = false;
154
       }
155
      
156
     mScreen.renderTo(mTexture, mMesh, mEffects, time );
157
     }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
}
(2-2/3)