Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.differentbitmaps;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
31
import org.distorted.library.DistortedBitmap;
32 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
33 5068fa06 Leszek Koltunski
import org.distorted.library.Float2D;
34
import org.distorted.library.Float4D;
35
import org.distorted.library.Interpolator2D;
36 427ab7bf Leszek Koltunski
37
import android.graphics.Bitmap;
38
import android.graphics.BitmapFactory;
39
import android.opengl.GLES20;
40
import android.opengl.GLSurfaceView;
41
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44
class DifferentBitmapsRenderer implements GLSurfaceView.Renderer 
45
{
46
   private static final int NUM = 3;
47
   
48
   private GLSurfaceView mView;
49
   private DistortedBitmap[] bmp;
50
   private Float2D point;
51
   private Interpolator2D di;
52
   private Float2D[] vec;
53
   private int bmpHeight, bmpWidth;
54
    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57
   public DifferentBitmapsRenderer(GLSurfaceView v) 
58
      {     
59
      mView = v;
60
     
61
      // create shared effects - enlarge the nose and keep moving the whole bitmap left and right.
62
      di = new Interpolator2D();
63
      di.setCount(0.0f);
64
      di.setDuration(3000);
65
      vec = new Float2D[2];
66
      vec[0] = new Float2D( 25,0);
67
      vec[1] = new Float2D(-25,0);
68
      di.add(vec[0]);
69
      di.add(vec[1]);
70
      point = new Float2D(305, 380);
71
      }
72
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75
   private Bitmap readBitmap(int id)
76
     {
77
     InputStream is = mView.getContext().getResources().openRawResource(id);
78
     Bitmap bitmap=null;
79
           
80
     try 
81
       {
82
       bitmap = BitmapFactory.decodeStream(is);
83
       } 
84
     finally 
85
       {
86
       try 
87
         {
88
         is.close();
89
         } 
90
       catch(IOException e) { }
91
       }
92
     
93
     return bitmap;
94
     }
95
   
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
   
98
    public void onDrawFrame(GL10 glUnused) 
99
      {
100
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
101
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
102
      
103
      long time = System.currentTimeMillis();
104
      
105
      for(int i=NUM-1; i>=0; i--) bmp[i].draw(time); 
106
      }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
111
      {  
112
      for(int i=NUM-1; i>=0; i--) 
113
        {   
114 a8c3ada7 Leszek Koltunski
        bmp[i].abortEffects(EffectTypes.MATRIX);
115 427ab7bf Leszek Koltunski
        }
116
      
117
      if( bmpHeight/(NUM*bmpWidth) > height/width )
118
        {
119
        int w = (height*bmpWidth)/bmpHeight;
120
      
121
        for(int i=NUM-1; i>=0; i--) 
122
          {
123
          bmp[i].move( (width-NUM*w)/2 +i*w ,0,0);
124
          bmp[i].scale((float)height/bmpHeight);
125
          }
126
        }
127
      else
128
        {
129
        int w = width/NUM;  
130
        int h = (width*bmpHeight)/(bmpWidth*NUM);
131
      
132
        for(int i=NUM-1; i>=0; i--) 
133
          {
134
          bmp[i].move(i*w, (height-h)/2, 0);  
135
          bmp[i].scale((float)width/(bmpWidth*NUM));
136
          }
137
        }
138
         
139
      Distorted.onSurfaceChanged(width, height); 
140
      }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
    
144
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
145
      {  
146
      Bitmap bitmap0= readBitmap(R.raw.dog);
147
      Bitmap bitmap1= readBitmap(R.raw.face);
148
      Bitmap bitmap2= readBitmap(R.raw.cat);
149
      
150
      bmpHeight = bitmap0.getHeight();
151
      bmpWidth  = bitmap0.getWidth();
152
      
153
      // create NUM DistortedBitmaps with shared effects
154
      bmp = new DistortedBitmap[NUM];
155
      bmp[0] = new DistortedBitmap(bmpWidth, bmpHeight, 30);
156
      for(int i=1; i<NUM; i++) 
157
      bmp[i] = new DistortedBitmap(bmp[0], Distorted.CLONE_VERTEX|Distorted.CLONE_FRAGMENT);
158
      
159
      bmp[0].setBitmap(bitmap0);
160
      bmp[1].setBitmap(bitmap1);
161
      bmp[2].setBitmap(bitmap2);
162
         
163
      bmp[0].sink(8.0f, new Float4D(0,0,80,80), point, 0, 0.5f);
164
      bmp[0].distort(di,point); 
165
      
166
      try
167
        {
168 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
169 427ab7bf Leszek Koltunski
        }
170
      catch(Exception ex)
171
        {
172
        android.util.Log.e("Renderer", ex.getMessage() );
173
        }
174
      }
175
}