Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ e4330c89

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