Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effectqueue / EffectQueueRenderer.java @ 5601cfa6

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.effectqueue;
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.GLSurfaceView;
30

    
31
import org.distorted.library.effect.FragmentEffectAlpha;
32
import org.distorted.library.effect.FragmentEffectChroma;
33
import org.distorted.library.effect.FragmentEffectSaturation;
34
import org.distorted.library.effect.MatrixEffectScale;
35
import org.distorted.library.effect.VertexEffectDistort;
36
import org.distorted.library.effect.VertexEffectSink;
37
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedLibrary;
39
import org.distorted.library.main.DistortedScreen;
40
import org.distorted.library.mesh.MeshRectangles;
41
import org.distorted.library.main.DistortedTexture;
42
import org.distorted.library.message.EffectListener;
43
import org.distorted.library.type.Static3D;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class EffectQueueRenderer implements GLSurfaceView.Renderer, EffectListener
48
  {
49
  private static final int NUMLINES =  10;
50
  private static final int MESH_QUALITY = 100;
51
  static final int BWID = 600;
52
  static final int BHEI = 600;
53
   
54
  private EffectQueueSurfaceView mView;
55
  private Paint mPaint;
56
  private int texWidth, texHeight;
57
  private Static3D mScale;
58

    
59
  private DistortedTexture mTexture;
60
  private MeshRectangles mMesh;
61
  private DistortedEffects mEffects;
62
  private DistortedScreen mScreen;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
  EffectQueueRenderer(EffectQueueSurfaceView v)
67
    {    
68
    mPaint = new Paint();
69
    mPaint.setAntiAlias(true);
70
    mPaint.setFakeBoldText(true);
71
    mPaint.setStyle(Style.FILL);
72
      
73
    mView = v;
74
      
75
    texWidth = BWID;
76
    texHeight= BHEI;
77
    mScale = new Static3D(1,1,1);
78
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
79

    
80
    mMesh = new MeshRectangles(MESH_QUALITY,MESH_QUALITY*texHeight/texWidth);
81

    
82
    mTexture = new DistortedTexture();
83
    mEffects = new DistortedEffects();
84
    mEffects.apply(scaleEffect);
85

    
86
    mScreen = new DistortedScreen();
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  DistortedEffects getEffects()
92
    {
93
    return mEffects;
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
99
    {
100
    Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
101
    Canvas canvas = new Canvas(bitmap);
102

    
103
    mPaint.setColor(0xff008800);
104
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
105
    mPaint.setColor(0xffffffff);
106

    
107
    for(int i=0; i<=NUMLINES ; i++ )
108
      {
109
      canvas.drawRect(texWidth*i/NUMLINES-1,                      0, texWidth*i/NUMLINES+1, texHeight             , mPaint);
110
      canvas.drawRect(                    0, texHeight*i/NUMLINES-1, texWidth             , texHeight*i/NUMLINES+1, mPaint);
111
      }
112
    mTexture.setTexture(bitmap);
113

    
114
    mScreen.detachAll();
115
    mScreen.attach(mTexture,mEffects,mMesh);
116

    
117
    VertexEffectDistort.enable();
118
    VertexEffectSink.enable();
119
    FragmentEffectAlpha.enable();
120
    FragmentEffectChroma.enable();
121
    FragmentEffectSaturation.enable();
122

    
123
    try
124
      {
125
      DistortedLibrary.onCreate(mView.getContext());
126
      }
127
    catch(Exception ex)
128
      {
129
      android.util.Log.e("EffectQueue", ex.getMessage() );
130
      }
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
136
    {
137
    // (w+h)/2 because we need to stretch more or less uniformly in all 3 directions.
138
    // If we set it to 0, Distort would not be shaded. If we set it to 1, the Distorted normal
139
    // vectors would get stretched only in X and Y dirs, becoming very 'flat' and the Distorted
140
    // area very dark.
141
    mScale.set(width, height, (width+height)*0.5f);
142

    
143
    mScreen.resize(width,height);
144
    mView.setScreenSize(width,height);
145
    }
146
   
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
   
149
  public void onDrawFrame(GL10 glUnused)
150
    {   
151
    mScreen.render( System.currentTimeMillis() );
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  public void effectFinished(final long effectID)
157
    {
158
    EffectQueueActivity act = (EffectQueueActivity)mView.getContext();
159
    act.effectFinished(effectID);
160
    }
161
  }
(2-2/3)