Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
39 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
40 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
41 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
42 af225332 Leszek Koltunski
import org.distorted.library.message.EffectListener;
43 630703d1 leszek
import org.distorted.library.type.Static3D;
44 427ab7bf Leszek Koltunski
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47 f6d884d5 Leszek Koltunski
class EffectQueueRenderer implements GLSurfaceView.Renderer, EffectListener
48 5601cfa6 Leszek Koltunski
  {
49 3f07bedc Leszek Koltunski
  private static final int NUMLINES =  10;
50 1585ba24 Leszek Koltunski
  private static final int MESH_QUALITY = 100;
51
  static final int BWID = 600;
52
  static final int BHEI = 600;
53 427ab7bf Leszek Koltunski
   
54 758303a3 Leszek Koltunski
  private EffectQueueSurfaceView mView;
55 bc0a685b Leszek Koltunski
  private Paint mPaint;
56
  private int texWidth, texHeight;
57 8dfa45c4 leszek
  private Static3D mScale;
58 3f07bedc Leszek Koltunski
59 f6d884d5 Leszek Koltunski
  private DistortedTexture mTexture;
60 f3ca895f Leszek Koltunski
  private MeshRectangles mMesh;
61 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
62 d218d64e leszek
  private DistortedScreen mScreen;
63 3f07bedc Leszek Koltunski
64 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 758303a3 Leszek Koltunski
  EffectQueueRenderer(EffectQueueSurfaceView v)
67 bc0a685b Leszek Koltunski
    {    
68
    mPaint = new Paint();
69
    mPaint.setAntiAlias(true);
70
    mPaint.setFakeBoldText(true);
71
    mPaint.setStyle(Style.FILL);
72 427ab7bf Leszek Koltunski
      
73 bc0a685b Leszek Koltunski
    mView = v;
74 427ab7bf Leszek Koltunski
      
75 bc0a685b Leszek Koltunski
    texWidth = BWID;
76
    texHeight= BHEI;
77 8dfa45c4 leszek
    mScale = new Static3D(1,1,1);
78
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
79 e8b6aa95 Leszek Koltunski
80 f3ca895f Leszek Koltunski
    mMesh = new MeshRectangles(MESH_QUALITY,MESH_QUALITY*texHeight/texWidth);
81 698ad0a8 Leszek Koltunski
82 687263cc Leszek Koltunski
    mTexture = new DistortedTexture();
83 698ad0a8 Leszek Koltunski
    mEffects = new DistortedEffects();
84 8dfa45c4 leszek
    mEffects.apply(scaleEffect);
85 392e16fd Leszek Koltunski
86 e4330c89 Leszek Koltunski
    mScreen = new DistortedScreen();
87 bc0a685b Leszek Koltunski
    }
88 427ab7bf Leszek Koltunski
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90 f6d884d5 Leszek Koltunski
91 d04a4886 Leszek Koltunski
  DistortedEffects getEffects()
92 f6d884d5 Leszek Koltunski
    {
93 392e16fd Leszek Koltunski
    return mEffects;
94 f6d884d5 Leszek Koltunski
    }
95
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
98 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
99 e8b6aa95 Leszek Koltunski
    {
100 bc0a685b Leszek Koltunski
    Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
101 e8b6aa95 Leszek Koltunski
    Canvas canvas = new Canvas(bitmap);
102
103 bc0a685b Leszek Koltunski
    mPaint.setColor(0xff008800);
104
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
105
    mPaint.setColor(0xffffffff);
106 e8b6aa95 Leszek Koltunski
107 bc0a685b Leszek Koltunski
    for(int i=0; i<=NUMLINES ; i++ )
108
      {
109 a4d59c0b Leszek Koltunski
      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 bc0a685b Leszek Koltunski
      }
112 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bitmap);
113 af225332 Leszek Koltunski
114 fe59d375 Leszek Koltunski
    mScreen.detachAll();
115
    mScreen.attach(mTexture,mEffects,mMesh);
116
117 885b9cca leszek
    VertexEffectDistort.enable();
118
    VertexEffectSink.enable();
119
    FragmentEffectAlpha.enable();
120
    FragmentEffectChroma.enable();
121
    FragmentEffectSaturation.enable();
122 6637d0f2 Leszek Koltunski
123 bc0a685b Leszek Koltunski
    try
124
      {
125 e3900503 Leszek Koltunski
      DistortedLibrary.onCreate(mView.getContext());
126 bc0a685b Leszek Koltunski
      }
127
    catch(Exception ex)
128
      {
129 e8b6aa95 Leszek Koltunski
      android.util.Log.e("EffectQueue", ex.getMessage() );
130 bc0a685b Leszek Koltunski
      }
131
    }
132 427ab7bf Leszek Koltunski
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
135 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
136
    {
137 5601cfa6 Leszek Koltunski
    // (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 392e16fd Leszek Koltunski
    mScreen.resize(width,height);
144 3f07bedc Leszek Koltunski
    mView.setScreenSize(width,height);
145 bc0a685b Leszek Koltunski
    }
146 427ab7bf Leszek Koltunski
   
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
   
149 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused)
150
    {   
151 fe59d375 Leszek Koltunski
    mScreen.render( System.currentTimeMillis() );
152 bc0a685b Leszek Koltunski
    }
153 af225332 Leszek Koltunski
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
156 8d5a8e06 Leszek Koltunski
  public void effectFinished(final long effectID)
157 af225332 Leszek Koltunski
    {
158 758303a3 Leszek Koltunski
    EffectQueueActivity act = (EffectQueueActivity)mView.getContext();
159 8d5a8e06 Leszek Koltunski
    act.effectFinished(effectID);
160 af225332 Leszek Koltunski
    }
161 bc0a685b Leszek Koltunski
  }