Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 625c67de

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.MeshSquare;
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.app.ActivityManager;
46
import android.content.Context;
47
import android.content.pm.ConfigurationInfo;
48
import android.content.res.Resources;
49
import android.graphics.Bitmap;
50
import android.graphics.BitmapFactory;
51
import android.opengl.GLSurfaceView;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
class DifferentBitmapsRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
56
{
57
   private static final int[] bitmap = { R.raw.dog, R.raw.face, R.raw.cat };
58
   private static final int NUM = bitmap.length;
59
   
60
   private final GLSurfaceView mView;
61
   private final DistortedEffects[] mEffects;
62
   private final DistortedScreen mScreen;
63
   private final Static3D mScaleMatrix, mScaleVertex;
64
   private final Static3D[] mMove;
65
   private final Resources mResources;
66
   private DistortedTexture[] mTexture;
67
   private MeshSquare mMesh;
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
   DifferentBitmapsRenderer(GLSurfaceView v)
72
      {     
73
      mView = v;
74
      mResources = v.getResources();
75

    
76
      Dynamic3D dDistort = new Dynamic3D(3000,0.0f);
77
      dDistort.add(new Static3D( 25,0,0));
78
      dDistort.add(new Static3D(-25,0,0));
79
      Static3D point = new Static3D(0,-60,0);
80
      Static4D region= new Static4D(0,0,0,300);
81

    
82
      mScaleMatrix = new Static3D(1,1,1);
83
      mScaleVertex = new Static3D(1,1,1);
84

    
85
      mEffects = new DistortedEffects[NUM];
86
      mEffects[0] = new DistortedEffects();
87
      for(int i=1; i<NUM; i++)
88
        mEffects[i] = new DistortedEffects(mEffects[0], DistortedLibrary.CLONE_VERTEX| DistortedLibrary.CLONE_FRAGMENT);
89

    
90
      // Add the effects only to the first queue - all VERTEX and FRAGMENT effects are shared!
91
      // (Matrix effect cannot be shared as we have to display each Texture in a different location)
92
      VertexEffectScale scale     = new VertexEffectScale(mScaleVertex);
93
      VertexEffectSink sink       = new VertexEffectSink(new Static1D(8), point, new Static4D(0,0,0,80));
94
      VertexEffectDistort distort = new VertexEffectDistort(dDistort,point,region);
95
      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)
96
      mEffects[0].apply(sink);    // enlarge the nose
97
      mEffects[0].apply(distort); // keep moving the whole bitmap left and right.
98

    
99
      // Now the Matrix effects
100

    
101
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScaleMatrix);
102
      mMove  = new Static3D[NUM];
103
      MatrixEffectMove[] moveEffect = new MatrixEffectMove[NUM];
104

    
105
      for(int i=0; i<NUM; i++)
106
        {
107
        mMove[i] = new Static3D(0,0,0);
108
        moveEffect[i] = new MatrixEffectMove(mMove[i]);
109
        mEffects[i].apply(scaleEffect);
110
        mEffects[i].apply(moveEffect[i]);
111
        }
112

    
113
      mScreen = new DistortedScreen();
114
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
115
      }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
   
119
   public void onDrawFrame(GL10 glUnused)
120
      {
121
      mScreen.render( System.currentTimeMillis() );
122
      }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
    
126
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
127
     {
128
     float horiRatio = (float)width / (NUM*mScaleVertex.get0());
129
     float vertRatio = (float)height/ (    mScaleVertex.get1());
130
     float factor    = Math.min(horiRatio, vertRatio);
131

    
132
     mScaleMatrix.set( factor,factor,factor );
133

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

    
136
     mScreen.resize(width, height);
137
     }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
    
141
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
142
     {
143
     Bitmap[] bmp = new Bitmap[NUM];
144

    
145
     for(int i=0; i<NUM; i++)
146
       {
147
       bmp[i] = readBitmap(bitmap[i]);
148
       }
149

    
150
     int bmpHeight = bmp[0].getHeight();
151
     int bmpWidth  = bmp[0].getWidth();
152

    
153
     mScaleVertex.set(bmpWidth,bmpHeight,0);
154

    
155
     if( mTexture==null )
156
       {
157
       mTexture = new DistortedTexture[NUM];
158

    
159
       for(int i=0; i<NUM; i++)
160
         mTexture[i] = new DistortedTexture();
161
       }
162

    
163
     for(int i=0; i<NUM; i++)
164
       {
165
       mTexture[i].setTexture(bmp[i]);
166
       }
167

    
168
     if( mMesh==null ) mMesh = new MeshSquare(30,30*bmpHeight/bmpWidth);
169

    
170
     mScreen.detachAll();
171
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture[i], mEffects[i], mMesh);
172

    
173
     VertexEffectScale.enable();
174
     VertexEffectSink.enable();
175
     VertexEffectDistort.enable();
176

    
177
     DistortedLibrary.onSurfaceCreated(this);
178
     }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

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

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

    
200
     return bitmap;
201
     }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
   public void distortedException(Exception ex)
206
     {
207
     android.util.Log.e("DifferentBitmaps", ex.getMessage() );
208
     }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
   public InputStream localFile(int fileID)
213
     {
214
     return mResources.openRawResource(fileID);
215
     }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
   public void logMessage(String message)
220
     {
221
     android.util.Log.e("DifferentBitmaps", message );
222
     }
223
}
(2-2/3)