Project

General

Profile

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

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

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.Dynamic3D;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static2D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
40
import android.graphics.Bitmap;
41
import android.graphics.BitmapFactory;
42
import android.opengl.GLES20;
43
import android.opengl.GLSurfaceView;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class DifferentBitmapsRenderer implements GLSurfaceView.Renderer 
48
{
49
   private static final int NUM = 3;
50
   
51
   private GLSurfaceView mView;
52
   private DistortedBitmap[] bmp;
53
   private Static2D point;
54
   private Dynamic3D dDistort;
55
   private Static3D[] vec;
56
   private int bmpHeight, bmpWidth;
57
    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

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

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

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

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

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