Project

General

Profile

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

examples / src / main / java / org / distorted / examples / polymesh / PolymeshRenderer.java @ 4bfffdd5

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.polymesh;
21

    
22
import android.content.res.Resources;
23
import android.graphics.Bitmap;
24
import android.graphics.Canvas;
25
import android.graphics.Paint;
26
import android.graphics.Paint.Style;
27
import android.opengl.GLSurfaceView;
28

    
29
import org.distorted.library.effect.Effect;
30
import org.distorted.library.effect.EffectType;
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.VertexEffectScale;
36
import org.distorted.library.effect.VertexEffectSink;
37
import org.distorted.library.effect.VertexEffectSwirl;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.main.DistortedLibrary;
40
import org.distorted.library.main.DistortedScreen;
41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.mesh.MeshSquare;
43
import org.distorted.library.type.Static3D;
44

    
45
import java.io.InputStream;
46

    
47
import javax.microedition.khronos.egl.EGLConfig;
48
import javax.microedition.khronos.opengles.GL10;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class PolymeshRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
53
   {  
54
   private final PolymeshSurfaceView mView;
55
   private final DistortedEffects mEffects;
56
   private final DistortedTexture mTexture;
57
   private final DistortedScreen mScreen;
58
   private final Static3D mScaleMatrix, mScaleVertex;
59
   private final Paint mPaint;
60
   private final Resources mResources;
61

    
62
   private MeshSquare mMesh;
63
   private Canvas mCanvas;
64
   private Bitmap mBitmap;
65
   private int texW, texH;
66
   private boolean mRefresh;
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
   PolymeshRenderer(PolymeshSurfaceView v)
71
     {
72
     mView = v;
73
     mResources = v.getResources();
74

    
75
     mPaint = new Paint();
76
     mPaint.setAntiAlias(true);
77
     mPaint.setFakeBoldText(true);
78
     mPaint.setStyle(Style.FILL);
79

    
80
     mEffects= new DistortedEffects();
81
     mScreen = new DistortedScreen();
82
     mTexture= new DistortedTexture();
83
     mRefresh= true;
84

    
85
     mScaleMatrix = new Static3D(1,1,1);
86
     mScaleVertex = new Static3D(1,1,1);
87
     mEffects.apply( new MatrixEffectScale(mScaleMatrix));
88
     mEffects.apply( new VertexEffectScale(mScaleVertex));
89
     }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
   private void drawBackground()
94
     {
95
     int NW, NH;
96

    
97
     if( texH<texW )
98
       {
99
       NH = 10;
100
       NW = NH*texW/texH;
101
       }
102
     else
103
       {
104
       NW = 10;
105
       NH = NW*texH/texW;
106
       }
107

    
108
     mPaint.setColor(0xff008800);
109
     mCanvas.drawRect(0, 0, texW, texH, mPaint);
110
     mPaint.setColor(0xffffffff);
111
         
112
     for(int i=0; i<=NH ; i++ ) mCanvas.drawRect( 0, texH*i/NH -2,  texW,  texH*i/NH + 2, mPaint);
113
     for(int i=0; i<=NW ; i++ ) mCanvas.drawRect( texW*i/NW - 2, 0, texW*i/NW + 2,  texH, mPaint);
114
     }
115
   
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
   void apply(Effect effect)
119
     {
120
     mEffects.abortByType(EffectType.VERTEX);
121
     mEffects.abortByType(EffectType.FRAGMENT);
122

    
123
     mEffects.apply( new VertexEffectScale(mScaleVertex));
124

    
125
     if( effect!=null ) mEffects.apply(effect);
126
     }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
   void setRefresh()
131
     {
132
     mRefresh = true;
133
     }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
   public void onDrawFrame(GL10 glUnused)
138
     {
139
     long time = System.currentTimeMillis();
140

    
141
     if (mView.getCurrentEffect() == PolymeshSurfaceView.EFFECT_POINTS && mRefresh )
142
       {
143
       drawBackground();
144
       mView.drawCurve(mCanvas,time);
145
       mTexture.setTexture(mBitmap);
146
       mRefresh = false;
147
       }
148

    
149
     mScreen.render(time);
150
     }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
155
     {
156
     texW = width;
157
     texH = height;
158

    
159
     mScaleMatrix.set(width,width,width);
160
     mScaleVertex.set(1.0f, (float)texH/texW, 1.0f);
161

    
162
     mBitmap  = Bitmap.createBitmap(texW,texH, Bitmap.Config.ARGB_8888);
163
     mCanvas  = new Canvas(mBitmap);
164

    
165
     if( mMesh!=null ) mMesh.markForDeletion();
166
     mMesh = new MeshSquare(80,80*texH/texW);
167

    
168
     mScreen.detachAll();
169
     mScreen.attach(mTexture,mEffects,mMesh);
170
     mScreen.resize(texW, texH);
171
     mView.onSurfaceChanged(texW,texH);
172

    
173
     mRefresh = true;
174
     }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
179
     {
180
     VertexEffectScale.enable();
181
     VertexEffectSwirl.enable();
182
     VertexEffectDeform.enable();
183
     VertexEffectSink.enable();
184
     FragmentEffectChroma.enable();
185
     FragmentEffectAlpha.enable();
186

    
187
     DistortedLibrary.onSurfaceCreated(this);
188
     }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
   public void distortedException(Exception ex)
193
     {
194
     android.util.Log.e("MovingEffects", ex.getMessage() );
195
     }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
   public InputStream localFile(int fileID)
200
      {
201
      return mResources.openRawResource(fileID);
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
   public void logMessage(String message)
207
      {
208
      android.util.Log.e("MovingEffects", message );
209
      }
210
}
(2-2/3)