Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effectqueue / EffectQueueRenderer.java @ 625c67de

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.app.ActivityManager;
26
import android.content.Context;
27
import android.content.pm.ConfigurationInfo;
28
import android.content.res.Resources;
29
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
import org.distorted.library.effect.FragmentEffectAlpha;
36
import org.distorted.library.effect.FragmentEffectChroma;
37
import org.distorted.library.effect.FragmentEffectSaturation;
38
import org.distorted.library.effect.MatrixEffectScale;
39
import org.distorted.library.effect.VertexEffectDistort;
40
import org.distorted.library.effect.VertexEffectSink;
41
import org.distorted.library.main.DistortedEffects;
42
import org.distorted.library.main.DistortedLibrary;
43
import org.distorted.library.main.DistortedScreen;
44
import org.distorted.library.mesh.MeshSquare;
45
import org.distorted.library.main.DistortedTexture;
46
import org.distorted.library.message.EffectListener;
47
import org.distorted.library.type.Static3D;
48

    
49
import java.io.InputStream;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
class EffectQueueRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.LibraryUser
54
  {
55
  private static final int NUMLINES =  10;
56
  private static final int MESH_QUALITY = 100;
57
  static final int BWID = 600;
58
  static final int BHEI = 600;
59

    
60
  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

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  EffectQueueRenderer(EffectQueueSurfaceView v)
73
    {
74
    mView = v;
75
    mResources = v.getResources();
76

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

    
82
    texWidth = BWID;
83
    texHeight= BHEI;
84
    mScale = new Static3D(1,1,1);
85
    MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
86

    
87
    mMesh = new MeshSquare(MESH_QUALITY,MESH_QUALITY*texHeight/texWidth);
88

    
89
    mTexture = new DistortedTexture();
90
    mEffects = new DistortedEffects();
91
    mEffects.apply(scaleEffect);
92

    
93
    mScreen = new DistortedScreen();
94
    }
95

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

    
98
  DistortedEffects getEffects()
99
    {
100
    return mEffects;
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
127
    {
128
    Bitmap bitmap = Bitmap.createBitmap(texWidth,texHeight, Bitmap.Config.ARGB_8888);
129
    Canvas canvas = new Canvas(bitmap);
130

    
131
    mPaint.setColor(0xff008800);
132
    canvas.drawRect(0, 0, texWidth, texHeight, mPaint);
133
    mPaint.setColor(0xffffffff);
134

    
135
    for(int i=0; i<=NUMLINES ; i++ )
136
      {
137
      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
      }
140
    mTexture.setTexture(bitmap);
141

    
142
    mScreen.detachAll();
143
    mScreen.attach(mTexture,mEffects,mMesh);
144

    
145
    VertexEffectDistort.enable();
146
    VertexEffectSink.enable();
147
    FragmentEffectAlpha.enable();
148
    FragmentEffectChroma.enable();
149
    FragmentEffectSaturation.enable();
150

    
151
    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
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
  public void distortedException(Exception ex)
165
    {
166
    android.util.Log.e("EffectQueue", ex.getMessage() );
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
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
    }
182
  }
(2-2/3)