Project

General

Profile

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

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

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 d218d64e leszek
import org.distorted.library.DistortedScreen;
33 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
34 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
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 f6d884d5 Leszek Koltunski
   private DistortedTexture[] mTexture;
55 d04a4886 Leszek Koltunski
   private DistortedEffects[] mEffects;
56 b01acdaf Leszek Koltunski
   private MeshFlat mMesh;
57 d218d64e leszek
   private DistortedScreen mScreen;
58 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
59
    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62 b4237a76 Leszek Koltunski
   DifferentBitmapsRenderer(GLSurfaceView v)
63 427ab7bf Leszek Koltunski
      {     
64
      mView = v;
65
     
66 f6d884d5 Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(3000,0.0f);
67 b4237a76 Leszek Koltunski
      dDistort.add(new Static3D( 25,0,0));
68
      dDistort.add(new Static3D(-25,0,0));
69 f6d884d5 Leszek Koltunski
      Static3D mPoint = new Static3D(305, 380, 0);
70
71 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects[NUM];
72
      mEffects[0] = new DistortedEffects();
73 f6d884d5 Leszek Koltunski
      for(int i=1; i<NUM; i++)
74 d04a4886 Leszek Koltunski
        mEffects[i] = new DistortedEffects(mEffects[0], Distorted.CLONE_VERTEX|Distorted.CLONE_FRAGMENT);
75 f6d884d5 Leszek Koltunski
76
      // Add the effects only to the first queue - all VERTEX and FRAGMENT effects are shared!
77
      // (Matrix effect cannot be shared as we have to display each Texture in a different location)
78 392e16fd Leszek Koltunski
      mEffects[0].sink( new Static1D(8), mPoint, new Static4D(0,0,80,80));  // enlarge the nose
79
      mEffects[0].distort(dDistort,mPoint);                                 // keep moving the whole bitmap left and right.
80
81 d218d64e leszek
      mScreen = new DistortedScreen();
82 427ab7bf Leszek Koltunski
      }
83
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
86
   private Bitmap readBitmap(int id)
87
     {
88
     InputStream is = mView.getContext().getResources().openRawResource(id);
89
     Bitmap bitmap=null;
90
           
91
     try 
92
       {
93
       bitmap = BitmapFactory.decodeStream(is);
94
       } 
95
     finally 
96
       {
97
       try 
98
         {
99
         is.close();
100
         } 
101
       catch(IOException e) { }
102
       }
103
     
104
     return bitmap;
105
     }
106
   
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
   
109
    public void onDrawFrame(GL10 glUnused) 
110
      {
111 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
112 427ab7bf Leszek Koltunski
      
113
      long time = System.currentTimeMillis();
114
      
115 b01acdaf Leszek Koltunski
      for(int i=NUM-1; i>=0; i--) mScreen.renderTo(mTexture[i], mMesh, mEffects[i], time);
116 427ab7bf Leszek Koltunski
      }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
121
      {  
122
      for(int i=NUM-1; i>=0; i--) 
123
        {   
124 392e16fd Leszek Koltunski
        mEffects[i].abortEffects(EffectTypes.MATRIX);
125 427ab7bf Leszek Koltunski
        }
126
      
127 5055b5d4 Leszek Koltunski
      if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
128 427ab7bf Leszek Koltunski
        {
129
        int w = (height*bmpWidth)/bmpHeight;
130 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
131
132 427ab7bf Leszek Koltunski
        for(int i=NUM-1; i>=0; i--) 
133
          {
134 392e16fd Leszek Koltunski
          mEffects[i].move( new Static3D((width-NUM*w)/2 +i*w ,0,0) );
135
          mEffects[i].scale(factor);
136 427ab7bf Leszek Koltunski
          }
137
        }
138
      else
139
        {
140
        int w = width/NUM;  
141
        int h = (width*bmpHeight)/(bmpWidth*NUM);
142 7589635e Leszek Koltunski
        float factor = (float)width/(bmpWidth*NUM);
143
144 427ab7bf Leszek Koltunski
        for(int i=NUM-1; i>=0; i--) 
145
          {
146 392e16fd Leszek Koltunski
          mEffects[i].move( new Static3D(i*w,(height-h)/2,0) );
147
          mEffects[i].scale(factor);
148 427ab7bf Leszek Koltunski
          }
149
        }
150
         
151 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
152 427ab7bf Leszek Koltunski
      }
153
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
    
156
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
157 e7a4ef16 Leszek Koltunski
      {
158 41a81a14 Leszek Koltunski
      GLES30.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
159 e7a4ef16 Leszek Koltunski
160 427ab7bf Leszek Koltunski
      Bitmap bitmap0= readBitmap(R.raw.dog);
161
      Bitmap bitmap1= readBitmap(R.raw.face);
162
      Bitmap bitmap2= readBitmap(R.raw.cat);
163
      
164
      bmpHeight = bitmap0.getHeight();
165
      bmpWidth  = bitmap0.getWidth();
166
      
167 f6d884d5 Leszek Koltunski
      mTexture = new DistortedTexture[NUM];
168
      for(int i=0; i<NUM; i++)
169 7451c98a Leszek Koltunski
        mTexture[i] = new DistortedTexture(bmpWidth,bmpHeight);
170 427ab7bf Leszek Koltunski
      
171 f6d884d5 Leszek Koltunski
      mTexture[0].setTexture(bitmap0);
172
      mTexture[1].setTexture(bitmap1);
173
      mTexture[2].setTexture(bitmap2);
174 e8b6aa95 Leszek Koltunski
175 b01acdaf Leszek Koltunski
      mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
176 e8b6aa95 Leszek Koltunski
177 427ab7bf Leszek Koltunski
      try
178
        {
179 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
180 427ab7bf Leszek Koltunski
        }
181
      catch(Exception ex)
182
        {
183
        android.util.Log.e("Renderer", ex.getMessage() );
184
        }
185
      }
186
}