Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 513b2e9c

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 16b22aab Leszek Koltunski
   private static final int[] bitmap = { R.raw.dog, R.raw.face, R.raw.cat };
53
   private static final int NUM = bitmap.length;
54 427ab7bf Leszek Koltunski
   
55
   private GLSurfaceView mView;
56 d04a4886 Leszek Koltunski
   private DistortedEffects[] mEffects;
57 b0ebdf5e Leszek Koltunski
   private DistortedTexture[] mTexture;
58 f3ca895f Leszek Koltunski
   private MeshRectangles mMesh;
59 d218d64e leszek
   private DistortedScreen mScreen;
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 513b2e9c Leszek Koltunski
      Static3D point = new Static3D(0,-60,0);
73
      Static4D region= new Static4D(0,0,0,300);
74 f6d884d5 Leszek Koltunski
75 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects[NUM];
76 698ad0a8 Leszek Koltunski
      mEffects[0] = new DistortedEffects();
77 f6d884d5 Leszek Koltunski
      for(int i=1; i<NUM; i++)
78 e3900503 Leszek Koltunski
        mEffects[i] = new DistortedEffects(mEffects[0], DistortedLibrary.CLONE_VERTEX| DistortedLibrary.CLONE_FRAGMENT);
79 f6d884d5 Leszek Koltunski
80
      // Add the effects only to the first queue - all VERTEX and FRAGMENT effects are shared!
81
      // (Matrix effect cannot be shared as we have to display each Texture in a different location)
82 513b2e9c Leszek Koltunski
      VertexEffectSink sink = new VertexEffectSink(new Static1D(8), point, new Static4D(0,0,0,80));
83
      VertexEffectDistort distort = new VertexEffectDistort(dDistort,point,region);
84 8dfa45c4 leszek
      mEffects[0].apply(sink);    // enlarge the nose
85
      mEffects[0].apply(distort); // keep moving the whole bitmap left and right.
86
87
      // Now the Matrix effects
88
      mScale = new Static3D(1,1,1);
89
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
90
      mMove  = new Static3D[NUM];
91
      MatrixEffectMove[] moveEffect = new MatrixEffectMove[NUM];
92
93
      for(int i=0; i<NUM; i++)
94
        {
95
        mMove[i] = new Static3D(0,0,0);
96
        moveEffect[i] = new MatrixEffectMove(mMove[i]);
97
        mEffects[i].apply(scaleEffect);
98 dae661e9 Leszek Koltunski
        mEffects[i].apply(moveEffect[i]);
99 8dfa45c4 leszek
        }
100 392e16fd Leszek Koltunski
101 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
102 855ba24e leszek
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
103 427ab7bf Leszek Koltunski
      }
104
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
   
107 e4330c89 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
108 427ab7bf Leszek Koltunski
      {
109 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
110 427ab7bf Leszek Koltunski
      }
111
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114 e4330c89 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
115
     {
116 126393c8 Leszek Koltunski
     float horiRatio = (float)width / (NUM*mMesh.getStretchX());
117
     float vertRatio = (float)height/ (    mMesh.getStretchY());
118 16b22aab Leszek Koltunski
     float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
119 e4330c89 Leszek Koltunski
120 16b22aab Leszek Koltunski
     mScale.set( factor,factor,factor );
121 8dfa45c4 leszek
122 16b22aab Leszek Koltunski
     for(int i=NUM-1; i>=0; i--) mMove[i].set((i-(NUM-1)/2.0f)*(width/NUM), 0, 0);
123 8dfa45c4 leszek
124 e4330c89 Leszek Koltunski
     mScreen.resize(width, height);
125
     }
126 427ab7bf Leszek Koltunski
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129 e4330c89 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
130
     {
131 16b22aab Leszek Koltunski
     Bitmap[] bmp = new Bitmap[NUM];
132
133
     for(int i=0; i<NUM; i++)
134
       {
135
       bmp[i] = readBitmap(bitmap[i]);
136
       }
137
138
     int bmpHeight = bmp[0].getHeight();
139
     int bmpWidth  = bmp[0].getWidth();
140 b0ebdf5e Leszek Koltunski
141 e4330c89 Leszek Koltunski
     if( mTexture==null )
142
       {
143
       mTexture = new DistortedTexture[NUM];
144 b0ebdf5e Leszek Koltunski
145 e4330c89 Leszek Koltunski
       for(int i=0; i<NUM; i++)
146 687263cc Leszek Koltunski
         mTexture[i] = new DistortedTexture();
147 e4330c89 Leszek Koltunski
       }
148 b0ebdf5e Leszek Koltunski
149 16b22aab Leszek Koltunski
     for(int i=0; i<NUM; i++)
150
       {
151
       mTexture[i].setTexture(bmp[i]);
152
       }
153 fe59d375 Leszek Koltunski
154 698ad0a8 Leszek Koltunski
     if( mMesh==null )
155
       {
156
       mMesh = new MeshRectangles(30,30*bmpHeight/bmpWidth);
157
       mMesh.setStretch(bmpWidth,bmpHeight,0);
158
       }
159 e8b6aa95 Leszek Koltunski
160 e4330c89 Leszek Koltunski
     mScreen.detachAll();
161
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture[i], mEffects[i], mMesh);
162 e8b6aa95 Leszek Koltunski
163 885b9cca leszek
     VertexEffectSink.enable();
164
     VertexEffectDistort.enable();
165 6637d0f2 Leszek Koltunski
166 e4330c89 Leszek Koltunski
     try
167
       {
168 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
169 e4330c89 Leszek Koltunski
       }
170
     catch(Exception ex)
171
       {
172
       android.util.Log.e("Renderer", ex.getMessage() );
173
       }
174
     }
175 16b22aab Leszek Koltunski
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
178
   private Bitmap readBitmap(int id)
179
     {
180
     InputStream is = mView.getContext().getResources().openRawResource(id);
181
     Bitmap bitmap;
182
183
     try
184
       {
185
       bitmap = BitmapFactory.decodeStream(is);
186
       }
187
     finally
188
       {
189
       try
190
         {
191
         is.close();
192
         }
193
       catch(IOException e) { }
194
       }
195
196
     return bitmap;
197
     }
198
199 427ab7bf Leszek Koltunski
}