Project

General

Profile

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

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

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 final String TAG = "CheckRenderer";
55
    private static String compilationResult;
56
    private static String compilationTitle;
57

    
58
    private GLSurfaceView mView;
59
    private DistortedBitmap mSuccess;
60
    private int bmpHeight, bmpWidth;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

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

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

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
   
76
    public void onDrawFrame(GL10 glUnused) 
77
      {
78
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
79
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
80
    
81
      mSuccess.draw(System.currentTimeMillis());
82
      }
83

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

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

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
113
      {  
114
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
115
      Bitmap bitmap;
116

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

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

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

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

    
141
      Dynamic2D dSwirl = new Dynamic2D(2000,0.0f);
142
      dSwirl.add(new Static2D(        0, bmpHeight/2));
143
      dSwirl.add(new Static2D( bmpWidth, bmpHeight/2));
144

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

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

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

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

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

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

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

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

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

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