Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.check;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
31 e8b6aa95 Leszek Koltunski
import org.distorted.library.DistortedObject;
32 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
33 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
34 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
35 7589635e Leszek Koltunski
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 427ab7bf Leszek Koltunski
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 b4a73ea5 Leszek Koltunski
import android.util.Log;
48 427ab7bf Leszek Koltunski
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51
class CheckRenderer implements GLSurfaceView.Renderer 
52
{
53 9b9440a0 Leszek Koltunski
    private static String compilationResult;
54
    private static String compilationTitle;
55 b4a73ea5 Leszek Koltunski
56 427ab7bf Leszek Koltunski
    private GLSurfaceView mView;
57 e8b6aa95 Leszek Koltunski
    private DistortedObject mSuccess;
58 10b7e588 Leszek Koltunski
    private GridFlat mGrid;
59 427ab7bf Leszek Koltunski
    private int bmpHeight, bmpWidth;
60 9c5b5cb6 Leszek Koltunski
61 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 9b9440a0 Leszek Koltunski
    CheckRenderer(GLSurfaceView view)
64 427ab7bf Leszek Koltunski
      { 
65
      mView = view;
66 9c5b5cb6 Leszek Koltunski
67 9b9440a0 Leszek Koltunski
      CheckActivity act = (CheckActivity)mView.getContext();
68
69
      Distorted.setMaxVertex(act.getMaxV());
70
      Distorted.setMaxFragment(act.getMaxF());
71 427ab7bf Leszek Koltunski
      }
72
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
   
75
    public void onDrawFrame(GL10 glUnused) 
76
      {
77
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
78 e8b6aa95 Leszek Koltunski
      mSuccess.draw(System.currentTimeMillis(), mGrid);
79 427ab7bf Leszek Koltunski
      }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
    
83
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
84
      { 
85 a8c3ada7 Leszek Koltunski
      mSuccess.abortEffects(EffectTypes.MATRIX);
86 427ab7bf Leszek Koltunski
      
87 5055b5d4 Leszek Koltunski
      if( (float)bmpHeight/bmpWidth > (float)height/width )
88 427ab7bf Leszek Koltunski
        {
89
        int w = (height*bmpWidth)/bmpHeight;
90 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
91
92
        mSuccess.move( new Static3D((width-w)/2,0,0) );
93 f988589e Leszek Koltunski
        mSuccess.scale(factor);
94 427ab7bf Leszek Koltunski
        }
95
      else
96
        {
97
        int h = (width*bmpHeight)/bmpWidth;
98 7589635e Leszek Koltunski
        float factor = (float)width/bmpWidth;
99
100
        mSuccess.move( new Static3D(0,(height-h)/2,0) );
101 f988589e Leszek Koltunski
        mSuccess.scale(factor);
102 427ab7bf Leszek Koltunski
        }
103
      
104
      Distorted.onSurfaceChanged(width, height);
105
      }
106
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
110
      {  
111 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
112
113 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
114
      Bitmap bitmap;
115 9c5b5cb6 Leszek Koltunski
116
      try
117 427ab7bf Leszek Koltunski
        {
118
        bitmap = BitmapFactory.decodeStream(is);
119
        } 
120
      finally 
121
        {
122
        try 
123
          {
124
          is.close();
125
          } 
126
        catch(IOException e) { }
127 9c5b5cb6 Leszek Koltunski
        }
128
129 427ab7bf Leszek Koltunski
      bmpHeight = bitmap.getHeight();
130
      bmpWidth  = bitmap.getWidth();
131 9c5b5cb6 Leszek Koltunski
132 e8b6aa95 Leszek Koltunski
      mSuccess = new DistortedObject(bmpWidth,bmpHeight,1);
133
      mSuccess.setTexture(bitmap);
134 10b7e588 Leszek Koltunski
      mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
135 e8b6aa95 Leszek Koltunski
136 9c5b5cb6 Leszek Koltunski
      // 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 7bf107f7 Leszek Koltunski
142 334c13fa Leszek Koltunski
      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 7bf107f7 Leszek Koltunski
146 b4a73ea5 Leszek Koltunski
      long swirlEffectID = mSuccess.swirl( new Static1D(30), dSwirl, new Static4D( 0,0,40,40) );
147
148
      if( swirlEffectID<0 )
149
        {
150 e7a4ef16 Leszek Koltunski
        Log.e("Check", "Failed to add Swirl effect!");
151 b4a73ea5 Leszek Koltunski
        }
152 7bf107f7 Leszek Koltunski
153 f988589e Leszek Koltunski
      Dynamic3D dDeform = new Dynamic3D(2000,0.0f);
154 7bf107f7 Leszek Koltunski
      dDeform.add(new Static3D( 0,         0,0));
155
      dDeform.add(new Static3D( 0,-bmpHeight,0));
156
157 334c13fa Leszek Koltunski
      long deformEffectID = mSuccess.deform(dDeform, new Static3D(bmpWidth/2,0,0) );
158 b4a73ea5 Leszek Koltunski
159
      if( deformEffectID<0 )
160
        {
161 e7a4ef16 Leszek Koltunski
        Log.e("Check", "Failed to add Deform effect!");
162 b4a73ea5 Leszek Koltunski
        }
163 9c5b5cb6 Leszek Koltunski
164
      // Now try adding 1 Fragment Effect. Likewise, will fail if maxFragmentEffects is <1.
165 7589635e Leszek Koltunski
      Static3D color = new Static3D(1,0,0);
166 f988589e Leszek Koltunski
      Dynamic1D inter = new Dynamic1D(2000,0.0f);
167 7589635e Leszek Koltunski
      inter.add(new Static1D(0));
168
      inter.add(new Static1D(1));
169 9c5b5cb6 Leszek Koltunski
170 b4a73ea5 Leszek Koltunski
      long chromaEffectID = mSuccess.chroma(inter, color);
171
172
      if( chromaEffectID<0 )
173
        {
174 e7a4ef16 Leszek Koltunski
        Log.e("Check", "Failed to add Chroma effect!");
175 b4a73ea5 Leszek Koltunski
        }
176 9c5b5cb6 Leszek Koltunski
177 427ab7bf Leszek Koltunski
      try
178
        {
179 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
180 427ab7bf Leszek Koltunski
        }
181
      catch(Exception ex)
182
        {
183 9c5b5cb6 Leszek Koltunski
        // 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 427ab7bf Leszek Koltunski
        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
}