Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 7589635e

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.differentbitmaps;
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.Dynamic2D;
34
import org.distorted.library.type.Static2D;
35
import org.distorted.library.type.Static3D;
36
import org.distorted.library.type.Static4D;
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 DifferentBitmapsRenderer implements GLSurfaceView.Renderer 
46
{
47
   private static final int NUM = 3;
48
   
49
   private GLSurfaceView mView;
50
   private DistortedBitmap[] bmp;
51
   private Static2D point;
52
   private Dynamic2D di;
53
   private Static2D[] vec;
54
   private int bmpHeight, bmpWidth;
55
    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
   public DifferentBitmapsRenderer(GLSurfaceView v) 
59
      {     
60
      mView = v;
61
     
62
      // create shared effects - enlarge the nose and keep moving the whole bitmap left and right.
63
      di = new Dynamic2D();
64
      di.setCount(0.0f);
65
      di.setDuration(3000);
66
      vec = new Static2D[2];
67
      vec[0] = new Static2D( 25,0);
68
      vec[1] = new Static2D(-25,0);
69
      di.add(vec[0]);
70
      di.add(vec[1]);
71
      point = new Static2D(305, 380);
72
      }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
   private Bitmap readBitmap(int id)
77
     {
78
     InputStream is = mView.getContext().getResources().openRawResource(id);
79
     Bitmap bitmap=null;
80
           
81
     try 
82
       {
83
       bitmap = BitmapFactory.decodeStream(is);
84
       } 
85
     finally 
86
       {
87
       try 
88
         {
89
         is.close();
90
         } 
91
       catch(IOException e) { }
92
       }
93
     
94
     return bitmap;
95
     }
96
   
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
   
99
    public void onDrawFrame(GL10 glUnused) 
100
      {
101
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
102
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
103
      
104
      long time = System.currentTimeMillis();
105
      
106
      for(int i=NUM-1; i>=0; i--) bmp[i].draw(time); 
107
      }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
    
111
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
112
      {  
113
      for(int i=NUM-1; i>=0; i--) 
114
        {   
115
        bmp[i].abortEffects(EffectTypes.MATRIX);
116
        }
117
      
118
      if( bmpHeight/(NUM*bmpWidth) > height/width )
119
        {
120
        int w = (height*bmpWidth)/bmpHeight;
121
        float factor = (float)height/bmpHeight;
122
        Static3D scale = new Static3D(factor,factor,factor);
123

    
124
        for(int i=NUM-1; i>=0; i--) 
125
          {
126
          bmp[i].move( new Static3D((width-NUM*w)/2 +i*w ,0,0) );
127
          bmp[i].scale(scale);
128
          }
129
        }
130
      else
131
        {
132
        int w = width/NUM;  
133
        int h = (width*bmpHeight)/(bmpWidth*NUM);
134
        float factor = (float)width/(bmpWidth*NUM);
135
        Static3D scale = new Static3D(factor,factor,factor);
136

    
137
        for(int i=NUM-1; i>=0; i--) 
138
          {
139
          bmp[i].move( new Static3D(i*w,(height-h)/2,0) );
140
          bmp[i].scale(scale);
141
          }
142
        }
143
         
144
      Distorted.onSurfaceChanged(width, height); 
145
      }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
    
149
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
150
      {  
151
      Bitmap bitmap0= readBitmap(R.raw.dog);
152
      Bitmap bitmap1= readBitmap(R.raw.face);
153
      Bitmap bitmap2= readBitmap(R.raw.cat);
154
      
155
      bmpHeight = bitmap0.getHeight();
156
      bmpWidth  = bitmap0.getWidth();
157
      
158
      // create NUM DistortedBitmaps with shared effects
159
      bmp = new DistortedBitmap[NUM];
160
      bmp[0] = new DistortedBitmap(bmpWidth, bmpHeight, 30);
161
      for(int i=1; i<NUM; i++) 
162
      bmp[i] = new DistortedBitmap(bmp[0], Distorted.CLONE_VERTEX|Distorted.CLONE_FRAGMENT);
163
      
164
      bmp[0].setBitmap(bitmap0);
165
      bmp[1].setBitmap(bitmap1);
166
      bmp[2].setBitmap(bitmap2);
167
         
168
      bmp[0].sink(8.0f, new Static4D(0,0,80,80), point, 0, 0.5f);
169
      bmp[0].distort(di,point); 
170
      
171
      try
172
        {
173
        Distorted.onSurfaceCreated(mView.getContext());
174
        }
175
      catch(Exception ex)
176
        {
177
        android.util.Log.e("Renderer", ex.getMessage() );
178
        }
179
      }
180
}
(2-2/3)