Project

General

Profile

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

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

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.DistortedScreen;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.DistortedEffects;
34
import org.distorted.library.EffectNames;
35
import org.distorted.library.MeshFlat;
36
import org.distorted.library.EffectTypes;
37
import org.distorted.library.type.Dynamic3D;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

    
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLSurfaceView;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
class DifferentBitmapsRenderer implements GLSurfaceView.Renderer 
49
{
50
   private static final int NUM = 3;
51
   
52
   private GLSurfaceView mView;
53
   private DistortedEffects[] mEffects;
54
   private DistortedTexture[] mTexture;
55
   private DistortedScreen mScreen;
56
   private int bmpHeight, bmpWidth;
57
    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
   DifferentBitmapsRenderer(GLSurfaceView v)
61
      {     
62
      mView = v;
63
     
64
      Dynamic3D dDistort = new Dynamic3D(3000,0.0f);
65
      dDistort.add(new Static3D( 25,0,0));
66
      dDistort.add(new Static3D(-25,0,0));
67
      Static3D mPoint = new Static3D(305, 380, 0);
68

    
69
      mEffects = new DistortedEffects[NUM];
70
      mEffects[0] = new DistortedEffects();
71
      for(int i=1; i<NUM; i++)
72
        mEffects[i] = new DistortedEffects(mEffects[0], Distorted.CLONE_VERTEX|Distorted.CLONE_FRAGMENT);
73

    
74
      // Add the effects only to the first queue - all VERTEX and FRAGMENT effects are shared!
75
      // (Matrix effect cannot be shared as we have to display each Texture in a different location)
76
      mEffects[0].sink( new Static1D(8), mPoint, new Static4D(0,0,80,80));  // enlarge the nose
77
      mEffects[0].distort(dDistort,mPoint);                                 // keep moving the whole bitmap left and right.
78

    
79
      mScreen = new DistortedScreen();
80
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
   private Bitmap readBitmap(int id)
86
     {
87
     InputStream is = mView.getContext().getResources().openRawResource(id);
88
     Bitmap bitmap=null;
89
           
90
     try 
91
       {
92
       bitmap = BitmapFactory.decodeStream(is);
93
       } 
94
     finally 
95
       {
96
       try 
97
         {
98
         is.close();
99
         } 
100
       catch(IOException e) { }
101
       }
102
     
103
     return bitmap;
104
     }
105
   
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
   
108
   public void onDrawFrame(GL10 glUnused)
109
      {
110
      mScreen.render( System.currentTimeMillis() );
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
116
     {
117
     for(int i=NUM-1; i>=0; i--)
118
       {
119
       mEffects[i].abortEffects(EffectTypes.MATRIX);
120
       }
121
      
122
     float qx = (float)width /bmpWidth;
123
     float qy = (float)height/bmpHeight;
124

    
125
     if( qx > NUM*qy )  // screen more 'horizontal' than NUM bitmaps next to each other
126
       {
127
       for(int i=0; i<NUM; i++)
128
         {
129
         mEffects[i].move( new Static3D( qy*((0.5f+i)/NUM-0.5f), 0, 0) );
130
         mEffects[i].scale( new Static3D( qy/qx, 1, 1) );
131
         }
132
       }
133
     else
134
       {
135
       for(int i=0; i<NUM; i++)
136
         {
137
         mEffects[i].move( new Static3D( (0.5f+i)/NUM-0.5f, 0, 0) );
138
         mEffects[i].scale( new Static3D( 1.0f/NUM, (qx/qy)/NUM, 1) );
139
         }
140
       }
141
         
142
     mScreen.resize(width, height);
143
     }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
    
147
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
148
     {
149
     Bitmap bitmap0= readBitmap(R.raw.dog);
150
     Bitmap bitmap1= readBitmap(R.raw.face);
151
     Bitmap bitmap2= readBitmap(R.raw.cat);
152
      
153
     bmpHeight = bitmap0.getHeight();
154
     bmpWidth  = bitmap0.getWidth();
155

    
156
     if( mTexture==null )
157
       {
158
       mTexture = new DistortedTexture[NUM];
159

    
160
       for(int i=0; i<NUM; i++)
161
         mTexture[i] = new DistortedTexture(bmpWidth,bmpHeight);
162
       }
163

    
164
     mTexture[0].setTexture(bitmap0);
165
     mTexture[1].setTexture(bitmap1);
166
     mTexture[2].setTexture(bitmap2);
167

    
168
     MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
169

    
170
     mScreen.detachAll();
171
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture[i], mEffects[i], mesh);
172

    
173
     DistortedEffects.enableEffect(EffectNames.SINK);
174
     DistortedEffects.enableEffect(EffectNames.DISTORT);
175

    
176
     try
177
       {
178
       Distorted.onCreate(mView.getContext());
179
       }
180
     catch(Exception ex)
181
       {
182
       android.util.Log.e("Renderer", ex.getMessage() );
183
       }
184
     }
185
}
(2-2/3)