Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 08eabc44

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.examples.differenteffects;
21

    
22
import java.io.IOException;
23
import java.io.InputStream;
24

    
25
import javax.microedition.khronos.egl.EGLConfig;
26
import javax.microedition.khronos.opengles.GL10;
27

    
28
import org.distorted.examples.R;
29

    
30
import org.distorted.library.Distorted;
31
import org.distorted.library.DistortedBitmap;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.type.Float2D;
34
import org.distorted.library.type.Float3D;
35
import org.distorted.library.type.Float4D;
36
import org.distorted.library.type.Interpolator3D;
37

    
38
import android.graphics.Bitmap;
39
import android.graphics.BitmapFactory;
40
import android.opengl.GLES20;
41
import android.opengl.GLSurfaceView;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

    
45
class DifferentEffectsRenderer implements GLSurfaceView.Renderer 
46
{
47
   private static final int NUM = 3;
48
   
49
   private GLSurfaceView mView;
50
   private DistortedBitmap[] bmp;
51
   private Float2D pLeft, pRight, pNose1;
52
   private Float4D RegionEye;
53
   private Interpolator3D mDI;
54
   private Float3D[] vec;
55
   private int bmpHeight, bmpWidth;
56
    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
   public DifferentEffectsRenderer(GLSurfaceView v) 
60
      {     
61
      mView = v;
62
      
63
      // bmp[0] effects
64
      pLeft = new Float2D(214, 206);
65
      pRight= new Float2D(390, 212);
66
      RegionEye = new Float4D(0,0,60,60);
67
      
68
      // bmp[1] effects
69
      mDI = new Interpolator3D();
70
      mDI.setCount(0.0f);
71
      mDI.setDuration(1000);
72
      vec = new Float3D[2];
73
      vec[0] = new Float3D( 50,0,0);
74
      vec[1] = new Float3D(-50,0,0);
75
      mDI.add(vec[0]);
76
      mDI.add(vec[1]);
77
      pNose1 = new Float2D(305, 340);
78
      
79
      // we don't need to prepare anything for bmp[2] effects
80
      }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
   
84
    public void onDrawFrame(GL10 glUnused) 
85
      {
86
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
87
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
88
      
89
      long time = System.currentTimeMillis();
90
   
91
      for(int i=NUM-1; i>=0; i--) bmp[i].draw(time);
92
      }
93

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

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
    
130
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
131
      {
132
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
133
      Bitmap bitmap;
134
        
135
      try 
136
        {
137
        bitmap = BitmapFactory.decodeStream(is);
138
        } 
139
      finally 
140
        {
141
        try 
142
          {
143
          is.close();
144
          } 
145
        catch(IOException e) { }
146
        }  
147
      
148
      bmpHeight = bitmap.getHeight();
149
      bmpWidth  = bitmap.getWidth();
150
      
151
      bmp = new DistortedBitmap[NUM];
152
      bmp[0] = new DistortedBitmap(bmpWidth, bmpHeight, 30);
153
      
154
      for(int i=1; i<NUM; i++) bmp[i] = new DistortedBitmap(bmp[0], Distorted.CLONE_BITMAP);
155
      
156
      // setting the bitmap once is enough; others are cloned!
157
      bmp[0].setBitmap(bitmap);
158
         
159
      bmp[0].sink(10.0f, RegionEye, pLeft , 2000, 0.0f);
160
      bmp[0].sink(10.0f, RegionEye, pRight, 2000, 0.0f);
161
      bmp[1].distort(mDI, pNose1);
162
      bmp[2].macroblock(50, 3000, 0.0f);
163
      
164
      try
165
        {
166
        Distorted.onSurfaceCreated(mView.getContext());
167
        }
168
      catch(Exception ex)
169
        {
170
        android.util.Log.e("DifferentEffects", ex.getMessage() );
171
        }
172
      }
173
}
(2-2/3)