Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 01782e85

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.main.Distorted;
31
import org.distorted.library.main.DistortedScreen;
32
import org.distorted.library.main.DistortedTexture;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.EffectNames;
35
import org.distorted.library.main.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 MeshFlat mMesh;
56
   private DistortedScreen mScreen;
57
   private int bmpHeight, bmpWidth;
58
    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

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

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

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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

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

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

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

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

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

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

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

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

    
173
      if( mMesh==null ) mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
174

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

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

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