Project

General

Profile

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

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

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