Project

General

Profile

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

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

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