Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 7bf107f7

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

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

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

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

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

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

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

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

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

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

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

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

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

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

    
131
      // Try adding 2 Vertex Effects to the Bitmap.
132
      // This will fail if we have set maxVertexEffects to something < 2.
133
      //
134
      // Even if adding some of the Effects fails, the App will still start - you just won't see
135
      // the effects that failed to add.
136

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

    
143
      mSuccess.swirl( new Static1D(30), dSwirl, new Static4D( 0,0,40,40) );
144

    
145
      Dynamic3D dDeform = new Dynamic3D();
146
      dDeform.setCount(0.0f);
147
      dDeform.setDuration(2000);
148
      dDeform.add(new Static3D( 0,         0,0));
149
      dDeform.add(new Static3D( 0,-bmpHeight,0));
150

    
151
      mSuccess.deform(dDeform, new Static2D(bmpWidth/2,0) );
152

    
153
      // Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
154
      Static3D color = new Static3D(1,0,0);
155
      Dynamic1D inter = new Dynamic1D();
156
      inter.setCount(0.0f);
157
      inter.setDuration(2000);
158
      inter.add(new Static1D(0));
159
      inter.add(new Static1D(1));
160

    
161
      mSuccess.chroma(inter, color);
162

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

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