Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 59759251

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.differenteffects;
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 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
31
import org.distorted.library.DistortedBitmap;
32 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
33 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
34 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
35 59759251 Leszek Koltunski
import org.distorted.library.type.Static1D;
36 7589635e Leszek Koltunski
import org.distorted.library.type.Static2D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39 427ab7bf Leszek Koltunski
40
import android.graphics.Bitmap;
41
import android.graphics.BitmapFactory;
42
import android.opengl.GLES20;
43
import android.opengl.GLSurfaceView;
44
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47
class DifferentEffectsRenderer implements GLSurfaceView.Renderer 
48
{
49
   private static final int NUM = 3;
50
   
51
   private GLSurfaceView mView;
52
   private DistortedBitmap[] bmp;
53 7589635e Leszek Koltunski
   private Static2D pLeft, pRight, pNose1;
54
   private Static4D RegionEye;
55
   private Dynamic3D mDI;
56
   private Static3D[] vec;
57 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
58
    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61
   public DifferentEffectsRenderer(GLSurfaceView v) 
62
      {     
63
      mView = v;
64
      
65
      // bmp[0] effects
66 7589635e Leszek Koltunski
      pLeft = new Static2D(214, 206);
67
      pRight= new Static2D(390, 212);
68
      RegionEye = new Static4D(0,0,60,60);
69 427ab7bf Leszek Koltunski
      
70
      // bmp[1] effects
71 7589635e Leszek Koltunski
      mDI = new Dynamic3D();
72 427ab7bf Leszek Koltunski
      mDI.setCount(0.0f);
73
      mDI.setDuration(1000);
74 7589635e Leszek Koltunski
      vec = new Static3D[2];
75
      vec[0] = new Static3D( 50,0,0);
76
      vec[1] = new Static3D(-50,0,0);
77 427ab7bf Leszek Koltunski
      mDI.add(vec[0]);
78
      mDI.add(vec[1]);
79 7589635e Leszek Koltunski
      pNose1 = new Static2D(305, 340);
80 427ab7bf Leszek Koltunski
      
81
      // we don't need to prepare anything for bmp[2] effects
82
      }
83
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85
   
86
    public void onDrawFrame(GL10 glUnused) 
87
      {
88
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
89
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
90
      
91
      long time = System.currentTimeMillis();
92
   
93
      for(int i=NUM-1; i>=0; i--) bmp[i].draw(time);
94
      }
95
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
    
98
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
99
      { 
100
      for(int i=NUM-1; i>=0; i--) 
101
        {   
102 a8c3ada7 Leszek Koltunski
        bmp[i].abortEffects(EffectTypes.MATRIX);
103 427ab7bf Leszek Koltunski
        }
104
      
105
      if( bmpHeight/(NUM*bmpWidth) > height/width )
106
        {
107
        int w = (height*bmpWidth)/bmpHeight;
108 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
109
        Static3D scaleFactor = new Static3D(factor,factor,factor);
110
111 427ab7bf Leszek Koltunski
        for(int i=NUM-1; i>=0; i--) 
112
          {
113 7589635e Leszek Koltunski
          bmp[i].move( new Static3D((width-NUM*w)/2 +i*w , 0, 0) );
114
          bmp[i].scale(scaleFactor);
115 427ab7bf Leszek Koltunski
          }
116
        }
117
      else
118
        {
119
        int w = width/NUM;  
120
        int h = (width*bmpHeight)/(bmpWidth*NUM);
121 7589635e Leszek Koltunski
        float factor = (float)width/(bmpWidth*NUM);
122
        Static3D scaleFactor = new Static3D(factor,factor,factor);
123
124 427ab7bf Leszek Koltunski
        for(int i=NUM-1; i>=0; i--) 
125
          {
126 7589635e Leszek Koltunski
          bmp[i].move( new Static3D(i*w, (height-h)/2, 0) );
127
          bmp[i].scale(scaleFactor);
128 427ab7bf Leszek Koltunski
          }
129
        }
130
       
131
      Distorted.onSurfaceChanged(width, height); 
132
      }
133
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
    
136
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
137
      {
138
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
139
      Bitmap bitmap;
140
        
141
      try 
142
        {
143
        bitmap = BitmapFactory.decodeStream(is);
144
        } 
145
      finally 
146
        {
147
        try 
148
          {
149
          is.close();
150
          } 
151
        catch(IOException e) { }
152
        }  
153
      
154
      bmpHeight = bitmap.getHeight();
155
      bmpWidth  = bitmap.getWidth();
156
      
157
      bmp = new DistortedBitmap[NUM];
158
      bmp[0] = new DistortedBitmap(bmpWidth, bmpHeight, 30);
159
      
160
      for(int i=1; i<NUM; i++) bmp[i] = new DistortedBitmap(bmp[0], Distorted.CLONE_BITMAP);
161
      
162
      // setting the bitmap once is enough; others are cloned!
163
      bmp[0].setBitmap(bitmap);
164
         
165
      bmp[0].sink(10.0f, RegionEye, pLeft , 2000, 0.0f);
166
      bmp[0].sink(10.0f, RegionEye, pRight, 2000, 0.0f);
167
      bmp[1].distort(mDI, pNose1);
168 59759251 Leszek Koltunski
169
      Dynamic1D macroblockDyn = new Dynamic1D();
170
      macroblockDyn.setDuration(3000);
171
      macroblockDyn.setCount(0);
172
      macroblockDyn.add(new Static1D(1));
173
      macroblockDyn.add(new Static1D(50));
174
175
      bmp[2].macroblock(macroblockDyn);
176 427ab7bf Leszek Koltunski
      
177
      try
178
        {
179 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
180 427ab7bf Leszek Koltunski
        }
181
      catch(Exception ex)
182
        {
183 ed1c0b33 Leszek Koltunski
        android.util.Log.e("DifferentEffects", ex.getMessage() );
184 427ab7bf Leszek Koltunski
        }
185
      }
186
}