Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ 8eed34d3

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

    
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[] bitmap = { R.raw.dog, R.raw.face, R.raw.cat };
53
   private static final int NUM = bitmap.length;
54
   
55
   private GLSurfaceView mView;
56
   private DistortedEffects[] mEffects;
57
   private DistortedTexture[] mTexture;
58
   private MeshRectangles mMesh;
59
   private DistortedScreen mScreen;
60
   private Static3D mScale;
61
   private Static3D[] mMove;
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
   DifferentBitmapsRenderer(GLSurfaceView v)
66
      {     
67
      mView = v;
68
     
69
      Dynamic3D dDistort = new Dynamic3D(3000,0.0f);
70
      dDistort.add(new Static3D( 25,0,0));
71
      dDistort.add(new Static3D(-25,0,0));
72
      Static3D mPoint = new Static3D(0,-60,0);
73

    
74
      mEffects = new DistortedEffects[NUM];
75
      mEffects[0] = new DistortedEffects();
76
      for(int i=1; i<NUM; i++)
77
        mEffects[i] = new DistortedEffects(mEffects[0], DistortedLibrary.CLONE_VERTEX| DistortedLibrary.CLONE_FRAGMENT);
78

    
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
      VertexEffectSink sink = new VertexEffectSink(new Static1D(8), mPoint, new Static4D(0,0,0,80));
82
      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
        mEffects[i].apply(moveEffect[i]);
98
        }
99

    
100
      mScreen = new DistortedScreen();
101
      mScreen.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
   
106
   public void onDrawFrame(GL10 glUnused)
107
      {
108
      mScreen.render( System.currentTimeMillis() );
109
      }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
    
113
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
114
     {
115
     float horiRatio = (float)width / (NUM*mTexture[0].getWidth());
116
     float vertRatio = (float)height/ (    mTexture[0].getHeight());
117
     float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
118

    
119
     mScale.set( factor,factor,factor );
120

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

    
123
     mScreen.resize(width, height);
124
     }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
129
     {
130
     Bitmap[] bmp = new Bitmap[NUM];
131

    
132
     for(int i=0; i<NUM; i++)
133
       {
134
       bmp[i] = readBitmap(bitmap[i]);
135
       }
136

    
137
     int bmpHeight = bmp[0].getHeight();
138
     int bmpWidth  = bmp[0].getWidth();
139

    
140
     if( mTexture==null )
141
       {
142
       mTexture = new DistortedTexture[NUM];
143

    
144
       for(int i=0; i<NUM; i++)
145
         mTexture[i] = new DistortedTexture();
146
       }
147

    
148
     for(int i=0; i<NUM; i++)
149
       {
150
       mTexture[i].setTexture(bmp[i]);
151
       }
152

    
153
     if( mMesh==null )
154
       {
155
       mMesh = new MeshRectangles(30,30*bmpHeight/bmpWidth);
156
       mMesh.setStretch(bmpWidth,bmpHeight,0);
157
       }
158

    
159
     mScreen.detachAll();
160
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture[i], mEffects[i], mMesh);
161

    
162
     VertexEffectSink.enable();
163
     VertexEffectDistort.enable();
164

    
165
     try
166
       {
167
       DistortedLibrary.onCreate(mView.getContext());
168
       }
169
     catch(Exception ex)
170
       {
171
       android.util.Log.e("Renderer", ex.getMessage() );
172
       }
173
     }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

    
177
   private Bitmap readBitmap(int id)
178
     {
179
     InputStream is = mView.getContext().getResources().openRawResource(id);
180
     Bitmap bitmap;
181

    
182
     try
183
       {
184
       bitmap = BitmapFactory.decodeStream(is);
185
       }
186
     finally
187
       {
188
       try
189
         {
190
         is.close();
191
         }
192
       catch(IOException e) { }
193
       }
194

    
195
     return bitmap;
196
     }
197

    
198
}
(2-2/3)