Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ a8c3ada7

1

    
2
package org.distorted.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.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.Float4D;
17
import org.distorted.library.Interpolator2D;
18

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

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25

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

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

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

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

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