Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 01782e85

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