Project

General

Profile

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

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

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 mPoint;
53
   private Dynamic3D dDistort;
54
   private int bmpHeight, bmpWidth;
55
    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
   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
      dDistort = new Dynamic3D(3000,0.0f);
64
      dDistort.add(new Static3D( 25,0,0));
65
      dDistort.add(new Static3D(-25,0,0));
66
      mPoint = new Static2D(305, 380);
67
      }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

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

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
    
106
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
107
      {  
108
      for(int i=NUM-1; i>=0; i--) 
109
        {   
110
        bmp[i].abortEffects(EffectTypes.MATRIX);
111
        }
112
      
113
      if( bmpHeight/(NUM*bmpWidth) > height/width )
114
        {
115
        int w = (height*bmpWidth)/bmpHeight;
116
        float factor = (float)height/bmpHeight;
117

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

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

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