Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 8664ea2e

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.effect.EffectType;
31
import org.distorted.library.effect.FragmentEffectChroma;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectDeform;
34
import org.distorted.library.effect.VertexEffectSwirl;
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.main.DistortedTexture;
39
import org.distorted.library.mesh.MeshRectangles;
40
import org.distorted.library.type.Dynamic3D;
41
import org.distorted.library.type.Static1D;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44
import org.distorted.library.type.Dynamic1D;
45

    
46
import android.app.AlertDialog;
47
import android.content.Context;
48
import android.content.DialogInterface;
49
import android.graphics.Bitmap;
50
import android.graphics.BitmapFactory;
51
import android.opengl.GLES31;
52
import android.opengl.GLSurfaceView;
53
import android.util.Log;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
class CheckRenderer implements GLSurfaceView.Renderer 
58
{
59
    private static String compilationResult;
60
    private static String compilationTitle;
61

    
62
    private GLSurfaceView mView;
63
    private DistortedEffects mEffects;
64
    private DistortedTexture mTexture;
65
    private MeshRectangles mMesh;
66
    private DistortedScreen mScreen;
67
    private int mObjHeight, mObjWidth;
68
    private Static3D mScale, mCenter;
69
    private Static3D mSwirl1, mSwirl2, mDeform1, mDeform2;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
    CheckRenderer(GLSurfaceView view)
74
      { 
75
      mView = view;
76

    
77
      CheckActivity act = (CheckActivity)mView.getContext();
78

    
79
      DistortedLibrary.setMax(EffectType.VERTEX  ,act.getMaxV());
80
      DistortedLibrary.setMax(EffectType.FRAGMENT,act.getMaxF());
81

    
82
      VertexEffectSwirl.enable();
83
      VertexEffectDeform.enable();
84
      FragmentEffectChroma.enable();
85

    
86
      mSwirl1 = new Static3D(0,0,0);
87
      mSwirl2 = new Static3D(0,0,0);
88
      mDeform1= new Static3D(0,0,0);
89
      mDeform2= new Static3D(0,0,0);
90
      mScale  = new Static3D(1,1,1);
91
      mCenter = new Static3D(0,0,0);
92

    
93
      mEffects = new DistortedEffects();
94
      mEffects.apply(new MatrixEffectScale(mScale));
95

    
96
      // Try adding 2 Vertex Effects to the Bitmap.
97
      // This will fail if we have set maxVertexEffects to something < 2.
98
      //
99
      // Even if adding some of the Effects fails, the App will still start - you just won't see
100
      // the effects that failed to add.
101

    
102
      Dynamic3D dSwirl = new Dynamic3D(2000,0.0f);
103
      dSwirl.add(mSwirl1);
104
      dSwirl.add(mSwirl2);
105

    
106
      if( !mEffects.apply( new VertexEffectSwirl(new Static1D(30), dSwirl, new Static4D( 0,0,0,40)) ) )
107
        {
108
        Log.e("Check", "Failed to add Swirl effect!");
109
        }
110

    
111
      Dynamic3D dDeform = new Dynamic3D(2000,0.0f);
112
      dDeform.add(mDeform1);
113
      dDeform.add(mDeform2);
114

    
115
      if( !mEffects.apply( new VertexEffectDeform(dDeform,mCenter) ) )
116
        {
117
        Log.e("Check", "Failed to add Deform effect!");
118
        }
119

    
120
      // Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
121
      Static3D color = new Static3D(1,0,0);
122
      Dynamic1D inter = new Dynamic1D(2000,0.0f);
123
      inter.add(new Static1D(0));
124
      inter.add(new Static1D(1));
125

    
126
      if( !mEffects.apply( new FragmentEffectChroma(inter, color) ) )
127
        {
128
        Log.e("Check", "Failed to add Chroma effect!");
129
        }
130

    
131
      mScreen = new DistortedScreen();
132
      }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
   
136
    public void onDrawFrame(GL10 glUnused) 
137
      {
138
      mScreen.render( System.currentTimeMillis() );
139
      }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
    
143
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
144
      {
145
      final float SCALE = 0.9f;
146

    
147
      float xW = (float)width /mObjWidth;
148
      float xH = (float)height/mObjHeight;
149

    
150
      float factor = xW>xH ? SCALE*xH : SCALE*xW;
151

    
152
      mScale.set(factor,factor,factor);
153

    
154
      mScreen.resize(width,height);
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
    
159
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
160
      {  
161
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
162
      Bitmap bitmap;
163

    
164
      try
165
        {
166
        bitmap = BitmapFactory.decodeStream(is);
167
        } 
168
      finally 
169
        {
170
        try 
171
          {
172
          is.close();
173
          } 
174
        catch(IOException e) { }
175
        }
176

    
177
      mObjHeight = bitmap.getHeight();
178
      mObjWidth  = bitmap.getWidth();
179

    
180
      if( mTexture==null ) mTexture = new DistortedTexture();
181
      mTexture.setTexture(bitmap);
182

    
183
      if( mMesh==null )
184
        {
185
        mMesh = new MeshRectangles(30,30*mObjHeight/mObjWidth);
186
        mMesh.setStretch(mObjWidth,mObjHeight,0);
187
        }
188

    
189
      mScreen.detachAll();
190
      mScreen.attach(mTexture,mEffects,mMesh);
191

    
192
      mSwirl1.set (          0, mObjHeight/2, 0);
193
      mSwirl2.set (mObjWidth  , mObjHeight/2, 0);
194
      mDeform1.set(          0,         0   , 0);
195
      mDeform2.set(          0, mObjHeight  , 0);
196
      mCenter.set (mObjWidth/2,         0   , 0);
197

    
198
      try
199
        {
200
        DistortedLibrary.onCreate(mView.getContext());
201
        }
202
      catch(Exception ex)
203
        {
204
        // We have failed to compile or link shaders - probably too many effects.
205
        // (each effect adds a certain number of Uniforms to the shader and there
206
        // can only be a limited number of those )
207

    
208
        compilationTitle = ex.getClass().getSimpleName();
209
        compilationResult= ex.getMessage();
210
      
211
        String ver;
212
      
213
        ver = GLES31.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION);
214
        compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
215
        ver = GLES31.glGetString(GLES31.GL_VERSION);
216
        compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
217
        ver = GLES31.glGetString(GLES31.GL_VENDOR);
218
        compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
219
        ver = GLES31.glGetString(GLES31.GL_RENDERER);
220
        compilationResult += "\nGL renderer: "+(ver==null ? "null" : ver);
221

    
222
        CheckActivity act = (CheckActivity)mView.getContext();
223

    
224
        act.runOnUiThread(new Runnable() 
225
          {
226
          public void run() 
227
            {
228
            Context context = mView.getContext();
229
      
230
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
231
            builder.setTitle(compilationTitle);
232
            builder.setMessage(compilationResult);
233
            builder.setCancelable(true);
234
           
235
            builder.setOnCancelListener(new DialogInterface.OnCancelListener() 
236
              {
237
              @Override
238
              public void onCancel(DialogInterface dialog) 
239
                {
240
                ((CheckActivity)mView.getContext()).finish();
241
                }
242
              });
243
            
244
            AlertDialog alert = builder.create();
245
            alert.show();
246
            }
247
          });
248
        }
249
      }
250
    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
    
253
}
(2-2/3)