Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 061449ed

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
20
package org.distorted.examples.differentbitmaps;
21

    
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
import org.distorted.examples.R;
29

    
30
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.VertexEffectScale;
34
import org.distorted.library.effect.VertexEffectSink;
35
import org.distorted.library.main.DistortedLibrary;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.main.DistortedEffects;
39
import org.distorted.library.mesh.MeshRectangles;
40
import org.distorted.library.type.Dynamic3D;
41
import org.distorted.library.type.Static1D;
42
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44

    
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLSurfaceView;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class DifferentBitmapsRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
52
{
53
   private static final int[] bitmap = { R.raw.dog, R.raw.face, R.raw.cat };
54
   private static final int NUM = bitmap.length;
55
   
56
   private GLSurfaceView mView;
57
   private DistortedEffects[] mEffects;
58
   private DistortedTexture[] mTexture;
59
   private MeshRectangles mMesh;
60
   private DistortedScreen mScreen;
61
   private Static3D mScaleMatrix, mScaleVertex;
62
   private Static3D[] mMove;
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

    
66
   DifferentBitmapsRenderer(GLSurfaceView v)
67
      {     
68
      mView = v;
69
     
70
      Dynamic3D dDistort = new Dynamic3D(3000,0.0f);
71
      dDistort.add(new Static3D( 25,0,0));
72
      dDistort.add(new Static3D(-25,0,0));
73
      Static3D point = new Static3D(0,-60,0);
74
      Static4D region= new Static4D(0,0,0,300);
75

    
76
      mScaleMatrix = new Static3D(1,1,1);
77
      mScaleVertex = new Static3D(1,1,1);
78

    
79
      mEffects = new DistortedEffects[NUM];
80
      mEffects[0] = new DistortedEffects();
81
      for(int i=1; i<NUM; i++)
82
        mEffects[i] = new DistortedEffects(mEffects[0], DistortedLibrary.CLONE_VERTEX| DistortedLibrary.CLONE_FRAGMENT);
83

    
84
      // Add the effects only to the first queue - all VERTEX and FRAGMENT effects are shared!
85
      // (Matrix effect cannot be shared as we have to display each Texture in a different location)
86
      VertexEffectScale scale     = new VertexEffectScale(mScaleVertex);
87
      VertexEffectSink sink       = new VertexEffectSink(new Static1D(8), point, new Static4D(0,0,0,80));
88
      VertexEffectDistort distort = new VertexEffectDistort(dDistort,point,region);
89
      mEffects[0].apply(scale);   // scale the whole thing (so far by 1, but mScaleVertex will be reset once we know the size of the bitmap)
90
      mEffects[0].apply(sink);    // enlarge the nose
91
      mEffects[0].apply(distort); // keep moving the whole bitmap left and right.
92

    
93
      // Now the Matrix effects
94

    
95
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScaleMatrix);
96
      mMove  = new Static3D[NUM];
97
      MatrixEffectMove[] moveEffect = new MatrixEffectMove[NUM];
98

    
99
      for(int i=0; i<NUM; i++)
100
        {
101
        mMove[i] = new Static3D(0,0,0);
102
        moveEffect[i] = new MatrixEffectMove(mMove[i]);
103
        mEffects[i].apply(scaleEffect);
104
        mEffects[i].apply(moveEffect[i]);
105
        }
106

    
107
      mScreen = new DistortedScreen();
108
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
109
      }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
   
113
   public void onDrawFrame(GL10 glUnused)
114
      {
115
      mScreen.render( System.currentTimeMillis() );
116
      }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
121
     {
122
     float horiRatio = (float)width / (NUM*mScaleVertex.get0());
123
     float vertRatio = (float)height/ (    mScaleVertex.get1());
124
     float factor    = Math.min(horiRatio, vertRatio);
125

    
126
     mScaleMatrix.set( factor,factor,factor );
127

    
128
     for(int i=NUM-1; i>=0; i--) mMove[i].set((i-(NUM-1)/2.0f)*width/NUM, 0, 0);
129

    
130
     mScreen.resize(width, height);
131
     }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
    
135
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
136
     {
137
     Bitmap[] bmp = new Bitmap[NUM];
138

    
139
     for(int i=0; i<NUM; i++)
140
       {
141
       bmp[i] = readBitmap(bitmap[i]);
142
       }
143

    
144
     int bmpHeight = bmp[0].getHeight();
145
     int bmpWidth  = bmp[0].getWidth();
146

    
147
     mScaleVertex.set(bmpWidth,bmpHeight,0);
148

    
149
     if( mTexture==null )
150
       {
151
       mTexture = new DistortedTexture[NUM];
152

    
153
       for(int i=0; i<NUM; i++)
154
         mTexture[i] = new DistortedTexture();
155
       }
156

    
157
     for(int i=0; i<NUM; i++)
158
       {
159
       mTexture[i].setTexture(bmp[i]);
160
       }
161

    
162
     if( mMesh==null ) mMesh = new MeshRectangles(30,30*bmpHeight/bmpWidth);
163

    
164
     mScreen.detachAll();
165
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture[i], mEffects[i], mMesh);
166

    
167
     VertexEffectScale.enable();
168
     VertexEffectSink.enable();
169
     VertexEffectDistort.enable();
170

    
171
     DistortedLibrary.onCreate(mView.getContext(),this);
172
     }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
   public void distortedException(Exception ex)
177
     {
178
     android.util.Log.e("DifferentBitmaps", ex.getMessage() );
179
     }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
   private Bitmap readBitmap(int id)
184
     {
185
     InputStream is = mView.getContext().getResources().openRawResource(id);
186
     Bitmap bitmap;
187

    
188
     try
189
       {
190
       bitmap = BitmapFactory.decodeStream(is);
191
       }
192
     finally
193
       {
194
       try
195
         {
196
         is.close();
197
         }
198
       catch(IOException e) { }
199
       }
200

    
201
     return bitmap;
202
     }
203

    
204
}
(2-2/3)