Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 5055b5d4

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 Static3D 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 Static3D(305, 380, 0);
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.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
97
      
98
      long time = System.currentTimeMillis();
99
      
100
      for(int i=NUM-1; i>=0; i--) bmp[i].draw(time); 
101
      }
102

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

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

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

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
    
141
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
142
      {
143
      GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
144

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