Project

General

Profile

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

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

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 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
32 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
33 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
34 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
35 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
36 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.Static1D;
38 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40 427ab7bf Leszek Koltunski
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 f6d884d5 Leszek Koltunski
   private DistortedTexture[] mTexture;
54 d04a4886 Leszek Koltunski
   private DistortedEffects[] mEffects;
55 10b7e588 Leszek Koltunski
   private GridFlat mGrid;
56 392e16fd Leszek Koltunski
   private DistortedFramebuffer mScreen;
57 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
58
    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 b4237a76 Leszek Koltunski
   DifferentBitmapsRenderer(GLSurfaceView v)
62 427ab7bf Leszek Koltunski
      {     
63
      mView = v;
64
     
65 f6d884d5 Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(3000,0.0f);
66 b4237a76 Leszek Koltunski
      dDistort.add(new Static3D( 25,0,0));
67
      dDistort.add(new Static3D(-25,0,0));
68 f6d884d5 Leszek Koltunski
      Static3D mPoint = new Static3D(305, 380, 0);
69
70 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects[NUM];
71
      mEffects[0] = new DistortedEffects();
72 f6d884d5 Leszek Koltunski
      for(int i=1; i<NUM; i++)
73 d04a4886 Leszek Koltunski
        mEffects[i] = new DistortedEffects(mEffects[0], Distorted.CLONE_VERTEX|Distorted.CLONE_FRAGMENT);
74 f6d884d5 Leszek Koltunski
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 392e16fd Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      }
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 bc29e409 Leszek Koltunski
      for(int i=NUM-1; i>=0; i--) mScreen.renderTo(mTexture[i], mGrid, mEffects[i], time);
115 427ab7bf Leszek Koltunski
      }
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 392e16fd Leszek Koltunski
        mEffects[i].abortEffects(EffectTypes.MATRIX);
124 427ab7bf Leszek Koltunski
        }
125
      
126 5055b5d4 Leszek Koltunski
      if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
127 427ab7bf Leszek Koltunski
        {
128
        int w = (height*bmpWidth)/bmpHeight;
129 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
130
131 427ab7bf Leszek Koltunski
        for(int i=NUM-1; i>=0; i--) 
132
          {
133 392e16fd Leszek Koltunski
          mEffects[i].move( new Static3D((width-NUM*w)/2 +i*w ,0,0) );
134
          mEffects[i].scale(factor);
135 427ab7bf Leszek Koltunski
          }
136
        }
137
      else
138
        {
139
        int w = width/NUM;  
140
        int h = (width*bmpHeight)/(bmpWidth*NUM);
141 7589635e Leszek Koltunski
        float factor = (float)width/(bmpWidth*NUM);
142
143 427ab7bf Leszek Koltunski
        for(int i=NUM-1; i>=0; i--) 
144
          {
145 392e16fd Leszek Koltunski
          mEffects[i].move( new Static3D(i*w,(height-h)/2,0) );
146
          mEffects[i].scale(factor);
147 427ab7bf Leszek Koltunski
          }
148
        }
149
         
150 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
151 427ab7bf Leszek Koltunski
      }
152
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
    
155
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
156 e7a4ef16 Leszek Koltunski
      {
157
      GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
158
159 427ab7bf Leszek Koltunski
      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 f6d884d5 Leszek Koltunski
      mTexture = new DistortedTexture[NUM];
167
      for(int i=0; i<NUM; i++)
168 7451c98a Leszek Koltunski
        mTexture[i] = new DistortedTexture(bmpWidth,bmpHeight);
169 427ab7bf Leszek Koltunski
      
170 f6d884d5 Leszek Koltunski
      mTexture[0].setTexture(bitmap0);
171
      mTexture[1].setTexture(bitmap1);
172
      mTexture[2].setTexture(bitmap2);
173 e8b6aa95 Leszek Koltunski
174 10b7e588 Leszek Koltunski
      mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
175 e8b6aa95 Leszek Koltunski
176 427ab7bf Leszek Koltunski
      try
177
        {
178 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
179 427ab7bf Leszek Koltunski
        }
180
      catch(Exception ex)
181
        {
182
        android.util.Log.e("Renderer", ex.getMessage() );
183
        }
184
      }
185
}