Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 392e16fd

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.DistortedFramebuffer;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.DistortedEffectQueues;
34
import org.distorted.library.GridFlat;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

    
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLES20;
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 DistortedTexture[] mTexture;
54
   private DistortedEffectQueues[] mEffects;
55
   private GridFlat mGrid;
56
   private DistortedFramebuffer 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 DistortedEffectQueues[NUM];
71
      mEffects[0] = new DistortedEffectQueues();
72
      for(int i=1; i<NUM; i++)
73
        mEffects[i] = new DistortedEffectQueues(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 DistortedFramebuffer(0);
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
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
111
      
112
      long time = System.currentTimeMillis();
113
      
114
      for(int i=NUM-1; i>=0; i--) mEffects[i].draw(time, mTexture[i], mGrid, mScreen);
115
      }
116

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

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

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

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
    
155
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
156
      {
157
      GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
158

    
159
      Bitmap bitmap0= readBitmap(R.raw.dog);
160
      Bitmap bitmap1= readBitmap(R.raw.face);
161
      Bitmap bitmap2= readBitmap(R.raw.cat);
162
      
163
      bmpHeight = bitmap0.getHeight();
164
      bmpWidth  = bitmap0.getWidth();
165
      
166
      mTexture = new DistortedTexture[NUM];
167
      for(int i=0; i<NUM; i++)
168
        mTexture[i] = new DistortedTexture(bmpWidth,bmpHeight);
169
      
170
      mTexture[0].setTexture(bitmap0);
171
      mTexture[1].setTexture(bitmap1);
172
      mTexture[2].setTexture(bitmap2);
173

    
174
      mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
175

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