Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effectqueue / EffectQueueRenderer.java @ a4d59c0b

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 758303a3 Leszek Koltunski
package org.distorted.examples.effectqueue;
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.GLSurfaceView;
30
31 885b9cca leszek
import org.distorted.library.effect.FragmentEffectAlpha;
32
import org.distorted.library.effect.FragmentEffectChroma;
33
import org.distorted.library.effect.FragmentEffectSaturation;
34 8dfa45c4 leszek
import org.distorted.library.effect.MatrixEffectScale;
35 885b9cca leszek
import org.distorted.library.effect.VertexEffectDistort;
36
import org.distorted.library.effect.VertexEffectSink;
37 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedScreen;
39 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshFlat;
40 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
41
import org.distorted.library.main.Distorted;
42 af225332 Leszek Koltunski
import org.distorted.library.message.EffectListener;
43
import org.distorted.library.message.EffectMessage;
44 630703d1 leszek
import org.distorted.library.type.Static3D;
45 427ab7bf Leszek Koltunski
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48 f6d884d5 Leszek Koltunski
class EffectQueueRenderer implements GLSurfaceView.Renderer, EffectListener
49 bc0a685b Leszek Koltunski
  {  
50 3f07bedc Leszek Koltunski
  private static final int NUMLINES =  10;
51 1585ba24 Leszek Koltunski
  private static final int MESH_QUALITY = 100;
52
  static final int BWID = 600;
53
  static final int BHEI = 600;
54 427ab7bf Leszek Koltunski
   
55 758303a3 Leszek Koltunski
  private EffectQueueSurfaceView mView;
56 bc0a685b Leszek Koltunski
  private Paint mPaint;
57
  private int texWidth, texHeight;
58 8dfa45c4 leszek
  private Static3D mScale;
59 3f07bedc Leszek Koltunski
60 f6d884d5 Leszek Koltunski
  private DistortedTexture mTexture;
61 b01acdaf Leszek Koltunski
  private MeshFlat mMesh;
62 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
63 d218d64e leszek
  private DistortedScreen mScreen;
64 3f07bedc Leszek Koltunski
65 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67 758303a3 Leszek Koltunski
  EffectQueueRenderer(EffectQueueSurfaceView v)
68 bc0a685b Leszek Koltunski
    {    
69
    mPaint = new Paint();
70
    mPaint.setAntiAlias(true);
71
    mPaint.setFakeBoldText(true);
72
    mPaint.setStyle(Style.FILL);
73 427ab7bf Leszek Koltunski
      
74 bc0a685b Leszek Koltunski
    mView = v;
75 427ab7bf Leszek Koltunski
      
76 bc0a685b Leszek Koltunski
    texWidth = BWID;
77
    texHeight= BHEI;
78 8dfa45c4 leszek
    mScale = new Static3D(1,1,1);
79
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
80 e8b6aa95 Leszek Koltunski
81 1585ba24 Leszek Koltunski
    mMesh = new MeshFlat(MESH_QUALITY,MESH_QUALITY*texHeight/texWidth);
82 7451c98a Leszek Koltunski
    mTexture = new DistortedTexture(texWidth,texHeight);
83 d04a4886 Leszek Koltunski
    mEffects = new DistortedEffects();
84 8dfa45c4 leszek
    mEffects.apply(scaleEffect);
85 392e16fd Leszek Koltunski
    mEffects.registerForMessages(this);
86
87 e4330c89 Leszek Koltunski
    mScreen = new DistortedScreen();
88 bc0a685b Leszek Koltunski
    }
89 427ab7bf Leszek Koltunski
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91 f6d884d5 Leszek Koltunski
92 d04a4886 Leszek Koltunski
  DistortedEffects getEffects()
93 f6d884d5 Leszek Koltunski
    {
94 392e16fd Leszek Koltunski
    return mEffects;
95 f6d884d5 Leszek Koltunski
    }
96
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
99 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
100 e8b6aa95 Leszek Koltunski
    {
101 bc0a685b Leszek Koltunski
    Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
102 e8b6aa95 Leszek Koltunski
    Canvas canvas = new Canvas(bitmap);
103
104 bc0a685b Leszek Koltunski
    mPaint.setColor(0xff008800);
105
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
106
    mPaint.setColor(0xffffffff);
107 e8b6aa95 Leszek Koltunski
108 bc0a685b Leszek Koltunski
    for(int i=0; i<=NUMLINES ; i++ )
109
      {
110 a4d59c0b Leszek Koltunski
      canvas.drawRect(texWidth*i/NUMLINES-1,                      0, texWidth*i/NUMLINES+1, texHeight             , mPaint);
111
      canvas.drawRect(                    0, texHeight*i/NUMLINES-1, texWidth             , texHeight*i/NUMLINES+1, mPaint);
112 bc0a685b Leszek Koltunski
      }
113 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bitmap);
114 af225332 Leszek Koltunski
115 fe59d375 Leszek Koltunski
    mScreen.detachAll();
116
    mScreen.attach(mTexture,mEffects,mMesh);
117
118 885b9cca leszek
    VertexEffectDistort.enable();
119
    VertexEffectSink.enable();
120
    FragmentEffectAlpha.enable();
121
    FragmentEffectChroma.enable();
122
    FragmentEffectSaturation.enable();
123 6637d0f2 Leszek Koltunski
124 bc0a685b Leszek Koltunski
    try
125
      {
126 76f9798b Leszek Koltunski
      Distorted.onCreate(mView.getContext());
127 bc0a685b Leszek Koltunski
      }
128
    catch(Exception ex)
129
      {
130 e8b6aa95 Leszek Koltunski
      android.util.Log.e("EffectQueue", ex.getMessage() );
131 bc0a685b Leszek Koltunski
      }
132
    }
133 427ab7bf Leszek Koltunski
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
137
    {
138 8dfa45c4 leszek
    mScale.set((float)width/texWidth,(float)height/texHeight,1);
139 392e16fd Leszek Koltunski
    mScreen.resize(width,height);
140 3f07bedc Leszek Koltunski
    mView.setScreenSize(width,height);
141 bc0a685b Leszek Koltunski
    }
142 427ab7bf Leszek Koltunski
   
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
   
145 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused)
146
    {   
147 fe59d375 Leszek Koltunski
    mScreen.render( System.currentTimeMillis() );
148 bc0a685b Leszek Koltunski
    }
149 af225332 Leszek Koltunski
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151 c5a28eb8 Leszek Koltunski
// the library sending messages to us. This is running on a library 'MessageSender' thread.
152 af225332 Leszek Koltunski
153 8dfa45c4 leszek
  public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
154 af225332 Leszek Koltunski
    {
155 758303a3 Leszek Koltunski
    EffectQueueActivity act = (EffectQueueActivity)mView.getContext();
156 af225332 Leszek Koltunski
157
    switch(em)
158
      {
159
      case EFFECT_REMOVED : act.effectRemoved(effectID) ; break;
160
      case EFFECT_FINISHED: act.effectFinished(effectID); break;
161
      default             : break;
162
      }
163
    }
164 bc0a685b Leszek Koltunski
  }