Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.differentbitmaps;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
31 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.DistortedEffectQueues;
33 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
34 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
35 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
36
import org.distorted.library.type.Static1D;
37 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39 427ab7bf Leszek Koltunski
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 f6d884d5 Leszek Koltunski
   private DistortedTexture[] mTexture;
53
   private DistortedEffectQueues[] mQueues;
54 10b7e588 Leszek Koltunski
   private GridFlat mGrid;
55 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
56
    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59 b4237a76 Leszek Koltunski
   DifferentBitmapsRenderer(GLSurfaceView v)
60 427ab7bf Leszek Koltunski
      {     
61
      mView = v;
62
     
63 f6d884d5 Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(3000,0.0f);
64 b4237a76 Leszek Koltunski
      dDistort.add(new Static3D( 25,0,0));
65
      dDistort.add(new Static3D(-25,0,0));
66 f6d884d5 Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      }
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 f6d884d5 Leszek Koltunski
      for(int i=NUM-1; i>=0; i--) mQueues[i].draw(time, mTexture[i], mGrid);
111 427ab7bf Leszek Koltunski
      }
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 f6d884d5 Leszek Koltunski
        mQueues[i].abortEffects(EffectTypes.MATRIX);
120 427ab7bf Leszek Koltunski
        }
121
      
122 5055b5d4 Leszek Koltunski
      if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
123 427ab7bf Leszek Koltunski
        {
124
        int w = (height*bmpWidth)/bmpHeight;
125 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
126
127 427ab7bf Leszek Koltunski
        for(int i=NUM-1; i>=0; i--) 
128
          {
129 f6d884d5 Leszek Koltunski
          mQueues[i].move( new Static3D((width-NUM*w)/2 +i*w ,0,0) );
130
          mQueues[i].scale(factor);
131 427ab7bf Leszek Koltunski
          }
132
        }
133
      else
134
        {
135
        int w = width/NUM;  
136
        int h = (width*bmpHeight)/(bmpWidth*NUM);
137 7589635e Leszek Koltunski
        float factor = (float)width/(bmpWidth*NUM);
138
139 427ab7bf Leszek Koltunski
        for(int i=NUM-1; i>=0; i--) 
140
          {
141 f6d884d5 Leszek Koltunski
          mQueues[i].move( new Static3D(i*w,(height-h)/2,0) );
142
          mQueues[i].scale(factor);
143 427ab7bf Leszek Koltunski
          }
144
        }
145
         
146
      Distorted.onSurfaceChanged(width, height); 
147
      }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
    
151
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
152 e7a4ef16 Leszek Koltunski
      {
153
      GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
154
155 427ab7bf Leszek Koltunski
      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 f6d884d5 Leszek Koltunski
      mTexture = new DistortedTexture[NUM];
163
      for(int i=0; i<NUM; i++)
164
        mTexture[i] = new DistortedTexture(bmpWidth, bmpHeight,0);
165 427ab7bf Leszek Koltunski
      
166 f6d884d5 Leszek Koltunski
      mTexture[0].setTexture(bitmap0);
167
      mTexture[1].setTexture(bitmap1);
168
      mTexture[2].setTexture(bitmap2);
169 e8b6aa95 Leszek Koltunski
170 10b7e588 Leszek Koltunski
      mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
171 e8b6aa95 Leszek Koltunski
172 427ab7bf Leszek Koltunski
      try
173
        {
174 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
175 427ab7bf Leszek Koltunski
        }
176
      catch(Exception ex)
177
        {
178
        android.util.Log.e("Renderer", ex.getMessage() );
179
        }
180
      }
181
}