Project

General

Profile

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

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

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.check;
21

    
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
import org.distorted.examples.R;
29

    
30
import org.distorted.library.Distorted;
31
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.DistortedEffectQueues;
33
import org.distorted.library.GridFlat;
34
import org.distorted.library.EffectTypes;
35
import org.distorted.library.type.Dynamic3D;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39
import org.distorted.library.type.Dynamic1D;
40

    
41
import android.app.AlertDialog;
42
import android.content.Context;
43
import android.content.DialogInterface;
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46
import android.opengl.GLES20;
47
import android.opengl.GLSurfaceView;
48
import android.util.Log;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class CheckRenderer implements GLSurfaceView.Renderer 
53
{
54
    private static String compilationResult;
55
    private static String compilationTitle;
56

    
57
    private GLSurfaceView mView;
58
    private DistortedTexture mTexture;
59
    private DistortedEffectQueues mQueues;
60
    private GridFlat mGrid;
61
    private int bmpHeight, bmpWidth;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
    CheckRenderer(GLSurfaceView view)
66
      { 
67
      mView = view;
68
      mQueues = new DistortedEffectQueues();
69

    
70
      CheckActivity act = (CheckActivity)mView.getContext();
71

    
72
      Distorted.setMaxVertex(act.getMaxV());
73
      Distorted.setMaxFragment(act.getMaxF());
74
      }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
   
78
    public void onDrawFrame(GL10 glUnused) 
79
      {
80
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
81
      mQueues.draw(System.currentTimeMillis(), mTexture, mGrid);
82
      }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
    
86
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
87
      { 
88
      mQueues.abortEffects(EffectTypes.MATRIX);
89
      
90
      if( (float)bmpHeight/bmpWidth > (float)height/width )
91
        {
92
        int w = (height*bmpWidth)/bmpHeight;
93
        float factor = (float)height/bmpHeight;
94

    
95
        mQueues.move( new Static3D((width-w)/2,0,0) );
96
        mQueues.scale(factor);
97
        }
98
      else
99
        {
100
        int h = (width*bmpHeight)/bmpWidth;
101
        float factor = (float)width/bmpWidth;
102

    
103
        mQueues.move( new Static3D(0,(height-h)/2,0) );
104
        mQueues.scale(factor);
105
        }
106
      
107
      Distorted.onSurfaceChanged(width, height);
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
113
      {  
114
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
115

    
116
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
117
      Bitmap bitmap;
118

    
119
      try
120
        {
121
        bitmap = BitmapFactory.decodeStream(is);
122
        } 
123
      finally 
124
        {
125
        try 
126
          {
127
          is.close();
128
          } 
129
        catch(IOException e) { }
130
        }
131

    
132
      bmpHeight = bitmap.getHeight();
133
      bmpWidth  = bitmap.getWidth();
134

    
135
      mTexture = new DistortedTexture(bmpWidth,bmpHeight,0);
136
      mTexture.setTexture(bitmap);
137
      mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
138

    
139
      // Try adding 2 Vertex Effects to the Bitmap.
140
      // This will fail if we have set maxVertexEffects to something < 2.
141
      //
142
      // Even if adding some of the Effects fails, the App will still start - you just won't see
143
      // the effects that failed to add.
144

    
145
      Dynamic3D dSwirl = new Dynamic3D(2000,0.0f);
146
      dSwirl.add(new Static3D(        0, bmpHeight/2, 0));
147
      dSwirl.add(new Static3D( bmpWidth, bmpHeight/2, 0));
148

    
149
      long swirlEffectID = mQueues.swirl( new Static1D(30), dSwirl, new Static4D( 0,0,40,40) );
150

    
151
      if( swirlEffectID<0 )
152
        {
153
        Log.e("Check", "Failed to add Swirl effect!");
154
        }
155

    
156
      Dynamic3D dDeform = new Dynamic3D(2000,0.0f);
157
      dDeform.add(new Static3D( 0,         0,0));
158
      dDeform.add(new Static3D( 0,-bmpHeight,0));
159

    
160
      long deformEffectID = mQueues.deform(dDeform, new Static3D(bmpWidth/2,0,0) );
161

    
162
      if( deformEffectID<0 )
163
        {
164
        Log.e("Check", "Failed to add Deform effect!");
165
        }
166

    
167
      // Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
168
      Static3D color = new Static3D(1,0,0);
169
      Dynamic1D inter = new Dynamic1D(2000,0.0f);
170
      inter.add(new Static1D(0));
171
      inter.add(new Static1D(1));
172

    
173
      long chromaEffectID = mQueues.chroma(inter, color);
174

    
175
      if( chromaEffectID<0 )
176
        {
177
        Log.e("Check", "Failed to add Chroma effect!");
178
        }
179

    
180
      try
181
        {
182
        Distorted.onSurfaceCreated(mView.getContext());
183
        }
184
      catch(Exception ex)
185
        {
186
        // We have failed to compile or link shaders - probably too many effects.
187
        // (each effect adds a certain number of Uniforms to the shader and there
188
        // can only be a limited number of those )
189

    
190
        compilationTitle = ex.getClass().getSimpleName();
191
        compilationResult= ex.getMessage();
192
      
193
        String ver;
194
      
195
        ver = GLES20.glGetString(GLES20.GL_SHADING_LANGUAGE_VERSION);   
196
        compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
197
        ver = GLES20.glGetString(GLES20.GL_VERSION);  
198
        compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
199
        ver = GLES20.glGetString(GLES20.GL_VENDOR);   
200
        compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
201
        ver = GLES20.glGetString(GLES20.GL_RENDERER);    
202
        compilationResult += "\nGL renderer: "+(ver==null ? "null" : ver);
203
      
204
        CheckActivity act = (CheckActivity)mView.getContext();
205
      
206
        act.runOnUiThread(new Runnable() 
207
          {
208
          public void run() 
209
            {
210
            Context context = mView.getContext();
211
      
212
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
213
            builder.setTitle(compilationTitle);
214
            builder.setMessage(compilationResult);
215
            builder.setCancelable(true);
216
           
217
            builder.setOnCancelListener(new DialogInterface.OnCancelListener() 
218
              {
219
              @Override
220
              public void onCancel(DialogInterface dialog) 
221
                {
222
                ((CheckActivity)mView.getContext()).finish();
223
                }
224
              });
225
            
226
            AlertDialog alert = builder.create();
227
            alert.show();
228
            }
229
          });
230
        }
231
      }
232
    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
    
235
}
(2-2/3)