Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 630703d1

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
      if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
123
        {
124
        int w = (height*bmpWidth)/bmpHeight;
125
        float factor = (float)height/bmpHeight;
126

    
127
        for(int i=NUM-1; i>=0; i--) 
128
          {
129
          mEffects[i].move( new Static3D((width-NUM*w)/2 +i*w ,0,0) );
130
          mEffects[i].scale(factor);
131
          }
132
        }
133
      else
134
        {
135
        int w = width/NUM;  
136
        int h = (width*bmpHeight)/(bmpWidth*NUM);
137
        float factor = (float)width/(bmpWidth*NUM);
138

    
139
        for(int i=NUM-1; i>=0; i--) 
140
          {
141
          mEffects[i].move( new Static3D(i*w,(height-h)/2,0) );
142
          mEffects[i].scale(factor);
143
          }
144
        }
145
         
146
      mScreen.resize(width, height);
147
      }
148

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

    
160
      if( mTexture==null )
161
        {
162
        mTexture = new DistortedTexture[NUM];
163

    
164
        for(int i=0; i<NUM; i++)
165
          mTexture[i] = new DistortedTexture(bmpWidth,bmpHeight);
166
        }
167

    
168
      mTexture[0].setTexture(bitmap0);
169
      mTexture[1].setTexture(bitmap1);
170
      mTexture[2].setTexture(bitmap2);
171

    
172
      MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
173

    
174
      mScreen.detachAll();
175
      for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture[i], mEffects[i], mesh);
176

    
177
      DistortedEffects.enableEffect(EffectNames.SINK);
178
      DistortedEffects.enableEffect(EffectNames.DISTORT);
179

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