Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 08eabc44

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