Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ d218d64e

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.DistortedFramebuffer;
33
import org.distorted.library.DistortedScreen;
34
import org.distorted.library.DistortedTexture;
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 DistortedTexture mTexture;
61
    private DistortedEffects mEffects;
62
    private MeshFlat mMesh;
63
    private DistortedScreen mScreen;
64
    private int bmpHeight, bmpWidth;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

    
68
    CheckRenderer(GLSurfaceView view)
69
      { 
70
      mView = view;
71
      mEffects = new DistortedEffects();
72

    
73
      CheckActivity act = (CheckActivity)mView.getContext();
74

    
75
      DistortedEffects.setMaxVertex(act.getMaxV());
76
      DistortedEffects.setMaxFragment(act.getMaxF());
77

    
78
      mScreen = new DistortedScreen();
79
      }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
   
83
    public void onDrawFrame(GL10 glUnused) 
84
      {
85
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
86
      mScreen.renderTo(mTexture, mMesh, mEffects, System.currentTimeMillis() );
87
      }
88

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

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

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

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116
    
117
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
118
      {  
119
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
120

    
121
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
122
      Bitmap bitmap;
123

    
124
      try
125
        {
126
        bitmap = BitmapFactory.decodeStream(is);
127
        } 
128
      finally 
129
        {
130
        try 
131
          {
132
          is.close();
133
          } 
134
        catch(IOException e) { }
135
        }
136

    
137
      bmpHeight = bitmap.getHeight();
138
      bmpWidth  = bitmap.getWidth();
139

    
140
      mTexture = new DistortedTexture(bmpWidth,bmpHeight);
141
      mTexture.setTexture(bitmap);
142
      mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
143

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

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

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

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

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

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

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

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

    
178
      long chromaEffectID = mEffects.chroma(inter, color);
179

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

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

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