Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 9c5b5cb6

1 427ab7bf Leszek Koltunski
2 5068fa06 Leszek Koltunski
package org.distorted.examples.check;
3 427ab7bf Leszek Koltunski
4
import java.io.IOException;
5
import java.io.InputStream;
6
7
import javax.microedition.khronos.egl.EGLConfig;
8
import javax.microedition.khronos.opengles.GL10;
9
10 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
11 427ab7bf Leszek Koltunski
12 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
13
import org.distorted.library.DistortedBitmap;
14 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
15 9c5b5cb6 Leszek Koltunski
import org.distorted.library.Float1D;
16 5068fa06 Leszek Koltunski
import org.distorted.library.Float2D;
17
import org.distorted.library.Float3D;
18 9c5b5cb6 Leszek Koltunski
import org.distorted.library.Float4D;
19
import org.distorted.library.Interpolator1D;
20
import org.distorted.library.Interpolator2D;
21 427ab7bf Leszek Koltunski
22
import android.app.AlertDialog;
23
import android.content.Context;
24
import android.content.DialogInterface;
25
import android.graphics.Bitmap;
26
import android.graphics.BitmapFactory;
27
import android.opengl.GLES20;
28
import android.opengl.GLSurfaceView;
29
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32
class CheckRenderer implements GLSurfaceView.Renderer 
33
{
34
    private GLSurfaceView mView;
35
    private DistortedBitmap mSuccess;
36
    private int bmpHeight, bmpWidth;
37 9c5b5cb6 Leszek Koltunski
38 427ab7bf Leszek Koltunski
    public static String compilationResult;
39
    public static String compilationTitle;
40 9c5b5cb6 Leszek Koltunski
41 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42
43
    public CheckRenderer(GLSurfaceView view) 
44
      { 
45
      mView = view;
46 9c5b5cb6 Leszek Koltunski
47 427ab7bf Leszek Koltunski
      Distorted.setMaxVertex(CheckActivity.getMaxV());
48
      Distorted.setMaxFragment(CheckActivity.getMaxF());
49
      }
50
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
   
53
    public void onDrawFrame(GL10 glUnused) 
54
      {
55
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
56
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
57
    
58
      mSuccess.draw(System.currentTimeMillis());
59
      }
60
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
    
63
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
64
      { 
65 a8c3ada7 Leszek Koltunski
      mSuccess.abortEffects(EffectTypes.MATRIX);
66 427ab7bf Leszek Koltunski
      
67
      if( bmpHeight/bmpWidth > height/width )
68
        {
69
        int w = (height*bmpWidth)/bmpHeight;
70
        mSuccess.move((width-w)/2 ,0, 0);
71
        mSuccess.scale((float)height/bmpHeight);
72
        }
73
      else
74
        {
75
        int h = (width*bmpHeight)/bmpWidth;
76
        mSuccess.move(0 ,(height-h)/2, 0);
77
        mSuccess.scale((float)width/bmpWidth);
78
        }
79
      
80
      Distorted.onSurfaceChanged(width, height);
81
      }
82
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
    
85
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
86
      {  
87
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
88
      Bitmap bitmap;
89 9c5b5cb6 Leszek Koltunski
90
      try
91 427ab7bf Leszek Koltunski
        {
92
        bitmap = BitmapFactory.decodeStream(is);
93
        } 
94
      finally 
95
        {
96
        try 
97
          {
98
          is.close();
99
          } 
100
        catch(IOException e) { }
101 9c5b5cb6 Leszek Koltunski
        }
102
103
      mSuccess = new DistortedBitmap(bitmap, 30);
104
105 427ab7bf Leszek Koltunski
      bmpHeight = bitmap.getHeight();
106
      bmpWidth  = bitmap.getWidth();
107 9c5b5cb6 Leszek Koltunski
108
      // Try adding 2 Vertex Effects to the Bitmap.
109
      // This will fail if we have set maxVertexEffects to something < 2.
110
      //
111
      // Even if adding some of the Effects fails, the App will still start - you just won't see
112
      // the effects that failed to add.
113
      Float2D pDown   = new Float2D(bmpWidth/2,   0);
114
      Float3D vDown   = new Float3D( 0,-bmpHeight,0);
115
      Float4D mRegion = new Float4D( 0, 0, 40 ,40 );
116
117
      Interpolator2D mPoint = new Interpolator2D();
118
      mPoint.setCount(0.0f);
119
      mPoint.setDuration(2000);
120
      mPoint.add(new Float2D(        0, bmpHeight/2));
121
      mPoint.add(new Float2D( bmpWidth, bmpHeight/2));
122
123
      mSuccess.swirl( 30, mRegion, mPoint );
124 427ab7bf Leszek Koltunski
      mSuccess.deform(vDown, pDown, 2000, 0.0f);
125 9c5b5cb6 Leszek Koltunski
126
      // Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
127
      Float3D color = new Float3D(1,0,0);
128
      Interpolator1D inter = new Interpolator1D();
129
      inter.setCount(0.0f);
130
      inter.setDuration(2000);
131
      inter.add(new Float1D(0));
132
      inter.add(new Float1D(1));
133
134
      mSuccess.chroma(inter, color, null, new Float2D(0,0) );
135
136 427ab7bf Leszek Koltunski
      try
137
        {
138 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
139 427ab7bf Leszek Koltunski
        }
140
      catch(Exception ex)
141
        {
142 9c5b5cb6 Leszek Koltunski
        // We have failed to compile or link shaders - probably too many effects.
143
        // (each effect adds a certain number of Uniforms to the shader and there
144
        // can only be a limited number of those )
145
146 427ab7bf Leszek Koltunski
        compilationTitle = ex.getClass().getSimpleName();
147
        compilationResult= ex.getMessage();
148
      
149
        String ver;
150
      
151
        ver = GLES20.glGetString(GLES20.GL_SHADING_LANGUAGE_VERSION);   
152
        compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
153
        ver = GLES20.glGetString(GLES20.GL_VERSION);  
154
        compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
155
        ver = GLES20.glGetString(GLES20.GL_VENDOR);   
156
        compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
157
        ver = GLES20.glGetString(GLES20.GL_RENDERER);    
158
        compilationResult += "\nGL renderer: "+(ver==null ? "null" : ver);
159
      
160
        CheckActivity act = (CheckActivity)mView.getContext();
161
      
162
        act.runOnUiThread(new Runnable() 
163
          {
164
          public void run() 
165
            {
166
            Context context = mView.getContext();
167
      
168
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
169
            builder.setTitle(compilationTitle);
170
            builder.setMessage(compilationResult);
171
            builder.setCancelable(true);
172
           
173
            builder.setOnCancelListener(new DialogInterface.OnCancelListener() 
174
              {
175
              @Override
176
              public void onCancel(DialogInterface dialog) 
177
                {
178
                ((CheckActivity)mView.getContext()).finish();
179
                }
180
              });
181
            
182
            AlertDialog alert = builder.create();
183
            alert.show();
184
            }
185
          });
186
        }
187
      }
188
    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
    
191
}