Project

General

Profile

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

examples / src / main / java / org / distortedandroid / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 427ab7bf

1

    
2
package org.distortedandroid.examples.differentbitmaps;
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.distortedandroid.examples.R;
11

    
12
import org.distortedandroid.library.Distorted;
13
import org.distortedandroid.library.DistortedBitmap;
14
import org.distortedandroid.library.Float2D;
15
import org.distortedandroid.library.Float4D;
16
import org.distortedandroid.library.Interpolator2D;
17

    
18
import android.graphics.Bitmap;
19
import android.graphics.BitmapFactory;
20
import android.opengl.GLES20;
21
import android.opengl.GLSurfaceView;
22

    
23
///////////////////////////////////////////////////////////////////////////////////////////////////
24

    
25
class DifferentBitmapsRenderer implements GLSurfaceView.Renderer 
26
{
27
   private static final int NUM = 3;
28
   
29
   private GLSurfaceView mView;
30
   private DistortedBitmap[] bmp;
31
   private Float2D point;
32
   private Interpolator2D di;
33
   private Float2D[] vec;
34
   private int bmpHeight, bmpWidth;
35
    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
   public DifferentBitmapsRenderer(GLSurfaceView v) 
39
      {     
40
      mView = v;
41
     
42
      // create shared effects - enlarge the nose and keep moving the whole bitmap left and right.
43
      di = new Interpolator2D();
44
      di.setCount(0.0f);
45
      di.setDuration(3000);
46
      vec = new Float2D[2];
47
      vec[0] = new Float2D( 25,0);
48
      vec[1] = new Float2D(-25,0);
49
      di.add(vec[0]);
50
      di.add(vec[1]);
51
      point = new Float2D(305, 380);
52
      }
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
   private Bitmap readBitmap(int id)
57
     {
58
     InputStream is = mView.getContext().getResources().openRawResource(id);
59
     Bitmap bitmap=null;
60
           
61
     try 
62
       {
63
       bitmap = BitmapFactory.decodeStream(is);
64
       } 
65
     finally 
66
       {
67
       try 
68
         {
69
         is.close();
70
         } 
71
       catch(IOException e) { }
72
       }
73
     
74
     return bitmap;
75
     }
76
   
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
   
79
    public void onDrawFrame(GL10 glUnused) 
80
      {
81
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
82
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
83
      
84
      long time = System.currentTimeMillis();
85
      
86
      for(int i=NUM-1; i>=0; i--) bmp[i].draw(time); 
87
      }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
    
91
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
92
      {  
93
      for(int i=NUM-1; i>=0; i--) 
94
        {   
95
        bmp[i].abortAllEffects(Distorted.TYPE_MATR);  
96
        }
97
      
98
      if( bmpHeight/(NUM*bmpWidth) > height/width )
99
        {
100
        int w = (height*bmpWidth)/bmpHeight;
101
      
102
        for(int i=NUM-1; i>=0; i--) 
103
          {
104
          bmp[i].move( (width-NUM*w)/2 +i*w ,0,0);
105
          bmp[i].scale((float)height/bmpHeight);
106
          }
107
        }
108
      else
109
        {
110
        int w = width/NUM;  
111
        int h = (width*bmpHeight)/(bmpWidth*NUM);
112
      
113
        for(int i=NUM-1; i>=0; i--) 
114
          {
115
          bmp[i].move(i*w, (height-h)/2, 0);  
116
          bmp[i].scale((float)width/(bmpWidth*NUM));
117
          }
118
        }
119
         
120
      Distorted.onSurfaceChanged(width, height); 
121
      }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
    
125
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
126
      {  
127
      Bitmap bitmap0= readBitmap(R.raw.dog);
128
      Bitmap bitmap1= readBitmap(R.raw.face);
129
      Bitmap bitmap2= readBitmap(R.raw.cat);
130
      
131
      bmpHeight = bitmap0.getHeight();
132
      bmpWidth  = bitmap0.getWidth();
133
      
134
      // create NUM DistortedBitmaps with shared effects
135
      bmp = new DistortedBitmap[NUM];
136
      bmp[0] = new DistortedBitmap(bmpWidth, bmpHeight, 30);
137
      for(int i=1; i<NUM; i++) 
138
      bmp[i] = new DistortedBitmap(bmp[0], Distorted.CLONE_VERTEX|Distorted.CLONE_FRAGMENT);
139
      
140
      bmp[0].setBitmap(bitmap0);
141
      bmp[1].setBitmap(bitmap1);
142
      bmp[2].setBitmap(bitmap2);
143
         
144
      bmp[0].sink(8.0f, new Float4D(0,0,80,80), point, 0, 0.5f);
145
      bmp[0].distort(di,point); 
146
      
147
      try
148
        {
149
        Distorted.onSurfaceCreated(mView);
150
        }
151
      catch(Exception ex)
152
        {
153
        android.util.Log.e("Renderer", ex.getMessage() );
154
        }
155
      }
156
}
(2-2/3)