Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differentbitmaps / DifferentBitmapsRenderer.java @ e4330c89

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 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.EffectName;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectDistort;
34
import org.distorted.library.effect.VertexEffectSink;
35 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
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.main.MeshFlat;
40 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
41
import org.distorted.library.type.Static1D;
42 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
43
import org.distorted.library.type.Static4D;
44 427ab7bf Leszek Koltunski
45
import android.graphics.Bitmap;
46
import android.graphics.BitmapFactory;
47
import android.opengl.GLSurfaceView;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51
class DifferentBitmapsRenderer implements GLSurfaceView.Renderer 
52
{
53
   private static final int NUM = 3;
54
   
55
   private GLSurfaceView mView;
56 d04a4886 Leszek Koltunski
   private DistortedEffects[] mEffects;
57 b0ebdf5e Leszek Koltunski
   private DistortedTexture[] mTexture;
58 5f2a53b6 leszek
   private MeshFlat mMesh;
59 d218d64e leszek
   private DistortedScreen mScreen;
60 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
61 8dfa45c4 leszek
   private Static3D mScale;
62
   private Static3D[] mMove;
63
64 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
65
66 b4237a76 Leszek Koltunski
   DifferentBitmapsRenderer(GLSurfaceView v)
67 427ab7bf Leszek Koltunski
      {     
68
      mView = v;
69
     
70 f6d884d5 Leszek Koltunski
      Dynamic3D dDistort = new Dynamic3D(3000,0.0f);
71 b4237a76 Leszek Koltunski
      dDistort.add(new Static3D( 25,0,0));
72
      dDistort.add(new Static3D(-25,0,0));
73 f6d884d5 Leszek Koltunski
      Static3D mPoint = new Static3D(305, 380, 0);
74
75 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects[NUM];
76
      mEffects[0] = new DistortedEffects();
77 f6d884d5 Leszek Koltunski
      for(int i=1; i<NUM; i++)
78 d04a4886 Leszek Koltunski
        mEffects[i] = new DistortedEffects(mEffects[0], Distorted.CLONE_VERTEX|Distorted.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 8dfa45c4 leszek
      VertexEffectSink sink = new VertexEffectSink(new Static1D(8), mPoint, new Static4D(0,0,80,80));
83
      VertexEffectDistort distort = new VertexEffectDistort(dDistort,mPoint);
84
      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(moveEffect[i]);
98
        mEffects[i].apply(scaleEffect);
99
        }
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
   private Bitmap readBitmap(int id)
108
     {
109
     InputStream is = mView.getContext().getResources().openRawResource(id);
110
     Bitmap bitmap=null;
111
           
112
     try 
113
       {
114
       bitmap = BitmapFactory.decodeStream(is);
115
       } 
116
     finally 
117
       {
118
       try 
119
         {
120
         is.close();
121
         } 
122
       catch(IOException e) { }
123
       }
124
     
125
     return bitmap;
126
     }
127
   
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
   
130 e4330c89 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
131 427ab7bf Leszek Koltunski
      {
132 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
133 427ab7bf Leszek Koltunski
      }
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
    
137 e4330c89 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
138
     {
139
     if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
140
       {
141
       int w = (height*bmpWidth)/bmpHeight;
142
       float factor = (float)height/bmpHeight;
143
       mScale.set(factor,factor,factor);
144
145
       for(int i=NUM-1; i>=0; i--)
146
         {
147
         mMove[i].set((width-NUM*w)/2 +i*w ,0,0);
148
         }
149
       }
150
     else
151
       {
152
       int w = width/NUM;
153
       int h = (width*bmpHeight)/(bmpWidth*NUM);
154
       float factor = (float)width/(bmpWidth*NUM);
155
       mScale.set(factor,factor,factor);
156
157
       for(int i=NUM-1; i>=0; i--)
158
         {
159
         mMove[i].set(i*w,(height-h)/2,0);
160
         }
161
       }
162 8dfa45c4 leszek
163
164 e4330c89 Leszek Koltunski
     mScreen.resize(width, height);
165
     }
166 427ab7bf Leszek Koltunski
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
    
169 e4330c89 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
170
     {
171
     Bitmap bitmap0= readBitmap(R.raw.dog);
172
     Bitmap bitmap1= readBitmap(R.raw.face);
173
     Bitmap bitmap2= readBitmap(R.raw.cat);
174 427ab7bf Leszek Koltunski
      
175 e4330c89 Leszek Koltunski
     bmpHeight = bitmap0.getHeight();
176
     bmpWidth  = bitmap0.getWidth();
177 b0ebdf5e Leszek Koltunski
178 e4330c89 Leszek Koltunski
     if( mTexture==null )
179
       {
180
       mTexture = new DistortedTexture[NUM];
181 b0ebdf5e Leszek Koltunski
182 e4330c89 Leszek Koltunski
       for(int i=0; i<NUM; i++)
183
         mTexture[i] = new DistortedTexture(bmpWidth,bmpHeight);
184
       }
185 b0ebdf5e Leszek Koltunski
186 e4330c89 Leszek Koltunski
     mTexture[0].setTexture(bitmap0);
187
     mTexture[1].setTexture(bitmap1);
188
     mTexture[2].setTexture(bitmap2);
189 fe59d375 Leszek Koltunski
190 e4330c89 Leszek Koltunski
     if( mMesh==null ) mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
191 e8b6aa95 Leszek Koltunski
192 e4330c89 Leszek Koltunski
     mScreen.detachAll();
193
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture[i], mEffects[i], mMesh);
194 e8b6aa95 Leszek Koltunski
195 e4330c89 Leszek Koltunski
     DistortedEffects.enableEffect(EffectName.SINK);
196
     DistortedEffects.enableEffect(EffectName.DISTORT);
197 6637d0f2 Leszek Koltunski
198 e4330c89 Leszek Koltunski
     try
199
       {
200
       Distorted.onCreate(mView.getContext());
201
       }
202
     catch(Exception ex)
203
       {
204
       android.util.Log.e("Renderer", ex.getMessage() );
205
       }
206
     }
207 427ab7bf Leszek Koltunski
}