Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 89a0d841

1

    
2
package org.distorted.examples.differenteffects;
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.Float2D;
15
import org.distorted.library.Float3D;
16
import org.distorted.library.Float4D;
17
import org.distorted.library.Interpolator3D;
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 DifferentEffectsRenderer implements GLSurfaceView.Renderer 
27
{
28
   private static final int NUM = 3;
29
   
30
   private GLSurfaceView mView;
31
   private DistortedBitmap[] bmp;
32
   private Float2D pLeft, pRight, pNose1;
33
   private Float4D RegionEye;
34
   private Interpolator3D mDI;
35
   private Float3D[] vec;
36
   private int bmpHeight, bmpWidth;
37
    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
   public DifferentEffectsRenderer(GLSurfaceView v) 
41
      {     
42
      mView = v;
43
      
44
      // bmp[0] effects
45
      pLeft = new Float2D(214, 206);
46
      pRight= new Float2D(390, 212);
47
      RegionEye = new Float4D(0,0,60,60);
48
      
49
      // bmp[1] effects
50
      mDI = new Interpolator3D();
51
      mDI.setCount(0.0f);
52
      mDI.setDuration(1000);
53
      vec = new Float3D[2];
54
      vec[0] = new Float3D( 50,0,0);
55
      vec[1] = new Float3D(-50,0,0);
56
      mDI.add(vec[0]);
57
      mDI.add(vec[1]);
58
      pNose1 = new Float2D(305, 340);
59
      
60
      // we don't need to prepare anything for bmp[2] effects
61
      }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
   
65
    public void onDrawFrame(GL10 glUnused) 
66
      {
67
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
68
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
69
      
70
      long time = System.currentTimeMillis();
71
   
72
      for(int i=NUM-1; i>=0; i--) bmp[i].draw(time);
73
      }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
    
77
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
78
      { 
79
      for(int i=NUM-1; i>=0; i--) 
80
        {   
81
        bmp[i].abortAllEffects(Distorted.TYPE_PRE);
82
        }
83
      
84
      if( bmpHeight/(NUM*bmpWidth) > height/width )
85
        {
86
        int w = (height*bmpWidth)/bmpHeight;
87
      
88
        for(int i=NUM-1; i>=0; i--) 
89
          {
90
          bmp[i].move( (width-NUM*w)/2 +i*w ,0, 0);
91
          bmp[i].scale((float)height/bmpHeight);
92
          }
93
        }
94
      else
95
        {
96
        int w = width/NUM;  
97
        int h = (width*bmpHeight)/(bmpWidth*NUM);
98
      
99
        for(int i=NUM-1; i>=0; i--) 
100
          {
101
          bmp[i].move(i*w, (height-h)/2, 0);  
102
          bmp[i].scale((float)width/(bmpWidth*NUM));
103
          }
104
        }
105
       
106
      Distorted.onSurfaceChanged(width, height); 
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
    
111
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
112
      {
113
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
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
      bmp = new DistortedBitmap[NUM];
133
      bmp[0] = new DistortedBitmap(bmpWidth, bmpHeight, 30);
134
      
135
      for(int i=1; i<NUM; i++) bmp[i] = new DistortedBitmap(bmp[0], Distorted.CLONE_BITMAP);
136
      
137
      // setting the bitmap once is enough; others are cloned!
138
      bmp[0].setBitmap(bitmap);
139
         
140
      bmp[0].sink(10.0f, RegionEye, pLeft , 2000, 0.0f);
141
      bmp[0].sink(10.0f, RegionEye, pRight, 2000, 0.0f);
142
      bmp[1].distort(mDI, pNose1);
143
      bmp[2].macroblock(50, 3000, 0.0f);
144
      
145
      try
146
        {
147
        Distorted.onSurfaceCreated(mView.getContext());
148
        }
149
      catch(Exception ex)
150
        {
151
        android.util.Log.e("DifferentEffects", ex.getMessage() );
152
        }
153
      }
154
}
(2-2/3)