Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 630703d1

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 d218d64e leszek
import org.distorted.library.DistortedScreen;
32 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
33 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
34 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
35 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
36 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
37 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
38
import org.distorted.library.type.Static1D;
39 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41 427ab7bf Leszek Koltunski
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 d04a4886 Leszek Koltunski
   private DistortedEffects[] mEffects;
54 b0ebdf5e Leszek Koltunski
   private DistortedTexture[] mTexture;
55 d218d64e leszek
   private DistortedScreen mScreen;
56 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
57
    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 b4237a76 Leszek Koltunski
   DifferentBitmapsRenderer(GLSurfaceView v)
61 427ab7bf Leszek Koltunski
      {     
62
      mView = v;
63
     
64 f6d884d5 Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(3000,0.0f);
65 b4237a76 Leszek Koltunski
      dDistort.add(new Static3D( 25,0,0));
66
      dDistort.add(new Static3D(-25,0,0));
67 f6d884d5 Leszek Koltunski
      Static3D mPoint = new Static3D(305, 380, 0);
68
69 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects[NUM];
70
      mEffects[0] = new DistortedEffects();
71 f6d884d5 Leszek Koltunski
      for(int i=1; i<NUM; i++)
72 d04a4886 Leszek Koltunski
        mEffects[i] = new DistortedEffects(mEffects[0], Distorted.CLONE_VERTEX|Distorted.CLONE_FRAGMENT);
73 f6d884d5 Leszek Koltunski
74
      // Add the effects only to the first queue - all VERTEX and FRAGMENT effects are shared!
75
      // (Matrix effect cannot be shared as we have to display each Texture in a different location)
76 392e16fd Leszek Koltunski
      mEffects[0].sink( new Static1D(8), mPoint, new Static4D(0,0,80,80));  // enlarge the nose
77
      mEffects[0].distort(dDistort,mPoint);                                 // keep moving the whole bitmap left and right.
78
79 d218d64e leszek
      mScreen = new DistortedScreen();
80 855ba24e leszek
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
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 630703d1 leszek
    public void onDrawFrame(GL10 glUnused) 
109 427ab7bf Leszek Koltunski
      {
110 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
111 427ab7bf Leszek Koltunski
      }
112
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115 630703d1 leszek
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
116
      {  
117
      for(int i=NUM-1; i>=0; i--) 
118
        {   
119
        mEffects[i].abortEffects(EffectTypes.MATRIX);
120
        }
121 427ab7bf Leszek Koltunski
      
122 630703d1 leszek
      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
          mEffects[i].move( new Static3D((width-NUM*w)/2 +i*w ,0,0) );
130
          mEffects[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
          mEffects[i].move( new Static3D(i*w,(height-h)/2,0) );
142
          mEffects[i].scale(factor);
143
          }
144
        }
145 427ab7bf Leszek Koltunski
         
146 630703d1 leszek
      mScreen.resize(width, height);
147
      }
148 427ab7bf Leszek Koltunski
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
    
151 630703d1 leszek
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
152
      {
153
      Bitmap bitmap0= readBitmap(R.raw.dog);
154
      Bitmap bitmap1= readBitmap(R.raw.face);
155
      Bitmap bitmap2= readBitmap(R.raw.cat);
156 427ab7bf Leszek Koltunski
      
157 630703d1 leszek
      bmpHeight = bitmap0.getHeight();
158
      bmpWidth  = bitmap0.getWidth();
159 b0ebdf5e Leszek Koltunski
160 630703d1 leszek
      if( mTexture==null )
161
        {
162
        mTexture = new DistortedTexture[NUM];
163 b0ebdf5e Leszek Koltunski
164 630703d1 leszek
        for(int i=0; i<NUM; i++)
165
          mTexture[i] = new DistortedTexture(bmpWidth,bmpHeight);
166
        }
167 b0ebdf5e Leszek Koltunski
168 630703d1 leszek
      mTexture[0].setTexture(bitmap0);
169
      mTexture[1].setTexture(bitmap1);
170
      mTexture[2].setTexture(bitmap2);
171 fe59d375 Leszek Koltunski
172 630703d1 leszek
      MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
173 e8b6aa95 Leszek Koltunski
174 630703d1 leszek
      mScreen.detachAll();
175
      for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture[i], mEffects[i], mesh);
176 e8b6aa95 Leszek Koltunski
177 630703d1 leszek
      DistortedEffects.enableEffect(EffectNames.SINK);
178
      DistortedEffects.enableEffect(EffectNames.DISTORT);
179 6637d0f2 Leszek Koltunski
180 630703d1 leszek
      try
181
        {
182
        Distorted.onCreate(mView.getContext());
183
        }
184
      catch(Exception ex)
185
        {
186
        android.util.Log.e("Renderer", ex.getMessage() );
187
        }
188
      }
189 427ab7bf Leszek Koltunski
}