Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 10b7e588

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.DistortedObject;
32
import org.distorted.library.GridFlat;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic3D;
35
import org.distorted.library.type.Static1D;
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 DistortedObject[] mObj;
52
   private GridFlat mGrid;
53
   private Static3D mPoint;
54
   private Dynamic3D dDistort;
55
   private int bmpHeight, bmpWidth;
56
    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
   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
      dDistort.add(new Static3D( 25,0,0));
66
      dDistort.add(new Static3D(-25,0,0));
67
      mPoint = new Static3D(305, 380, 0);
68
      }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
   private Bitmap readBitmap(int id)
73
     {
74
     InputStream is = mView.getContext().getResources().openRawResource(id);
75
     Bitmap bitmap=null;
76
           
77
     try 
78
       {
79
       bitmap = BitmapFactory.decodeStream(is);
80
       } 
81
     finally 
82
       {
83
       try 
84
         {
85
         is.close();
86
         } 
87
       catch(IOException e) { }
88
       }
89
     
90
     return bitmap;
91
     }
92
   
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
   
95
    public void onDrawFrame(GL10 glUnused) 
96
      {
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--) mObj[i].draw(time, mGrid);
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
        mObj[i].abortEffects(EffectTypes.MATRIX);
111
        }
112
      
113
      if( (float)bmpHeight/(NUM*bmpWidth) > (float)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
          mObj[i].move( new Static3D((width-NUM*w)/2 +i*w ,0,0) );
121
          mObj[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
          mObj[i].move( new Static3D(i*w,(height-h)/2,0) );
133
          mObj[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
      GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
145

    
146
      Bitmap bitmap0= readBitmap(R.raw.dog);
147
      Bitmap bitmap1= readBitmap(R.raw.face);
148
      Bitmap bitmap2= readBitmap(R.raw.cat);
149
      
150
      bmpHeight = bitmap0.getHeight();
151
      bmpWidth  = bitmap0.getWidth();
152
      
153
      // create NUM DistortedBitmaps with shared effects
154
      mObj = new DistortedObject[NUM];
155
      mObj[0] = new DistortedObject(bmpWidth, bmpHeight, 1);
156
      for(int i=1; i<NUM; i++) 
157
        mObj[i] = new DistortedObject(mObj[0], Distorted.CLONE_VERTEX|Distorted.CLONE_FRAGMENT);
158
      
159
      mObj[0].setTexture(bitmap0);
160
      mObj[1].setTexture(bitmap1);
161
      mObj[2].setTexture(bitmap2);
162
         
163
      mObj[0].sink( new Static1D(8), mPoint, new Static4D(0,0,80,80));
164
      mObj[0].distort(dDistort,mPoint);
165

    
166
      mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
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)