Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 855ba24e

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.DistortedEffects;
32
import org.distorted.library.DistortedScreen;
33
import org.distorted.library.DistortedTexture;
34
import org.distorted.library.EffectNames;
35
import org.distorted.library.MeshFlat;
36
import org.distorted.library.EffectTypes;
37
import org.distorted.library.type.Dynamic3D;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41
import org.distorted.library.type.Dynamic1D;
42

    
43
import android.app.AlertDialog;
44
import android.content.Context;
45
import android.content.DialogInterface;
46
import android.graphics.Bitmap;
47
import android.graphics.BitmapFactory;
48
import android.opengl.GLES30;
49
import android.opengl.GLSurfaceView;
50
import android.util.Log;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

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

    
59
    private GLSurfaceView mView;
60
    private DistortedEffects mEffects;
61
    private DistortedScreen mScreen;
62
    private int bmpHeight, bmpWidth;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
    CheckRenderer(GLSurfaceView view)
67
      { 
68
      mView = view;
69
      mEffects = new DistortedEffects();
70

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

    
73
      DistortedEffects.setMaxVertex(act.getMaxV());
74
      DistortedEffects.setMaxFragment(act.getMaxF());
75

    
76
      mScreen = new DistortedScreen();
77
      }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
   
81
    public void onDrawFrame(GL10 glUnused) 
82
      {
83
      mScreen.render( System.currentTimeMillis() );
84
      }
85

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

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

    
105
        mEffects.move( new Static3D(0,(height-h)/2,0) );
106
        mEffects.scale(factor);
107
        }
108
      
109
      mScreen.resize(width, height);
110
      }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
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
      DistortedTexture texture = new DistortedTexture(bmpWidth,bmpHeight);
136
      texture.setTexture(bitmap);
137
      MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
138

    
139
      mScreen.detachAll();
140
      mScreen.attach(texture,mEffects,mesh);
141

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

    
148
      Dynamic3D dSwirl = new Dynamic3D(2000,0.0f);
149
      dSwirl.add(new Static3D(        0, bmpHeight/2, 0));
150
      dSwirl.add(new Static3D( bmpWidth, bmpHeight/2, 0));
151

    
152
      long swirlEffectID = mEffects.swirl( new Static1D(30), dSwirl, new Static4D( 0,0,40,40) );
153

    
154
      if( swirlEffectID<0 )
155
        {
156
        Log.e("Check", "Failed to add Swirl effect!");
157
        }
158

    
159
      Dynamic3D dDeform = new Dynamic3D(2000,0.0f);
160
      dDeform.add(new Static3D( 0,         0,0));
161
      dDeform.add(new Static3D( 0,-bmpHeight,0));
162

    
163
      long deformEffectID = mEffects.deform(dDeform, new Static3D(bmpWidth/2,0,0) );
164

    
165
      if( deformEffectID<0 )
166
        {
167
        Log.e("Check", "Failed to add Deform effect!");
168
        }
169

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

    
176
      long chromaEffectID = mEffects.chroma(inter, color);
177

    
178
      if( chromaEffectID<0 )
179
        {
180
        Log.e("Check", "Failed to add Chroma effect!");
181
        }
182

    
183
      DistortedEffects.enableEffect(EffectNames.SWIRL);
184
      DistortedEffects.enableEffect(EffectNames.DEFORM);
185
      DistortedEffects.enableEffect(EffectNames.CHROMA);
186

    
187
      try
188
        {
189
        Distorted.onCreate(mView.getContext());
190
        }
191
      catch(Exception ex)
192
        {
193
        // We have failed to compile or link shaders - probably too many effects.
194
        // (each effect adds a certain number of Uniforms to the shader and there
195
        // can only be a limited number of those )
196

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