Project

General

Profile

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

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

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.VertexEffectScale;
35
import org.distorted.library.effect.VertexEffectSwirl;
36
import org.distorted.library.main.DistortedLibrary;
37
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.mesh.MeshSquare;
41
import org.distorted.library.type.Dynamic3D;
42
import org.distorted.library.type.Static1D;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45
import org.distorted.library.type.Dynamic1D;
46

    
47
import android.app.ActivityManager;
48
import android.app.AlertDialog;
49
import android.content.Context;
50
import android.content.DialogInterface;
51
import android.content.pm.ConfigurationInfo;
52
import android.content.res.Resources;
53
import android.graphics.Bitmap;
54
import android.graphics.BitmapFactory;
55
import android.opengl.GLES31;
56
import android.opengl.GLSurfaceView;
57
import android.util.Log;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
class CheckRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
62
{
63
    private static String compilationResult;
64
    private static String compilationTitle;
65

    
66
    private final GLSurfaceView mView;
67
    private final Resources mResources;
68
    private final DistortedEffects mEffects;
69
    private final DistortedScreen mScreen;
70
    private final Static3D mScaleMatrix, mScaleVertex, mSwirl1, mSwirl2, mDeform1, mDeform2;
71
    private final Static1D mRadius;
72

    
73
    private DistortedTexture mTexture;
74
    private MeshSquare mMesh;
75
    private int mObjHeight, mObjWidth;
76

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

    
79
    CheckRenderer(GLSurfaceView view)
80
      { 
81
      mView = view;
82
      mResources = view.getResources();
83

    
84
      CheckActivity act = (CheckActivity)mView.getContext();
85

    
86
      DistortedLibrary.setMax(EffectType.VERTEX  ,act.getMaxV());
87
      DistortedLibrary.setMax(EffectType.FRAGMENT,act.getMaxF());
88

    
89
      VertexEffectScale.enable();
90
      VertexEffectSwirl.enable();
91
      VertexEffectDeform.enable();
92
      FragmentEffectChroma.enable();
93

    
94
      mSwirl1     = new Static3D(0,0,0);
95
      mSwirl2     = new Static3D(0,0,0);
96
      mDeform1    = new Static3D(0,0,0);
97
      mDeform2    = new Static3D(0,0,0);
98
      mScaleMatrix= new Static3D(1,1,1);
99
      mScaleVertex= new Static3D(1,1,1);
100

    
101
      mRadius = new Static1D(0);
102

    
103
      mEffects = new DistortedEffects();
104
      mEffects.apply(new MatrixEffectScale(mScaleMatrix));
105

    
106
      // Try adding 3 Vertex Effects to the Bitmap.
107
      // This will fail if we have set maxVertexEffects to something < 3.
108
      //
109
      // Even if adding some of the Effects fails, the App will still start - you just won't see
110
      // the effects that failed to add.
111

    
112
      if( !mEffects.apply( new VertexEffectScale(mScaleVertex) ) )
113
        {
114
        Log.e("Check", "Failed to add Scale effect!");
115
        }
116

    
117
      Dynamic3D dSwirl = new Dynamic3D(2000,0.0f);
118
      dSwirl.add(mSwirl1);
119
      dSwirl.add(mSwirl2);
120

    
121
      if( !mEffects.apply( new VertexEffectSwirl(new Static1D(30), dSwirl, new Static4D( 0,0,0,40)) ) )
122
        {
123
        Log.e("Check", "Failed to add Swirl effect!");
124
        }
125

    
126
      Dynamic3D dDeform = new Dynamic3D(2000,0.0f);
127
      dDeform.add(mDeform1);
128
      dDeform.add(mDeform2);
129

    
130
      if( !mEffects.apply( new VertexEffectDeform(dDeform, mRadius, new Static3D(0,0,0)) ) )
131
        {
132
        Log.e("Check", "Failed to add Deform effect!");
133
        }
134

    
135
      // Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
136
      Static3D color = new Static3D(1,0,0);
137
      Dynamic1D inter = new Dynamic1D(2000,0.0f);
138
      inter.add(new Static1D(0));
139
      inter.add(new Static1D(1));
140

    
141
      if( !mEffects.apply( new FragmentEffectChroma(inter, color) ) )
142
        {
143
        Log.e("Check", "Failed to add Chroma effect!");
144
        }
145

    
146
      mScreen = new DistortedScreen();
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
   
151
    public void onDrawFrame(GL10 glUnused) 
152
      {
153
      mScreen.render( System.currentTimeMillis() );
154
      }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
    
158
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
159
      {
160
      final float SCALE = 0.9f;
161

    
162
      float xW = (float)width /mObjWidth;
163
      float xH = (float)height/mObjHeight;
164

    
165
      float factor = xW>xH ? SCALE*xH : SCALE*xW;
166

    
167
      mScaleMatrix.set(factor,factor,factor);
168

    
169
      mScreen.resize(width,height);
170
      }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
    
174
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
175
      {  
176
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
177
      Bitmap bitmap;
178

    
179
      try
180
        {
181
        bitmap = BitmapFactory.decodeStream(is);
182
        } 
183
      finally 
184
        {
185
        try 
186
          {
187
          is.close();
188
          } 
189
        catch(IOException ignored) { }
190
        }
191

    
192
      mObjHeight = bitmap.getHeight();
193
      mObjWidth  = bitmap.getWidth();
194

    
195
      mRadius.set(mObjWidth*0.5f);
196
      mScaleVertex.set(mObjWidth,mObjHeight,0);
197

    
198
      if( mMesh   ==null ) mMesh = new MeshSquare(30,30*mObjHeight/mObjWidth);
199
      if( mTexture==null ) mTexture = new DistortedTexture();
200
      mTexture.setTexture(bitmap);
201

    
202
      mScreen.detachAll();
203
      mScreen.attach(mTexture,mEffects,mMesh);
204

    
205
      mSwirl1.set (-mObjWidth/2,          0, 0);
206
      mSwirl2.set ( mObjWidth/2,          0, 0);
207
      mDeform1.set(           0,          0, 0);
208
      mDeform2.set(           0, mObjHeight, 0);
209

    
210
      DistortedLibrary.onSurfaceCreated(this);
211
      }
212
    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
// We have failed to compile or link shaders - probably too many effects.
215
// (each effect adds a certain number of Uniforms to the shader and there
216
// can only be a limited number of those )
217

    
218
    public void distortedException(Exception ex)
219
      {
220
      compilationTitle = ex.getClass().getSimpleName();
221
      compilationResult= ex.getMessage();
222

    
223
      String ver;
224

    
225
      ver = GLES31.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION);
226
      compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
227
      ver = GLES31.glGetString(GLES31.GL_VERSION);
228
      compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
229
      ver = GLES31.glGetString(GLES31.GL_VENDOR);
230
      compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
231
      ver = GLES31.glGetString(GLES31.GL_RENDERER);
232
      compilationResult += "\nGL renderer: "+(ver==null ? "null" : ver);
233

    
234
      CheckActivity act = (CheckActivity)mView.getContext();
235

    
236
      act.runOnUiThread(new Runnable()
237
        {
238
        public void run()
239
          {
240
          Context context = mView.getContext();
241

    
242
          AlertDialog.Builder builder = new AlertDialog.Builder(context);
243
          builder.setTitle(compilationTitle);
244
          builder.setMessage(compilationResult);
245
          builder.setCancelable(true);
246

    
247
          builder.setOnCancelListener(new DialogInterface.OnCancelListener()
248
            {
249
            @Override
250
            public void onCancel(DialogInterface dialog)
251
              {
252
              ((CheckActivity)mView.getContext()).finish();
253
              }
254
            });
255

    
256
          AlertDialog alert = builder.create();
257
          alert.show();
258
          }
259
        });
260
      }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263

    
264
   public int openGlVersion()
265
      {
266
      Context context = mView.getContext();
267
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
268
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
269
      int glESversion = configurationInfo.reqGlEsVersion;
270
      int major = glESversion >> 16;
271
      int minor = glESversion & 0xff;
272

    
273
      return 100*major + 10*minor;
274
      }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277

    
278
   public InputStream localFile(int fileID)
279
      {
280
      return mResources.openRawResource(fileID);
281
      }
282

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

    
285
   public void logMessage(String message)
286
      {
287
      android.util.Log.e("Check", message );
288
      }
289
}
(2-2/3)