Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 107e4b72

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.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectDeform;
35
import org.distorted.library.effect.VertexEffectSwirl;
36
import org.distorted.library.main.Distorted;
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.MeshFlat;
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.AlertDialog;
48
import android.content.Context;
49
import android.content.DialogInterface;
50
import android.graphics.Bitmap;
51
import android.graphics.BitmapFactory;
52
import android.opengl.GLES31;
53
import android.opengl.GLSurfaceView;
54
import android.util.Log;
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

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

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

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

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

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

    
80
      DistortedEffects.setMax(EffectType.VERTEX  ,act.getMaxV());
81
      DistortedEffects.setMax(EffectType.FRAGMENT,act.getMaxF());
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
      mMove   = new Static3D(0,0,0);
91
      mScale  = new Static3D(1,1,1);
92
      mCenter = new Static3D(0,0,0);
93

    
94
      mEffects = new DistortedEffects();
95
      mEffects.apply(new MatrixEffectMove(mMove));
96
      mEffects.apply(new MatrixEffectScale(mScale));
97

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

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

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

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

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

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

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

    
133
      mScreen = new DistortedScreen();
134
      }
135

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

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
    
145
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
146
      { 
147
      if( (float)mObjHeight/mObjWidth > (float)height/width )
148
        {
149
        int w = (height*mObjWidth)/mObjHeight;
150
        float factor = (float)height/mObjHeight;
151
        mMove.set((width-w)/2,0,0);
152
        mScale.set(factor,factor,factor);
153
        }
154
      else
155
        {
156
        int h = (width*mObjHeight)/mObjWidth;
157
        float factor = (float)width/mObjWidth;
158
        mMove.set(0,(height-h)/2,0);
159
        mScale.set(factor,factor,factor);
160
        }
161

    
162
      mScreen.resize(width,height);
163
      }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
    
167
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
168
      {  
169
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
170
      Bitmap bitmap;
171

    
172
      try
173
        {
174
        bitmap = BitmapFactory.decodeStream(is);
175
        } 
176
      finally 
177
        {
178
        try 
179
          {
180
          is.close();
181
          } 
182
        catch(IOException e) { }
183
        }
184

    
185
      mObjHeight = bitmap.getHeight();
186
      mObjWidth  = bitmap.getWidth();
187

    
188
      if( mTexture==null ) mTexture = new DistortedTexture(mObjWidth,mObjHeight);
189
      mTexture.setTexture(bitmap);
190

    
191
      if( mMesh==null ) mMesh = new MeshFlat(30,30*mObjHeight/mObjWidth);
192

    
193
      mScreen.detachAll();
194
      mScreen.attach(mTexture,mEffects,mMesh);
195

    
196
      mSwirl1.set (          0, mObjHeight/2, 0);
197
      mSwirl2.set (mObjWidth  , mObjHeight/2, 0);
198
      mDeform1.set(          0,         0   , 0);
199
      mDeform2.set(          0,-mObjHeight  , 0);
200
      mCenter.set (mObjWidth/2,         0   , 0);
201

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

    
212
        compilationTitle = ex.getClass().getSimpleName();
213
        compilationResult= ex.getMessage();
214
      
215
        String ver;
216
      
217
        ver = GLES31.glGetString(GLES31.GL_SHADING_LANGUAGE_VERSION);
218
        compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
219
        ver = GLES31.glGetString(GLES31.GL_VERSION);
220
        compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
221
        ver = GLES31.glGetString(GLES31.GL_VENDOR);
222
        compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
223
        ver = GLES31.glGetString(GLES31.GL_RENDERER);
224
        compilationResult += "\nGL renderer: "+(ver==null ? "null" : ver);
225
      
226
        CheckActivity act = (CheckActivity)mView.getContext();
227
      
228
        act.runOnUiThread(new Runnable() 
229
          {
230
          public void run() 
231
            {
232
            Context context = mView.getContext();
233
      
234
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
235
            builder.setTitle(compilationTitle);
236
            builder.setMessage(compilationResult);
237
            builder.setCancelable(true);
238
           
239
            builder.setOnCancelListener(new DialogInterface.OnCancelListener() 
240
              {
241
              @Override
242
              public void onCancel(DialogInterface dialog) 
243
                {
244
                ((CheckActivity)mView.getContext()).finish();
245
                }
246
              });
247
            
248
            AlertDialog alert = builder.create();
249
            alert.show();
250
            }
251
          });
252
        }
253
      }
254
    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256
    
257
}
(2-2/3)