Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 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.GLES20;
30
import android.opengl.GLSurfaceView;
31
32 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
33 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
34 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
35 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
36 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
37 427ab7bf Leszek Koltunski
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40 97dadfe5 Leszek Koltunski
class MovingEffectsRenderer implements GLSurfaceView.Renderer
41 427ab7bf Leszek Koltunski
   {  
42 4562f295 Leszek Koltunski
   private MovingEffectsSurfaceView mView;
43 427ab7bf Leszek Koltunski
   private Canvas mCanvas;
44
   private Bitmap mBitmap;
45
   private Paint mPaint;
46 97dadfe5 Leszek Koltunski
   private int texW, texH;
47
48 b01acdaf Leszek Koltunski
   private MeshFlat mMesh;
49 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
50 f6d884d5 Leszek Koltunski
   private DistortedTexture mTexture;
51 392e16fd Leszek Koltunski
   private DistortedFramebuffer mScreen;
52 3c8b1903 Leszek Koltunski
   private boolean mRefresh;
53 97dadfe5 Leszek Koltunski
54 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 4562f295 Leszek Koltunski
   MovingEffectsRenderer(MovingEffectsSurfaceView v)
57 427ab7bf Leszek Koltunski
     {    
58
     mPaint = new Paint();
59
     mPaint.setAntiAlias(true);
60
     mPaint.setFakeBoldText(true);
61
     mPaint.setStyle(Style.FILL);
62 f6d884d5 Leszek Koltunski
63 392e16fd Leszek Koltunski
     mView   = v;
64 d04a4886 Leszek Koltunski
     mEffects= new DistortedEffects();
65 392e16fd Leszek Koltunski
     mScreen = new DistortedFramebuffer(0);
66
     mRefresh= true;
67 427ab7bf Leszek Koltunski
     }
68
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71
   private void drawBackground()
72
     {
73 97dadfe5 Leszek Koltunski
     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 427ab7bf Leszek Koltunski
     mPaint.setColor(0xff008800);
87 97dadfe5 Leszek Koltunski
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
88 427ab7bf Leszek Koltunski
     mPaint.setColor(0xffffffff);
89
         
90 97dadfe5 Leszek Koltunski
     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 427ab7bf Leszek Koltunski
     }
93
   
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95 f6d884d5 Leszek Koltunski
96 d04a4886 Leszek Koltunski
   DistortedEffects getEffects()
97 f6d884d5 Leszek Koltunski
     {
98 392e16fd Leszek Koltunski
     return mEffects;
99 f6d884d5 Leszek Koltunski
     }
100
101 3c8b1903 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103
   void setRefresh()
104
     {
105
     mRefresh = true;
106
     }
107
108 f6d884d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
109
110 427ab7bf Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
111 97dadfe5 Leszek Koltunski
     {
112 427ab7bf Leszek Koltunski
     try
113 97dadfe5 Leszek Koltunski
       {
114 76f9798b Leszek Koltunski
       Distorted.onCreate(mView.getContext());
115 97dadfe5 Leszek Koltunski
       }
116
     catch(Exception ex)
117
       {
118
       android.util.Log.e("Renderer", ex.getMessage() );
119
       }
120 427ab7bf Leszek Koltunski
     }
121
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
124
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
125
     {
126 97dadfe5 Leszek Koltunski
     texW = width;
127
     texH = height;
128
129 b01acdaf Leszek Koltunski
     mMesh = new MeshFlat(80,80*texH/texW);
130 7451c98a Leszek Koltunski
     mTexture = new DistortedTexture(texW,texH);
131 f6d884d5 Leszek Koltunski
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
132
     mCanvas  = new Canvas(mBitmap);
133 97dadfe5 Leszek Koltunski
134 392e16fd Leszek Koltunski
     mScreen.resize(texW, texH);
135 4562f295 Leszek Koltunski
     mView.onSurfaceChanged(texW,texH);
136 fd89ecb6 Leszek Koltunski
137
     mRefresh = true;
138 427ab7bf Leszek Koltunski
     }
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 3c8b1903 Leszek Koltunski
     if (mView.getCurrentEffect() == MovingEffectsSurfaceView.EFFECT_POINTS && mRefresh )
149 427ab7bf Leszek Koltunski
       {
150 fd89ecb6 Leszek Koltunski
       drawBackground();
151 4562f295 Leszek Koltunski
       mView.drawCurve(mCanvas,time);
152 f6d884d5 Leszek Koltunski
       mTexture.setTexture(mBitmap);
153 3c8b1903 Leszek Koltunski
       mRefresh = false;
154 427ab7bf Leszek Koltunski
       }
155
      
156 b01acdaf Leszek Koltunski
     mScreen.renderTo(mTexture, mMesh, mEffects, time );
157 427ab7bf Leszek Koltunski
     }
158
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
}