Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 6637d0f2

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