Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 7589635e

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.DistortedBitmap;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.type.Static1D;
34
import org.distorted.library.type.Static2D;
35
import org.distorted.library.type.Static3D;
36
import org.distorted.library.type.Static4D;
37
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.Dynamic2D;
39

    
40
import android.app.AlertDialog;
41
import android.content.Context;
42
import android.content.DialogInterface;
43
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
45
import android.opengl.GLES20;
46
import android.opengl.GLSurfaceView;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class CheckRenderer implements GLSurfaceView.Renderer 
51
{
52
    private GLSurfaceView mView;
53
    private DistortedBitmap mSuccess;
54
    private int bmpHeight, bmpWidth;
55

    
56
    public static String compilationResult;
57
    public static String compilationTitle;
58

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

    
61
    public CheckRenderer(GLSurfaceView view) 
62
      { 
63
      mView = view;
64

    
65
      Distorted.setMaxVertex(CheckActivity.getMaxV());
66
      Distorted.setMaxFragment(CheckActivity.getMaxF());
67
      }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
   
71
    public void onDrawFrame(GL10 glUnused) 
72
      {
73
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
74
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
75
    
76
      mSuccess.draw(System.currentTimeMillis());
77
      }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
    
81
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
82
      { 
83
      mSuccess.abortEffects(EffectTypes.MATRIX);
84
      
85
      if( bmpHeight/bmpWidth > height/width )
86
        {
87
        int w = (height*bmpWidth)/bmpHeight;
88
        float factor = (float)height/bmpHeight;
89

    
90
        mSuccess.move( new Static3D((width-w)/2,0,0) );
91
        mSuccess.scale( new Static3D(factor,factor,factor) );
92
        }
93
      else
94
        {
95
        int h = (width*bmpHeight)/bmpWidth;
96
        float factor = (float)width/bmpWidth;
97

    
98
        mSuccess.move( new Static3D(0,(height-h)/2,0) );
99
        mSuccess.scale( new Static3D(factor,factor,factor) );
100
        }
101
      
102
      Distorted.onSurfaceChanged(width, height);
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
108
      {  
109
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
110
      Bitmap bitmap;
111

    
112
      try
113
        {
114
        bitmap = BitmapFactory.decodeStream(is);
115
        } 
116
      finally 
117
        {
118
        try 
119
          {
120
          is.close();
121
          } 
122
        catch(IOException e) { }
123
        }
124

    
125
      mSuccess = new DistortedBitmap(bitmap, 30);
126

    
127
      bmpHeight = bitmap.getHeight();
128
      bmpWidth  = bitmap.getWidth();
129

    
130
      // Try adding 2 Vertex Effects to the Bitmap.
131
      // This will fail if we have set maxVertexEffects to something < 2.
132
      //
133
      // Even if adding some of the Effects fails, the App will still start - you just won't see
134
      // the effects that failed to add.
135
      Static2D pDown   = new Static2D(bmpWidth/2,   0);
136
      Static3D vDown   = new Static3D( 0,-bmpHeight,0);
137
      Static4D mRegion = new Static4D( 0, 0, 40 ,40 );
138

    
139
      Dynamic2D mPoint = new Dynamic2D();
140
      mPoint.setCount(0.0f);
141
      mPoint.setDuration(2000);
142
      mPoint.add(new Static2D(        0, bmpHeight/2));
143
      mPoint.add(new Static2D( bmpWidth, bmpHeight/2));
144

    
145
      mSuccess.swirl( 30, mRegion, mPoint );
146
      mSuccess.deform(vDown, pDown, 2000, 0.0f);
147

    
148
      // Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
149
      Static3D color = new Static3D(1,0,0);
150
      Dynamic1D inter = new Dynamic1D();
151
      inter.setCount(0.0f);
152
      inter.setDuration(2000);
153
      inter.add(new Static1D(0));
154
      inter.add(new Static1D(1));
155

    
156
      mSuccess.chroma(inter, color, null, new Static2D(0,0) );
157

    
158
      try
159
        {
160
        Distorted.onSurfaceCreated(mView.getContext());
161
        }
162
      catch(Exception ex)
163
        {
164
        // We have failed to compile or link shaders - probably too many effects.
165
        // (each effect adds a certain number of Uniforms to the shader and there
166
        // can only be a limited number of those )
167

    
168
        compilationTitle = ex.getClass().getSimpleName();
169
        compilationResult= ex.getMessage();
170
      
171
        String ver;
172
      
173
        ver = GLES20.glGetString(GLES20.GL_SHADING_LANGUAGE_VERSION);   
174
        compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
175
        ver = GLES20.glGetString(GLES20.GL_VERSION);  
176
        compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
177
        ver = GLES20.glGetString(GLES20.GL_VENDOR);   
178
        compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
179
        ver = GLES20.glGetString(GLES20.GL_RENDERER);    
180
        compilationResult += "\nGL renderer: "+(ver==null ? "null" : ver);
181
      
182
        CheckActivity act = (CheckActivity)mView.getContext();
183
      
184
        act.runOnUiThread(new Runnable() 
185
          {
186
          public void run() 
187
            {
188
            Context context = mView.getContext();
189
      
190
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
191
            builder.setTitle(compilationTitle);
192
            builder.setMessage(compilationResult);
193
            builder.setCancelable(true);
194
           
195
            builder.setOnCancelListener(new DialogInterface.OnCancelListener() 
196
              {
197
              @Override
198
              public void onCancel(DialogInterface dialog) 
199
                {
200
                ((CheckActivity)mView.getContext()).finish();
201
                }
202
              });
203
            
204
            AlertDialog alert = builder.create();
205
            alert.show();
206
            }
207
          });
208
        }
209
      }
210
    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212
    
213
}
(2-2/3)