Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 10b7e588

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.DistortedObject;
32
import org.distorted.library.GridFlat;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic3D;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38
import org.distorted.library.type.Dynamic1D;
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
import android.util.Log;
48

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

    
51
class CheckRenderer implements GLSurfaceView.Renderer 
52
{
53
    private static String compilationResult;
54
    private static String compilationTitle;
55

    
56
    private GLSurfaceView mView;
57
    private DistortedObject mSuccess;
58
    private GridFlat mGrid;
59
    private int bmpHeight, bmpWidth;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

    
67
      CheckActivity act = (CheckActivity)mView.getContext();
68

    
69
      Distorted.setMaxVertex(act.getMaxV());
70
      Distorted.setMaxFragment(act.getMaxF());
71
      }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
   
75
    public void onDrawFrame(GL10 glUnused) 
76
      {
77
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
78
      mSuccess.draw(System.currentTimeMillis(), mGrid);
79
      }
80

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

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

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

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
110
      {  
111
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
112

    
113
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
114
      Bitmap bitmap;
115

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

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

    
132
      mSuccess = new DistortedObject(bmpWidth,bmpHeight,1);
133
      mSuccess.setTexture(bitmap);
134
      mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
135

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

    
142
      Dynamic3D dSwirl = new Dynamic3D(2000,0.0f);
143
      dSwirl.add(new Static3D(        0, bmpHeight/2, 0));
144
      dSwirl.add(new Static3D( bmpWidth, bmpHeight/2, 0));
145

    
146
      long swirlEffectID = mSuccess.swirl( new Static1D(30), dSwirl, new Static4D( 0,0,40,40) );
147

    
148
      if( swirlEffectID<0 )
149
        {
150
        Log.e("Check", "Failed to add Swirl effect!");
151
        }
152

    
153
      Dynamic3D dDeform = new Dynamic3D(2000,0.0f);
154
      dDeform.add(new Static3D( 0,         0,0));
155
      dDeform.add(new Static3D( 0,-bmpHeight,0));
156

    
157
      long deformEffectID = mSuccess.deform(dDeform, new Static3D(bmpWidth/2,0,0) );
158

    
159
      if( deformEffectID<0 )
160
        {
161
        Log.e("Check", "Failed to add Deform effect!");
162
        }
163

    
164
      // Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
165
      Static3D color = new Static3D(1,0,0);
166
      Dynamic1D inter = new Dynamic1D(2000,0.0f);
167
      inter.add(new Static1D(0));
168
      inter.add(new Static1D(1));
169

    
170
      long chromaEffectID = mSuccess.chroma(inter, color);
171

    
172
      if( chromaEffectID<0 )
173
        {
174
        Log.e("Check", "Failed to add Chroma effect!");
175
        }
176

    
177
      try
178
        {
179
        Distorted.onSurfaceCreated(mView.getContext());
180
        }
181
      catch(Exception ex)
182
        {
183
        // We have failed to compile or link shaders - probably too many effects.
184
        // (each effect adds a certain number of Uniforms to the shader and there
185
        // can only be a limited number of those )
186

    
187
        compilationTitle = ex.getClass().getSimpleName();
188
        compilationResult= ex.getMessage();
189
      
190
        String ver;
191
      
192
        ver = GLES20.glGetString(GLES20.GL_SHADING_LANGUAGE_VERSION);   
193
        compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
194
        ver = GLES20.glGetString(GLES20.GL_VERSION);  
195
        compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
196
        ver = GLES20.glGetString(GLES20.GL_VENDOR);   
197
        compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
198
        ver = GLES20.glGetString(GLES20.GL_RENDERER);    
199
        compilationResult += "\nGL renderer: "+(ver==null ? "null" : ver);
200
      
201
        CheckActivity act = (CheckActivity)mView.getContext();
202
      
203
        act.runOnUiThread(new Runnable() 
204
          {
205
          public void run() 
206
            {
207
            Context context = mView.getContext();
208
      
209
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
210
            builder.setTitle(compilationTitle);
211
            builder.setMessage(compilationResult);
212
            builder.setCancelable(true);
213
           
214
            builder.setOnCancelListener(new DialogInterface.OnCancelListener() 
215
              {
216
              @Override
217
              public void onCancel(DialogInterface dialog) 
218
                {
219
                ((CheckActivity)mView.getContext()).finish();
220
                }
221
              });
222
            
223
            AlertDialog alert = builder.create();
224
            alert.show();
225
            }
226
          });
227
        }
228
      }
229
    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
    
232
}
(2-2/3)