Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ a8c3ada7

1 427ab7bf Leszek Koltunski
2 5068fa06 Leszek Koltunski
package org.distorted.examples.differenteffects;
3 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
11 427ab7bf Leszek Koltunski
12 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
13
import org.distorted.library.DistortedBitmap;
14 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
15 5068fa06 Leszek Koltunski
import org.distorted.library.Float2D;
16
import org.distorted.library.Float3D;
17
import org.distorted.library.Float4D;
18
import org.distorted.library.Interpolator3D;
19 427ab7bf Leszek Koltunski
20
import android.graphics.Bitmap;
21
import android.graphics.BitmapFactory;
22
import android.opengl.GLES20;
23
import android.opengl.GLSurfaceView;
24
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26
27
class DifferentEffectsRenderer implements GLSurfaceView.Renderer 
28
{
29
   private static final int NUM = 3;
30
   
31
   private GLSurfaceView mView;
32
   private DistortedBitmap[] bmp;
33
   private Float2D pLeft, pRight, pNose1;
34
   private Float4D RegionEye;
35
   private Interpolator3D mDI;
36
   private Float3D[] vec;
37
   private int bmpHeight, bmpWidth;
38
    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41
   public DifferentEffectsRenderer(GLSurfaceView v) 
42
      {     
43
      mView = v;
44
      
45
      // bmp[0] effects
46
      pLeft = new Float2D(214, 206);
47
      pRight= new Float2D(390, 212);
48
      RegionEye = new Float4D(0,0,60,60);
49
      
50
      // bmp[1] effects
51
      mDI = new Interpolator3D();
52
      mDI.setCount(0.0f);
53
      mDI.setDuration(1000);
54
      vec = new Float3D[2];
55
      vec[0] = new Float3D( 50,0,0);
56
      vec[1] = new Float3D(-50,0,0);
57
      mDI.add(vec[0]);
58
      mDI.add(vec[1]);
59
      pNose1 = new Float2D(305, 340);
60
      
61
      // we don't need to prepare anything for bmp[2] effects
62
      }
63
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
   
66
    public void onDrawFrame(GL10 glUnused) 
67
      {
68
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
69
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
70
      
71
      long time = System.currentTimeMillis();
72
   
73
      for(int i=NUM-1; i>=0; i--) bmp[i].draw(time);
74
      }
75
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
    
78
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
79
      { 
80
      for(int i=NUM-1; i>=0; i--) 
81
        {   
82 a8c3ada7 Leszek Koltunski
        bmp[i].abortEffects(EffectTypes.MATRIX);
83 427ab7bf Leszek Koltunski
        }
84
      
85
      if( bmpHeight/(NUM*bmpWidth) > height/width )
86
        {
87
        int w = (height*bmpWidth)/bmpHeight;
88
      
89
        for(int i=NUM-1; i>=0; i--) 
90
          {
91
          bmp[i].move( (width-NUM*w)/2 +i*w ,0, 0);
92
          bmp[i].scale((float)height/bmpHeight);
93
          }
94
        }
95
      else
96
        {
97
        int w = width/NUM;  
98
        int h = (width*bmpHeight)/(bmpWidth*NUM);
99
      
100
        for(int i=NUM-1; i>=0; i--) 
101
          {
102
          bmp[i].move(i*w, (height-h)/2, 0);  
103
          bmp[i].scale((float)width/(bmpWidth*NUM));
104
          }
105
        }
106
       
107
      Distorted.onSurfaceChanged(width, height); 
108
      }
109
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
113
      {
114
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
115
      Bitmap bitmap;
116
        
117
      try 
118
        {
119
        bitmap = BitmapFactory.decodeStream(is);
120
        } 
121
      finally 
122
        {
123
        try 
124
          {
125
          is.close();
126
          } 
127
        catch(IOException e) { }
128
        }  
129
      
130
      bmpHeight = bitmap.getHeight();
131
      bmpWidth  = bitmap.getWidth();
132
      
133
      bmp = new DistortedBitmap[NUM];
134
      bmp[0] = new DistortedBitmap(bmpWidth, bmpHeight, 30);
135
      
136
      for(int i=1; i<NUM; i++) bmp[i] = new DistortedBitmap(bmp[0], Distorted.CLONE_BITMAP);
137
      
138
      // setting the bitmap once is enough; others are cloned!
139
      bmp[0].setBitmap(bitmap);
140
         
141
      bmp[0].sink(10.0f, RegionEye, pLeft , 2000, 0.0f);
142
      bmp[0].sink(10.0f, RegionEye, pRight, 2000, 0.0f);
143
      bmp[1].distort(mDI, pNose1);
144
      bmp[2].macroblock(50, 3000, 0.0f);
145
      
146
      try
147
        {
148 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
149 427ab7bf Leszek Koltunski
        }
150
      catch(Exception ex)
151
        {
152 ed1c0b33 Leszek Koltunski
        android.util.Log.e("DifferentEffects", ex.getMessage() );
153 427ab7bf Leszek Koltunski
        }
154
      }
155
}