Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 513b2e9c

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