Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 6637d0f2

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 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
45 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
46
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49
class DifferentBitmapsRenderer implements GLSurfaceView.Renderer 
50
{
51
   private static final int NUM = 3;
52
   
53
   private GLSurfaceView mView;
54 d04a4886 Leszek Koltunski
   private DistortedEffects[] mEffects;
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 427ab7bf Leszek Koltunski
      }
81
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
84
   private Bitmap readBitmap(int id)
85
     {
86
     InputStream is = mView.getContext().getResources().openRawResource(id);
87
     Bitmap bitmap=null;
88
           
89
     try 
90
       {
91
       bitmap = BitmapFactory.decodeStream(is);
92
       } 
93
     finally 
94
       {
95
       try 
96
         {
97
         is.close();
98
         } 
99
       catch(IOException e) { }
100
       }
101
     
102
     return bitmap;
103
     }
104
   
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
   
107
    public void onDrawFrame(GL10 glUnused) 
108
      {
109 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
110 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
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 392e16fd Leszek Koltunski
        mEffects[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 392e16fd Leszek Koltunski
          mEffects[i].move( new Static3D((width-NUM*w)/2 +i*w ,0,0) );
130
          mEffects[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 392e16fd Leszek Koltunski
          mEffects[i].move( new Static3D(i*w,(height-h)/2,0) );
142
          mEffects[i].scale(factor);
143 427ab7bf Leszek Koltunski
          }
144
        }
145
         
146 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
147 427ab7bf Leszek Koltunski
      }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
    
151
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
152 e7a4ef16 Leszek Koltunski
      {
153 41a81a14 Leszek Koltunski
      GLES30.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
154 e7a4ef16 Leszek Koltunski
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 fe59d375 Leszek Koltunski
      DistortedTexture[] texture = new DistortedTexture[NUM];
163 f6d884d5 Leszek Koltunski
      for(int i=0; i<NUM; i++)
164 fe59d375 Leszek Koltunski
        texture[i] = new DistortedTexture(bmpWidth,bmpHeight);
165 427ab7bf Leszek Koltunski
      
166 fe59d375 Leszek Koltunski
      texture[0].setTexture(bitmap0);
167
      texture[1].setTexture(bitmap1);
168
      texture[2].setTexture(bitmap2);
169
170
      MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
171 e8b6aa95 Leszek Koltunski
172 fe59d375 Leszek Koltunski
      mScreen.detachAll();
173
      for(int i=NUM-1; i>=0; i--) mScreen.attach(texture[i], mEffects[i], mesh);
174 e8b6aa95 Leszek Koltunski
175 6637d0f2 Leszek Koltunski
      DistortedEffects.enableEffect(EffectNames.SINK);
176
      DistortedEffects.enableEffect(EffectNames.DISTORT);
177
178 427ab7bf Leszek Koltunski
      try
179
        {
180 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
181 427ab7bf Leszek Koltunski
        }
182
      catch(Exception ex)
183
        {
184
        android.util.Log.e("Renderer", ex.getMessage() );
185
        }
186
      }
187
}