Project

General

Profile

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

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

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 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
32 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
33 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
34 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
35 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
36 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
37 7589635e Leszek Koltunski
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40
import org.distorted.library.type.Dynamic1D;
41 427ab7bf Leszek Koltunski
42
import android.app.AlertDialog;
43
import android.content.Context;
44
import android.content.DialogInterface;
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLES20;
48
import android.opengl.GLSurfaceView;
49 b4a73ea5 Leszek Koltunski
import android.util.Log;
50 427ab7bf Leszek Koltunski
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53
class CheckRenderer implements GLSurfaceView.Renderer 
54
{
55 9b9440a0 Leszek Koltunski
    private static String compilationResult;
56
    private static String compilationTitle;
57 b4a73ea5 Leszek Koltunski
58 427ab7bf Leszek Koltunski
    private GLSurfaceView mView;
59 f6d884d5 Leszek Koltunski
    private DistortedTexture mTexture;
60 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
61 10b7e588 Leszek Koltunski
    private GridFlat mGrid;
62 392e16fd Leszek Koltunski
    private DistortedFramebuffer mScreen;
63 427ab7bf Leszek Koltunski
    private int bmpHeight, bmpWidth;
64 9c5b5cb6 Leszek Koltunski
65 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67 9b9440a0 Leszek Koltunski
    CheckRenderer(GLSurfaceView view)
68 427ab7bf Leszek Koltunski
      { 
69
      mView = view;
70 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
71 9c5b5cb6 Leszek Koltunski
72 9b9440a0 Leszek Koltunski
      CheckActivity act = (CheckActivity)mView.getContext();
73
74
      Distorted.setMaxVertex(act.getMaxV());
75
      Distorted.setMaxFragment(act.getMaxF());
76 392e16fd Leszek Koltunski
77
      mScreen = new DistortedFramebuffer(0);
78 427ab7bf Leszek Koltunski
      }
79
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
   
82
    public void onDrawFrame(GL10 glUnused) 
83
      {
84
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
85 bc29e409 Leszek Koltunski
      mScreen.renderTo(mTexture, mGrid, mEffects, System.currentTimeMillis() );
86 427ab7bf Leszek Koltunski
      }
87
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
    
90
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
91
      { 
92 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
93 427ab7bf Leszek Koltunski
      
94 5055b5d4 Leszek Koltunski
      if( (float)bmpHeight/bmpWidth > (float)height/width )
95 427ab7bf Leszek Koltunski
        {
96
        int w = (height*bmpWidth)/bmpHeight;
97 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
98
99 392e16fd Leszek Koltunski
        mEffects.move( new Static3D((width-w)/2,0,0) );
100
        mEffects.scale(factor);
101 427ab7bf Leszek Koltunski
        }
102
      else
103
        {
104
        int h = (width*bmpHeight)/bmpWidth;
105 7589635e Leszek Koltunski
        float factor = (float)width/bmpWidth;
106
107 392e16fd Leszek Koltunski
        mEffects.move( new Static3D(0,(height-h)/2,0) );
108
        mEffects.scale(factor);
109 427ab7bf Leszek Koltunski
        }
110
      
111 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
112 427ab7bf Leszek Koltunski
      }
113
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
    
116
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
117
      {  
118 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
119
120 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
121
      Bitmap bitmap;
122 9c5b5cb6 Leszek Koltunski
123
      try
124 427ab7bf Leszek Koltunski
        {
125
        bitmap = BitmapFactory.decodeStream(is);
126
        } 
127
      finally 
128
        {
129
        try 
130
          {
131
          is.close();
132
          } 
133
        catch(IOException e) { }
134 9c5b5cb6 Leszek Koltunski
        }
135
136 427ab7bf Leszek Koltunski
      bmpHeight = bitmap.getHeight();
137
      bmpWidth  = bitmap.getWidth();
138 9c5b5cb6 Leszek Koltunski
139 7451c98a Leszek Koltunski
      mTexture = new DistortedTexture(bmpWidth,bmpHeight);
140 f6d884d5 Leszek Koltunski
      mTexture.setTexture(bitmap);
141 10b7e588 Leszek Koltunski
      mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
142 e8b6aa95 Leszek Koltunski
143 9c5b5cb6 Leszek Koltunski
      // Try adding 2 Vertex Effects to the Bitmap.
144
      // This will fail if we have set maxVertexEffects to something < 2.
145
      //
146
      // Even if adding some of the Effects fails, the App will still start - you just won't see
147
      // the effects that failed to add.
148 7bf107f7 Leszek Koltunski
149 334c13fa Leszek Koltunski
      Dynamic3D dSwirl = new Dynamic3D(2000,0.0f);
150
      dSwirl.add(new Static3D(        0, bmpHeight/2, 0));
151
      dSwirl.add(new Static3D( bmpWidth, bmpHeight/2, 0));
152 7bf107f7 Leszek Koltunski
153 392e16fd Leszek Koltunski
      long swirlEffectID = mEffects.swirl( new Static1D(30), dSwirl, new Static4D( 0,0,40,40) );
154 b4a73ea5 Leszek Koltunski
155
      if( swirlEffectID<0 )
156
        {
157 e7a4ef16 Leszek Koltunski
        Log.e("Check", "Failed to add Swirl effect!");
158 b4a73ea5 Leszek Koltunski
        }
159 7bf107f7 Leszek Koltunski
160 f988589e Leszek Koltunski
      Dynamic3D dDeform = new Dynamic3D(2000,0.0f);
161 7bf107f7 Leszek Koltunski
      dDeform.add(new Static3D( 0,         0,0));
162
      dDeform.add(new Static3D( 0,-bmpHeight,0));
163
164 392e16fd Leszek Koltunski
      long deformEffectID = mEffects.deform(dDeform, new Static3D(bmpWidth/2,0,0) );
165 b4a73ea5 Leszek Koltunski
166
      if( deformEffectID<0 )
167
        {
168 e7a4ef16 Leszek Koltunski
        Log.e("Check", "Failed to add Deform effect!");
169 b4a73ea5 Leszek Koltunski
        }
170 9c5b5cb6 Leszek Koltunski
171
      // Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
172 7589635e Leszek Koltunski
      Static3D color = new Static3D(1,0,0);
173 f988589e Leszek Koltunski
      Dynamic1D inter = new Dynamic1D(2000,0.0f);
174 7589635e Leszek Koltunski
      inter.add(new Static1D(0));
175
      inter.add(new Static1D(1));
176 9c5b5cb6 Leszek Koltunski
177 392e16fd Leszek Koltunski
      long chromaEffectID = mEffects.chroma(inter, color);
178 b4a73ea5 Leszek Koltunski
179
      if( chromaEffectID<0 )
180
        {
181 e7a4ef16 Leszek Koltunski
        Log.e("Check", "Failed to add Chroma effect!");
182 b4a73ea5 Leszek Koltunski
        }
183 9c5b5cb6 Leszek Koltunski
184 427ab7bf Leszek Koltunski
      try
185
        {
186 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
187 427ab7bf Leszek Koltunski
        }
188
      catch(Exception ex)
189
        {
190 9c5b5cb6 Leszek Koltunski
        // We have failed to compile or link shaders - probably too many effects.
191
        // (each effect adds a certain number of Uniforms to the shader and there
192
        // can only be a limited number of those )
193
194 427ab7bf Leszek Koltunski
        compilationTitle = ex.getClass().getSimpleName();
195
        compilationResult= ex.getMessage();
196
      
197
        String ver;
198
      
199
        ver = GLES20.glGetString(GLES20.GL_SHADING_LANGUAGE_VERSION);   
200
        compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
201
        ver = GLES20.glGetString(GLES20.GL_VERSION);  
202
        compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
203
        ver = GLES20.glGetString(GLES20.GL_VENDOR);   
204
        compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
205
        ver = GLES20.glGetString(GLES20.GL_RENDERER);    
206
        compilationResult += "\nGL renderer: "+(ver==null ? "null" : ver);
207
      
208
        CheckActivity act = (CheckActivity)mView.getContext();
209
      
210
        act.runOnUiThread(new Runnable() 
211
          {
212
          public void run() 
213
            {
214
            Context context = mView.getContext();
215
      
216
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
217
            builder.setTitle(compilationTitle);
218
            builder.setMessage(compilationResult);
219
            builder.setCancelable(true);
220
           
221
            builder.setOnCancelListener(new DialogInterface.OnCancelListener() 
222
              {
223
              @Override
224
              public void onCancel(DialogInterface dialog) 
225
                {
226
                ((CheckActivity)mView.getContext()).finish();
227
                }
228
              });
229
            
230
            AlertDialog alert = builder.create();
231
            alert.show();
232
            }
233
          });
234
        }
235
      }
236
    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
    
239
}