Project

General

Profile

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

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

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

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

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

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

    
59
   public DifferentBitmapsRenderer(GLSurfaceView v) 
60
      {     
61
      mView = v;
62
     
63
      // create shared effects - enlarge the nose and keep moving the whole bitmap left and right.
64
      dDistort = new Dynamic3D(3000,0.0f);
65
      vec = new Static3D[2];
66
      vec[0] = new Static3D( 25,0,0);
67
      vec[1] = new Static3D(-25,0,0);
68
      dDistort.add(vec[0]);
69
      dDistort.add(vec[1]);
70
      point = new Static2D(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
        bmp[i].abortEffects(EffectTypes.MATRIX);
115
        }
116
      
117
      if( bmpHeight/(NUM*bmpWidth) > height/width )
118
        {
119
        int w = (height*bmpWidth)/bmpHeight;
120
        float factor = (float)height/bmpHeight;
121

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

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

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