Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effectqueue / EffectQueueRenderer.java @ 77e66c58

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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
26
import android.content.Context;
27
import android.content.pm.ConfigurationInfo;
28
import android.content.res.Resources;
29 427ab7bf Leszek Koltunski
import android.graphics.Bitmap;
30
import android.graphics.Canvas;
31
import android.graphics.Paint;
32
import android.graphics.Paint.Style;
33
import android.opengl.GLSurfaceView;
34
35 885b9cca leszek
import org.distorted.library.effect.FragmentEffectAlpha;
36
import org.distorted.library.effect.FragmentEffectChroma;
37
import org.distorted.library.effect.FragmentEffectSaturation;
38 8dfa45c4 leszek
import org.distorted.library.effect.MatrixEffectScale;
39 885b9cca leszek
import org.distorted.library.effect.VertexEffectDistort;
40
import org.distorted.library.effect.VertexEffectSink;
41 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
42 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
43 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
44 f2085b96 Leszek Koltunski
import org.distorted.library.mesh.MeshSquare;
45 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
46 af225332 Leszek Koltunski
import org.distorted.library.message.EffectListener;
47 630703d1 leszek
import org.distorted.library.type.Static3D;
48 427ab7bf Leszek Koltunski
49 dc10a48d Leszek Koltunski
import java.io.InputStream;
50
51 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 dc10a48d Leszek Koltunski
class EffectQueueRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.LibraryUser
54 5601cfa6 Leszek Koltunski
  {
55 3f07bedc Leszek Koltunski
  private static final int NUMLINES =  10;
56 1585ba24 Leszek Koltunski
  private static final int MESH_QUALITY = 100;
57
  static final int BWID = 600;
58
  static final int BHEI = 600;
59 3f07bedc Leszek Koltunski
60 dc10a48d Leszek Koltunski
  private final EffectQueueSurfaceView mView;
61
  private final Resources mResources;
62
  private final DistortedTexture mTexture;
63
  private final MeshSquare mMesh;
64
  private final DistortedEffects mEffects;
65
  private final DistortedScreen mScreen;
66
  private final Paint mPaint;
67
  private final Static3D mScale;
68
  private final int texWidth, texHeight;
69 3f07bedc Leszek Koltunski
70 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72 758303a3 Leszek Koltunski
  EffectQueueRenderer(EffectQueueSurfaceView v)
73 dc10a48d Leszek Koltunski
    {
74
    mView = v;
75
    mResources = v.getResources();
76
77 bc0a685b Leszek Koltunski
    mPaint = new Paint();
78
    mPaint.setAntiAlias(true);
79
    mPaint.setFakeBoldText(true);
80
    mPaint.setStyle(Style.FILL);
81 dc10a48d Leszek Koltunski
82 bc0a685b Leszek Koltunski
    texWidth = BWID;
83
    texHeight= BHEI;
84 8dfa45c4 leszek
    mScale = new Static3D(1,1,1);
85
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
86 e8b6aa95 Leszek Koltunski
87 f2085b96 Leszek Koltunski
    mMesh = new MeshSquare(MESH_QUALITY,MESH_QUALITY*texHeight/texWidth);
88 698ad0a8 Leszek Koltunski
89 687263cc Leszek Koltunski
    mTexture = new DistortedTexture();
90 698ad0a8 Leszek Koltunski
    mEffects = new DistortedEffects();
91 8dfa45c4 leszek
    mEffects.apply(scaleEffect);
92 392e16fd Leszek Koltunski
93 e4330c89 Leszek Koltunski
    mScreen = new DistortedScreen();
94 bc0a685b Leszek Koltunski
    }
95 427ab7bf Leszek Koltunski
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97 f6d884d5 Leszek Koltunski
98 d04a4886 Leszek Koltunski
  DistortedEffects getEffects()
99 f6d884d5 Leszek Koltunski
    {
100 392e16fd Leszek Koltunski
    return mEffects;
101 f6d884d5 Leszek Koltunski
    }
102
103 061449ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
104
105
  public void onDrawFrame(GL10 glUnused)
106
    {
107
    mScreen.render( System.currentTimeMillis() );
108
    }
109
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
112
  public void onSurfaceChanged(GL10 glUnused, int width, int height)
113
    {
114
    // (w+h)/2 because we need to stretch more or less uniformly in all 3 directions.
115
    // If we set it to 0, Distort would not be shaded. If we set it to 1, the Distorted normal
116
    // vectors would get stretched only in X and Y dirs, becoming very 'flat' and the Distorted
117
    // area very dark.
118
    mScale.set(width, height, (width+height)*0.5f);
119
120
    mScreen.resize(width,height);
121
    mView.setScreenSize(width,height);
122
    }
123
124 f6d884d5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
127 e8b6aa95 Leszek Koltunski
    {
128 bc0a685b Leszek Koltunski
    Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
129 e8b6aa95 Leszek Koltunski
    Canvas canvas = new Canvas(bitmap);
130
131 bc0a685b Leszek Koltunski
    mPaint.setColor(0xff008800);
132
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
133
    mPaint.setColor(0xffffffff);
134 e8b6aa95 Leszek Koltunski
135 bc0a685b Leszek Koltunski
    for(int i=0; i<=NUMLINES ; i++ )
136
      {
137 a4d59c0b Leszek Koltunski
      canvas.drawRect(texWidth*i/NUMLINES-1,                      0, texWidth*i/NUMLINES+1, texHeight             , mPaint);
138
      canvas.drawRect(                    0, texHeight*i/NUMLINES-1, texWidth             , texHeight*i/NUMLINES+1, mPaint);
139 bc0a685b Leszek Koltunski
      }
140 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bitmap);
141 af225332 Leszek Koltunski
142 fe59d375 Leszek Koltunski
    mScreen.detachAll();
143
    mScreen.attach(mTexture,mEffects,mMesh);
144
145 885b9cca leszek
    VertexEffectDistort.enable();
146
    VertexEffectSink.enable();
147
    FragmentEffectAlpha.enable();
148
    FragmentEffectChroma.enable();
149
    FragmentEffectSaturation.enable();
150 6637d0f2 Leszek Koltunski
151 dc10a48d Leszek Koltunski
    DistortedLibrary.onSurfaceCreated(this);
152
    }
153
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
156
  public void effectFinished(final long effectID)
157
    {
158
    EffectQueueActivity act = (EffectQueueActivity)mView.getContext();
159
    act.effectFinished(effectID);
160 bc0a685b Leszek Koltunski
    }
161 427ab7bf Leszek Koltunski
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
164 061449ed Leszek Koltunski
  public void distortedException(Exception ex)
165 bc0a685b Leszek Koltunski
    {
166 061449ed Leszek Koltunski
    android.util.Log.e("EffectQueue", ex.getMessage() );
167 bc0a685b Leszek Koltunski
    }
168 af225332 Leszek Koltunski
169 dc10a48d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
170
171
  public InputStream localFile(int fileID)
172
    {
173
    return mResources.openRawResource(fileID);
174
    }
175
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
178
  public void logMessage(String message)
179
    {
180
    android.util.Log.e("EffectQueue", message );
181 af225332 Leszek Koltunski
    }
182 bc0a685b Leszek Koltunski
  }