Project

General

Profile

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

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

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

    
40
import android.graphics.Bitmap;
41
import android.graphics.BitmapFactory;
42
import android.opengl.GLES20;
43
import android.opengl.GLSurfaceView;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class DifferentBitmapsRenderer implements GLSurfaceView.Renderer 
48
{
49
   private static final int NUM = 3;
50
   
51
   private GLSurfaceView mView;
52
   private DistortedTexture[] mTexture;
53
   private DistortedEffectQueues[] mQueues;
54
   private GridFlat mGrid;
55
   private int bmpHeight, bmpWidth;
56
    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

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

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

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
   private Bitmap readBitmap(int id)
82
     {
83
     InputStream is = mView.getContext().getResources().openRawResource(id);
84
     Bitmap bitmap=null;
85
           
86
     try 
87
       {
88
       bitmap = BitmapFactory.decodeStream(is);
89
       } 
90
     finally 
91
       {
92
       try 
93
         {
94
         is.close();
95
         } 
96
       catch(IOException e) { }
97
       }
98
     
99
     return bitmap;
100
     }
101
   
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
   
104
    public void onDrawFrame(GL10 glUnused) 
105
      {
106
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
107
      
108
      long time = System.currentTimeMillis();
109
      
110
      for(int i=NUM-1; i>=0; i--) mQueues[i].draw(time, mTexture[i], mGrid);
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
        mQueues[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
          mQueues[i].move( new Static3D((width-NUM*w)/2 +i*w ,0,0) );
130
          mQueues[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
          mQueues[i].move( new Static3D(i*w,(height-h)/2,0) );
142
          mQueues[i].scale(factor);
143
          }
144
        }
145
         
146
      Distorted.onSurfaceChanged(width, height); 
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
    
151
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
152
      {
153
      GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
154

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

    
170
      mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
171

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