Project

General

Profile

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

examples / src / main / java / org / distorted / examples / check / CheckRenderer.java @ 95593730

1

    
2
package org.distorted.examples.check;
3

    
4
import java.io.IOException;
5
import java.io.InputStream;
6

    
7
import javax.microedition.khronos.egl.EGLConfig;
8
import javax.microedition.khronos.opengles.GL10;
9

    
10
import org.distorted.examples.R;
11

    
12
import org.distorted.library.Distorted;
13
import org.distorted.library.DistortedBitmap;
14
import org.distorted.library.EffectTypes;
15
import org.distorted.library.Float2D;
16
import org.distorted.library.Float3D;
17

    
18
import android.app.AlertDialog;
19
import android.content.Context;
20
import android.content.DialogInterface;
21
import android.graphics.Bitmap;
22
import android.graphics.BitmapFactory;
23
import android.opengl.GLES20;
24
import android.opengl.GLSurfaceView;
25

    
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

    
28
class CheckRenderer implements GLSurfaceView.Renderer 
29
{
30
    private GLSurfaceView mView;
31
    private DistortedBitmap mSuccess;
32
    private Float2D pUp, pDown;
33
    private Float3D vUp, vDown;
34
    
35
    private int bmpHeight, bmpWidth;
36
    
37
    public static String compilationResult;
38
    public static String compilationTitle;
39
    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
    public CheckRenderer(GLSurfaceView view) 
43
      { 
44
      mView = view;
45
    
46
      Distorted.setMaxVertex(CheckActivity.getMaxV());
47
      Distorted.setMaxFragment(CheckActivity.getMaxF());
48
      }
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51
   
52
    public void onDrawFrame(GL10 glUnused) 
53
      {
54
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
55
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
56
    
57
      mSuccess.draw(System.currentTimeMillis());
58
      }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
    
62
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
63
      { 
64
      mSuccess.abortAllEffects(EffectTypes.MATRIX.type);
65
      
66
      if( bmpHeight/bmpWidth > height/width )
67
        {
68
        int w = (height*bmpWidth)/bmpHeight;
69
        mSuccess.move((width-w)/2 ,0, 0);
70
        mSuccess.scale((float)height/bmpHeight);
71
        }
72
      else
73
        {
74
        int h = (width*bmpHeight)/bmpWidth;
75
        mSuccess.move(0 ,(height-h)/2, 0);
76
        mSuccess.scale((float)width/bmpWidth);
77
        }
78
      
79
      Distorted.onSurfaceChanged(width, height);
80
      }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
    
84
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
85
      {  
86
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.success);
87
      Bitmap bitmap;
88
        
89
      try 
90
        {
91
        bitmap = BitmapFactory.decodeStream(is);
92
        } 
93
      finally 
94
        {
95
        try 
96
          {
97
          is.close();
98
          } 
99
        catch(IOException e) { }
100
        }  
101
      
102
      bmpHeight = bitmap.getHeight();
103
      bmpWidth  = bitmap.getWidth();
104
       
105
      pDown = new Float2D(bmpWidth/2,        0);
106
      pUp   = new Float2D(bmpWidth/2,bmpHeight);
107
      
108
      vUp   = new Float3D( 0,+bmpHeight,0);
109
      vDown = new Float3D( 0,-bmpHeight,0);
110
      
111
      mSuccess = new DistortedBitmap(bitmap, 30);  
112
      mSuccess.deform(  vUp,   pUp, 2000, 0.0f);
113
      mSuccess.deform(vDown, pDown, 2000, 0.0f);
114
      
115
      try
116
        {
117
        Distorted.onSurfaceCreated(mView.getContext());
118
        }
119
      catch(Exception ex)
120
        {
121
        compilationTitle = ex.getClass().getSimpleName();
122
        compilationResult= ex.getMessage();
123
      
124
        String ver;
125
      
126
        ver = GLES20.glGetString(GLES20.GL_SHADING_LANGUAGE_VERSION);   
127
        compilationResult += "\n\nGLSL version: "+(ver==null ? "null" : ver);
128
        ver = GLES20.glGetString(GLES20.GL_VERSION);  
129
        compilationResult += "\nGL version: "+(ver==null ? "null" : ver);
130
        ver = GLES20.glGetString(GLES20.GL_VENDOR);   
131
        compilationResult += "\nGL vendor: "+(ver==null ? "null" : ver);
132
        ver = GLES20.glGetString(GLES20.GL_RENDERER);    
133
        compilationResult += "\nGL renderer: "+(ver==null ? "null" : ver);
134
      
135
        CheckActivity act = (CheckActivity)mView.getContext();
136
      
137
        act.runOnUiThread(new Runnable() 
138
          {
139
          public void run() 
140
            {
141
            Context context = mView.getContext();
142
      
143
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
144
            builder.setTitle(compilationTitle);
145
            builder.setMessage(compilationResult);
146
            builder.setCancelable(true);
147
           
148
            builder.setOnCancelListener(new DialogInterface.OnCancelListener() 
149
              {
150
              @Override
151
              public void onCancel(DialogInterface dialog) 
152
                {
153
                ((CheckActivity)mView.getContext()).finish();
154
                }
155
              });
156
            
157
            AlertDialog alert = builder.create();
158
            alert.show();
159
            }
160
          });
161
        }
162
      }
163
    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
    
166
}
(2-2/3)