Project

General

Profile

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

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

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

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

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

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

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
64
     mView   = v;
65
     mEffects= new DistortedEffects();
66
     mScreen = new DistortedScreen();
67
     mRefresh= true;
68
     }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

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

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

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

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

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

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

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

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

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

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

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