Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 061449ed

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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.check;
21 427ab7bf Leszek Koltunski
22
import java.io.IOException;
23
import java.io.InputStream;
24
25
import javax.microedition.khronos.egl.EGLConfig;
26
import javax.microedition.khronos.opengles.GL10;
27
28 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 d76638f1 leszek
import org.distorted.library.effect.EffectType;
31 37b324c4 Leszek Koltunski
import org.distorted.library.effect.FragmentEffectChroma;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectDeform;
34 9930cd43 Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
35 37b324c4 Leszek Koltunski
import org.distorted.library.effect.VertexEffectSwirl;
36 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
37 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.DistortedTexture;
40 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
41 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
42 7589635e Leszek Koltunski
import org.distorted.library.type.Static1D;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45
import org.distorted.library.type.Dynamic1D;
46 427ab7bf Leszek Koltunski
47
import android.app.AlertDialog;
48
import android.content.Context;
49
import android.content.DialogInterface;
50
import android.graphics.Bitmap;
51
import android.graphics.BitmapFactory;
52 41a85bb7 Leszek Koltunski
import android.opengl.GLES31;
53 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
54 b4a73ea5 Leszek Koltunski
import android.util.Log;
55 427ab7bf Leszek Koltunski
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 061449ed Leszek Koltunski
class CheckRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
59 427ab7bf Leszek Koltunski
{
60 9b9440a0 Leszek Koltunski
    private static String compilationResult;
61
    private static String compilationTitle;
62 b4a73ea5 Leszek Koltunski
63 427ab7bf Leszek Koltunski
    private GLSurfaceView mView;
64 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
65 b0ebdf5e Leszek Koltunski
    private DistortedTexture mTexture;
66 f3ca895f Leszek Koltunski
    private MeshRectangles mMesh;
67 d218d64e leszek
    private DistortedScreen mScreen;
68 37b324c4 Leszek Koltunski
    private int mObjHeight, mObjWidth;
69 9930cd43 Leszek Koltunski
    private Static3D mScaleMatrix, mScaleVertex, mSwirl1, mSwirl2, mDeform1, mDeform2;
70 513b2e9c Leszek Koltunski
    private Static1D mRadius;
71 9c5b5cb6 Leszek Koltunski
72 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 9b9440a0 Leszek Koltunski
    CheckRenderer(GLSurfaceView view)
75 427ab7bf Leszek Koltunski
      { 
76
      mView = view;
77 9c5b5cb6 Leszek Koltunski
78 9b9440a0 Leszek Koltunski
      CheckActivity act = (CheckActivity)mView.getContext();
79
80 e3900503 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX  ,act.getMaxV());
81
      DistortedLibrary.setMax(EffectType.FRAGMENT,act.getMaxF());
82 676c14da Leszek Koltunski
83 9930cd43 Leszek Koltunski
      VertexEffectScale.enable();
84 885b9cca leszek
      VertexEffectSwirl.enable();
85
      VertexEffectDeform.enable();
86
      FragmentEffectChroma.enable();
87 37b324c4 Leszek Koltunski
88 9930cd43 Leszek Koltunski
      mSwirl1     = new Static3D(0,0,0);
89
      mSwirl2     = new Static3D(0,0,0);
90
      mDeform1    = new Static3D(0,0,0);
91
      mDeform2    = new Static3D(0,0,0);
92
      mScaleMatrix= new Static3D(1,1,1);
93
      mScaleVertex= new Static3D(1,1,1);
94 37b324c4 Leszek Koltunski
95 513b2e9c Leszek Koltunski
      mRadius = new Static1D(0);
96
97 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
98 9930cd43 Leszek Koltunski
      mEffects.apply(new MatrixEffectScale(mScaleMatrix));
99 37b324c4 Leszek Koltunski
100 9930cd43 Leszek Koltunski
      // Try adding 3 Vertex Effects to the Bitmap.
101
      // This will fail if we have set maxVertexEffects to something < 3.
102 37b324c4 Leszek Koltunski
      //
103
      // Even if adding some of the Effects fails, the App will still start - you just won't see
104
      // the effects that failed to add.
105
106 9930cd43 Leszek Koltunski
      if( !mEffects.apply( new VertexEffectScale(mScaleVertex) ) )
107
        {
108
        Log.e("Check", "Failed to add Scale effect!");
109
        }
110
111 37b324c4 Leszek Koltunski
      Dynamic3D dSwirl = new Dynamic3D(2000,0.0f);
112
      dSwirl.add(mSwirl1);
113
      dSwirl.add(mSwirl2);
114
115 9e7b6dbd Leszek Koltunski
      if( !mEffects.apply( new VertexEffectSwirl(new Static1D(30), dSwirl, new Static4D( 0,0,0,40)) ) )
116 37b324c4 Leszek Koltunski
        {
117
        Log.e("Check", "Failed to add Swirl effect!");
118
        }
119
120
      Dynamic3D dDeform = new Dynamic3D(2000,0.0f);
121
      dDeform.add(mDeform1);
122
      dDeform.add(mDeform2);
123
124 513b2e9c Leszek Koltunski
      if( !mEffects.apply( new VertexEffectDeform(dDeform, mRadius, new Static3D(0,0,0)) ) )
125 37b324c4 Leszek Koltunski
        {
126
        Log.e("Check", "Failed to add Deform effect!");
127
        }
128
129
      // Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
130
      Static3D color = new Static3D(1,0,0);
131
      Dynamic1D inter = new Dynamic1D(2000,0.0f);
132
      inter.add(new Static1D(0));
133
      inter.add(new Static1D(1));
134
135
      if( !mEffects.apply( new FragmentEffectChroma(inter, color) ) )
136
        {
137
        Log.e("Check", "Failed to add Chroma effect!");
138
        }
139 392e16fd Leszek Koltunski
140 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
141 427ab7bf Leszek Koltunski
      }
142
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
   
145
    public void onDrawFrame(GL10 glUnused) 
146
      {
147 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
148 427ab7bf Leszek Koltunski
      }
149
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
    
152
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
153 a4d59c0b Leszek Koltunski
      {
154
      final float SCALE = 0.9f;
155
156
      float xW = (float)width /mObjWidth;
157
      float xH = (float)height/mObjHeight;
158
159
      float factor = xW>xH ? SCALE*xH : SCALE*xW;
160
161 9930cd43 Leszek Koltunski
      mScaleMatrix.set(factor,factor,factor);
162 37b324c4 Leszek Koltunski
163
      mScreen.resize(width,height);
164 427ab7bf Leszek Koltunski
      }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
    
168
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
169
      {  
170
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
171
      Bitmap bitmap;
172 9c5b5cb6 Leszek Koltunski
173
      try
174 427ab7bf Leszek Koltunski
        {
175
        bitmap = BitmapFactory.decodeStream(is);
176
        } 
177
      finally 
178
        {
179
        try 
180
          {
181
          is.close();
182
          } 
183
        catch(IOException e) { }
184 9c5b5cb6 Leszek Koltunski
        }
185
186 37b324c4 Leszek Koltunski
      mObjHeight = bitmap.getHeight();
187
      mObjWidth  = bitmap.getWidth();
188 9c5b5cb6 Leszek Koltunski
189 9930cd43 Leszek Koltunski
      mRadius.set(mObjWidth*0.5f);
190
      mScaleVertex.set(mObjWidth,mObjHeight,0);
191 513b2e9c Leszek Koltunski
192 9930cd43 Leszek Koltunski
      if( mMesh   ==null ) mMesh = new MeshRectangles(30,30*mObjHeight/mObjWidth);
193 687263cc Leszek Koltunski
      if( mTexture==null ) mTexture = new DistortedTexture();
194 b0ebdf5e Leszek Koltunski
      mTexture.setTexture(bitmap);
195 fe59d375 Leszek Koltunski
196
      mScreen.detachAll();
197 5f2a53b6 leszek
      mScreen.attach(mTexture,mEffects,mMesh);
198 b0ebdf5e Leszek Koltunski
199 8eed34d3 Leszek Koltunski
      mSwirl1.set (-mObjWidth/2,          0, 0);
200
      mSwirl2.set ( mObjWidth/2,          0, 0);
201
      mDeform1.set(           0,          0, 0);
202
      mDeform2.set(           0, mObjHeight, 0);
203 6637d0f2 Leszek Koltunski
204 061449ed Leszek Koltunski
      DistortedLibrary.onCreate(mView.getContext(),this);
205
      }
206
    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
// We have failed to compile or link shaders - probably too many effects.
209
// (each effect adds a certain number of Uniforms to the shader and there
210
// can only be a limited number of those )
211
212
    public void distortedException(Exception ex)
213
      {
214
      compilationTitle = ex.getClass().getSimpleName();
215
      compilationResult= ex.getMessage();
216
217
      String ver;
218
219
      ver = GLES31.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION);
220
      compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
221
      ver = GLES31.glGetString(GLES31.GL_VERSION);
222
      compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
223
      ver = GLES31.glGetString(GLES31.GL_VENDOR);
224
      compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
225
      ver = GLES31.glGetString(GLES31.GL_RENDERER);
226
      compilationResult += "\nGL renderer: "+(ver==null ? "null" : ver);
227
228
      CheckActivity act = (CheckActivity)mView.getContext();
229
230
      act.runOnUiThread(new Runnable()
231 427ab7bf Leszek Koltunski
        {
232 061449ed Leszek Koltunski
        public void run()
233 427ab7bf Leszek Koltunski
          {
234 061449ed Leszek Koltunski
          Context context = mView.getContext();
235
236
          AlertDialog.Builder builder = new AlertDialog.Builder(context);
237
          builder.setTitle(compilationTitle);
238
          builder.setMessage(compilationResult);
239
          builder.setCancelable(true);
240
241
          builder.setOnCancelListener(new DialogInterface.OnCancelListener()
242 427ab7bf Leszek Koltunski
            {
243 061449ed Leszek Koltunski
            @Override
244
            public void onCancel(DialogInterface dialog)
245 427ab7bf Leszek Koltunski
              {
246 061449ed Leszek Koltunski
              ((CheckActivity)mView.getContext()).finish();
247
              }
248
            });
249
250
          AlertDialog alert = builder.create();
251
          alert.show();
252
          }
253
        });
254 427ab7bf Leszek Koltunski
      }
255
}