Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 5055b5d4

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.DistortedBitmap;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.type.Dynamic3D;
34
import org.distorted.library.type.Static1D;
35
import org.distorted.library.type.Static2D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38
import org.distorted.library.type.Dynamic1D;
39
import org.distorted.library.type.Dynamic2D;
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 DistortedBitmap mSuccess;
59
    private int bmpHeight, bmpWidth;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
    CheckRenderer(GLSurfaceView view)
64
      { 
65
      mView = view;
66

    
67
      CheckActivity act = (CheckActivity)mView.getContext();
68

    
69
      Distorted.setMaxVertex(act.getMaxV());
70
      Distorted.setMaxFragment(act.getMaxF());
71
      }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
   
75
    public void onDrawFrame(GL10 glUnused) 
76
      {
77
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
78
      mSuccess.draw(System.currentTimeMillis());
79
      }
80

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

    
92
        mSuccess.move( new Static3D((width-w)/2,0,0) );
93
        mSuccess.scale(factor);
94
        }
95
      else
96
        {
97
        int h = (width*bmpHeight)/bmpWidth;
98
        float factor = (float)width/bmpWidth;
99

    
100
        mSuccess.move( new Static3D(0,(height-h)/2,0) );
101
        mSuccess.scale(factor);
102
        }
103
      
104
      Distorted.onSurfaceChanged(width, height);
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
110
      {  
111
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
112

    
113
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
114
      Bitmap bitmap;
115

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

    
129
      mSuccess = new DistortedBitmap(bitmap, 30);
130

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

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

    
140
      Dynamic3D dSwirl = new Dynamic3D(2000,0.0f);
141
      dSwirl.add(new Static3D(        0, bmpHeight/2, 0));
142
      dSwirl.add(new Static3D( bmpWidth, bmpHeight/2, 0));
143

    
144
      long swirlEffectID = mSuccess.swirl( new Static1D(30), dSwirl, new Static4D( 0,0,40,40) );
145

    
146
      if( swirlEffectID<0 )
147
        {
148
        Log.e("Check", "Failed to add Swirl effect!");
149
        }
150

    
151
      Dynamic3D dDeform = new Dynamic3D(2000,0.0f);
152
      dDeform.add(new Static3D( 0,         0,0));
153
      dDeform.add(new Static3D( 0,-bmpHeight,0));
154

    
155
      long deformEffectID = mSuccess.deform(dDeform, new Static3D(bmpWidth/2,0,0) );
156

    
157
      if( deformEffectID<0 )
158
        {
159
        Log.e("Check", "Failed to add Deform effect!");
160
        }
161

    
162
      // Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
163
      Static3D color = new Static3D(1,0,0);
164
      Dynamic1D inter = new Dynamic1D(2000,0.0f);
165
      inter.add(new Static1D(0));
166
      inter.add(new Static1D(1));
167

    
168
      long chromaEffectID = mSuccess.chroma(inter, color);
169

    
170
      if( chromaEffectID<0 )
171
        {
172
        Log.e("Check", "Failed to add Chroma effect!");
173
        }
174

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

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