Project

General

Profile

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

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

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

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

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

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

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

    
77
      mScreen = new DistortedScreen();
78
      }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
   
82
    public void onDrawFrame(GL10 glUnused) 
83
      {
84
      mScreen.render( System.currentTimeMillis() );
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
    
89
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
90
      { 
91
      float qx = (float)width /bmpWidth;
92
      float qy = (float)height/bmpHeight;
93

    
94
      mEffects.abortEffects(EffectTypes.MATRIX);
95
      mEffects.scale(  qx<qy ? (new Static3D(1,qx/qy,1)) : (new Static3D(qy/qx,1,1)) );
96
      
97
      mScreen.resize(width, height);
98
      }
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

    
107
      try
108
        {
109
        bitmap = BitmapFactory.decodeStream(is);
110
        } 
111
      finally 
112
        {
113
        try 
114
          {
115
          is.close();
116
          } 
117
        catch(IOException e) { }
118
        }
119

    
120
      bmpHeight = bitmap.getHeight();
121
      bmpWidth  = bitmap.getWidth();
122

    
123
      if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
124
      mTexture.setTexture(bitmap);
125

    
126
      mScreen.detachAll();
127
      mScreen.attach(mTexture,mEffects,new MeshFlat(30,30*bmpHeight/bmpWidth));
128

    
129
      mEffects.abortEffects(EffectTypes.VERTEX);
130
      mEffects.abortEffects(EffectTypes.FRAGMENT);
131

    
132
      // 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

    
138
      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

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

    
144
      if( swirlEffectID<0 )
145
        {
146
        Log.e("Check", "Failed to add Swirl effect!");
147
        }
148

    
149
      Dynamic3D dDeform = new Dynamic3D(2000,0.0f);
150
      dDeform.add(new Static3D( 0,         0,0));
151
      dDeform.add(new Static3D( 0,-bmpHeight,0));
152

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

    
155
      if( deformEffectID<0 )
156
        {
157
        Log.e("Check", "Failed to add Deform effect!");
158
        }
159

    
160
      // Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
161
      Static3D color = new Static3D(1,0,0);
162
      Dynamic1D inter = new Dynamic1D(2000,0.0f);
163
      inter.add(new Static1D(0));
164
      inter.add(new Static1D(1));
165

    
166
      long chromaEffectID = mEffects.chroma(inter, color);
167

    
168
      if( chromaEffectID<0 )
169
        {
170
        Log.e("Check", "Failed to add Chroma effect!");
171
        }
172

    
173
      DistortedEffects.enableEffect(EffectNames.SWIRL);
174
      DistortedEffects.enableEffect(EffectNames.DEFORM);
175
      DistortedEffects.enableEffect(EffectNames.CHROMA);
176

    
177
      try
178
        {
179
        Distorted.onCreate(mView.getContext());
180
        }
181
      catch(Exception ex)
182
        {
183
        // 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
        compilationTitle = ex.getClass().getSimpleName();
188
        compilationResult= ex.getMessage();
189
      
190
        String ver;
191
      
192
        ver = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);   
193
        compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
194
        ver = GLES30.glGetString(GLES30.GL_VERSION);  
195
        compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
196
        ver = GLES30.glGetString(GLES30.GL_VENDOR);   
197
        compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
198
        ver = GLES30.glGetString(GLES30.GL_RENDERER);    
199
        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
}
(2-2/3)