Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 698ad0a8

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 8dfa45c4 leszek
import org.distorted.library.effect.MatrixEffectMove;
31
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.effect.VertexEffectDistort;
33
import org.distorted.library.effect.VertexEffectSink;
34 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
35 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.main.DistortedEffects;
38 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
39 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
40
import org.distorted.library.type.Static1D;
41 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43 427ab7bf Leszek Koltunski
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46
import android.opengl.GLSurfaceView;
47
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50
class DifferentBitmapsRenderer implements GLSurfaceView.Renderer 
51
{
52
   private static final int NUM = 3;
53
   
54
   private GLSurfaceView mView;
55 d04a4886 Leszek Koltunski
   private DistortedEffects[] mEffects;
56 b0ebdf5e Leszek Koltunski
   private DistortedTexture[] mTexture;
57 f3ca895f Leszek Koltunski
   private MeshRectangles mMesh;
58 d218d64e leszek
   private DistortedScreen mScreen;
59 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
60 8dfa45c4 leszek
   private Static3D mScale;
61
   private Static3D[] mMove;
62
63 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65 b4237a76 Leszek Koltunski
   DifferentBitmapsRenderer(GLSurfaceView v)
66 427ab7bf Leszek Koltunski
      {     
67
      mView = v;
68
     
69 f6d884d5 Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(3000,0.0f);
70 b4237a76 Leszek Koltunski
      dDistort.add(new Static3D( 25,0,0));
71
      dDistort.add(new Static3D(-25,0,0));
72 a4d59c0b Leszek Koltunski
      Static3D mPoint = new Static3D(305, 220, 0);
73 f6d884d5 Leszek Koltunski
74 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects[NUM];
75 698ad0a8 Leszek Koltunski
      mEffects[0] = new DistortedEffects();
76 f6d884d5 Leszek Koltunski
      for(int i=1; i<NUM; i++)
77 e3900503 Leszek Koltunski
        mEffects[i] = new DistortedEffects(mEffects[0], DistortedLibrary.CLONE_VERTEX| DistortedLibrary.CLONE_FRAGMENT);
78 f6d884d5 Leszek Koltunski
79
      // Add the effects only to the first queue - all VERTEX and FRAGMENT effects are shared!
80
      // (Matrix effect cannot be shared as we have to display each Texture in a different location)
81 a4d59c0b Leszek Koltunski
      VertexEffectSink sink = new VertexEffectSink(new Static1D(8), mPoint, new Static4D(0,0,0,80));
82 8dfa45c4 leszek
      VertexEffectDistort distort = new VertexEffectDistort(dDistort,mPoint);
83
      mEffects[0].apply(sink);    // enlarge the nose
84
      mEffects[0].apply(distort); // keep moving the whole bitmap left and right.
85
86
      // Now the Matrix effects
87
      mScale = new Static3D(1,1,1);
88
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
89
      mMove  = new Static3D[NUM];
90
      MatrixEffectMove[] moveEffect = new MatrixEffectMove[NUM];
91
92
      for(int i=0; i<NUM; i++)
93
        {
94
        mMove[i] = new Static3D(0,0,0);
95
        moveEffect[i] = new MatrixEffectMove(mMove[i]);
96
        mEffects[i].apply(scaleEffect);
97 dae661e9 Leszek Koltunski
        mEffects[i].apply(moveEffect[i]);
98 8dfa45c4 leszek
        }
99 392e16fd Leszek Koltunski
100 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
101 855ba24e leszek
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
102 427ab7bf Leszek Koltunski
      }
103
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
106
   private Bitmap readBitmap(int id)
107
     {
108
     InputStream is = mView.getContext().getResources().openRawResource(id);
109 a4d59c0b Leszek Koltunski
     Bitmap bitmap;
110 427ab7bf Leszek Koltunski
           
111
     try 
112
       {
113
       bitmap = BitmapFactory.decodeStream(is);
114
       } 
115
     finally 
116
       {
117
       try 
118
         {
119
         is.close();
120
         } 
121
       catch(IOException e) { }
122
       }
123
     
124
     return bitmap;
125
     }
126
   
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
   
129 e4330c89 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
130 427ab7bf Leszek Koltunski
      {
131 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
132 427ab7bf Leszek Koltunski
      }
133
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
    
136 e4330c89 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
137
     {
138
     if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
139
       {
140
       int w = (height*bmpWidth)/bmpHeight;
141
       float factor = (float)height/bmpHeight;
142
       mScale.set(factor,factor,factor);
143
144
       for(int i=NUM-1; i>=0; i--)
145
         {
146
         mMove[i].set((width-NUM*w)/2 +i*w ,0,0);
147
         }
148
       }
149
     else
150
       {
151
       int w = width/NUM;
152
       int h = (width*bmpHeight)/(bmpWidth*NUM);
153
       float factor = (float)width/(bmpWidth*NUM);
154
       mScale.set(factor,factor,factor);
155
156
       for(int i=NUM-1; i>=0; i--)
157
         {
158
         mMove[i].set(i*w,(height-h)/2,0);
159
         }
160
       }
161 8dfa45c4 leszek
162
163 e4330c89 Leszek Koltunski
     mScreen.resize(width, height);
164
     }
165 427ab7bf Leszek Koltunski
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
    
168 e4330c89 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
169
     {
170
     Bitmap bitmap0= readBitmap(R.raw.dog);
171
     Bitmap bitmap1= readBitmap(R.raw.face);
172
     Bitmap bitmap2= readBitmap(R.raw.cat);
173 427ab7bf Leszek Koltunski
      
174 e4330c89 Leszek Koltunski
     bmpHeight = bitmap0.getHeight();
175
     bmpWidth  = bitmap0.getWidth();
176 b0ebdf5e Leszek Koltunski
177 e4330c89 Leszek Koltunski
     if( mTexture==null )
178
       {
179
       mTexture = new DistortedTexture[NUM];
180 b0ebdf5e Leszek Koltunski
181 e4330c89 Leszek Koltunski
       for(int i=0; i<NUM; i++)
182 687263cc Leszek Koltunski
         mTexture[i] = new DistortedTexture();
183 e4330c89 Leszek Koltunski
       }
184 b0ebdf5e Leszek Koltunski
185 e4330c89 Leszek Koltunski
     mTexture[0].setTexture(bitmap0);
186
     mTexture[1].setTexture(bitmap1);
187
     mTexture[2].setTexture(bitmap2);
188 fe59d375 Leszek Koltunski
189 698ad0a8 Leszek Koltunski
     if( mMesh==null )
190
       {
191
       mMesh = new MeshRectangles(30,30*bmpHeight/bmpWidth);
192
       mMesh.setStretch(bmpWidth,bmpHeight,0);
193
       }
194 e8b6aa95 Leszek Koltunski
195 e4330c89 Leszek Koltunski
     mScreen.detachAll();
196
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture[i], mEffects[i], mMesh);
197 e8b6aa95 Leszek Koltunski
198 885b9cca leszek
     VertexEffectSink.enable();
199
     VertexEffectDistort.enable();
200 6637d0f2 Leszek Koltunski
201 e4330c89 Leszek Koltunski
     try
202
       {
203 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
204 e4330c89 Leszek Koltunski
       }
205
     catch(Exception ex)
206
       {
207
       android.util.Log.e("Renderer", ex.getMessage() );
208
       }
209
     }
210 427ab7bf Leszek Koltunski
}