Project

General

Profile

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

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

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.differenteffects;
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.Distorted;
31
import org.distorted.library.DistortedBitmap;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.type.Dynamic1D;
34
import org.distorted.library.type.Dynamic3D;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static2D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
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
   private Static2D pLeft, pRight, pNose1;
54
   private Static4D RegionEye;
55
   private Dynamic3D mDI;
56
   private Static3D[] vec;
57
   private int bmpHeight, bmpWidth;
58
    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
   public DifferentEffectsRenderer(GLSurfaceView v) 
62
      {     
63
      mView = v;
64
      
65
      // bmp[0] effects
66
      pLeft = new Static2D(214, 206);
67
      pRight= new Static2D(390, 212);
68
      RegionEye = new Static4D(0,0,60,60);
69
      
70
      // bmp[1] effects
71
      mDI = new Dynamic3D(1000,0.0f);
72
      vec = new Static3D[2];
73
      vec[0] = new Static3D( 50,0,0);
74
      vec[1] = new Static3D(-50,0,0);
75
      mDI.add(vec[0]);
76
      mDI.add(vec[1]);
77
      pNose1 = new Static2D(305, 340);
78
      
79
      // we don't need to prepare anything for bmp[2] effects
80
      }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
   
84
    public void onDrawFrame(GL10 glUnused) 
85
      {
86
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
87
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
88
      
89
      long time = System.currentTimeMillis();
90
   
91
      for(int i=NUM-1; i>=0; i--) bmp[i].draw(time);
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
    
96
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
97
      { 
98
      for(int i=NUM-1; i>=0; i--) 
99
        {   
100
        bmp[i].abortEffects(EffectTypes.MATRIX);
101
        }
102
      
103
      if( bmpHeight/(NUM*bmpWidth) > height/width )
104
        {
105
        int w = (height*bmpWidth)/bmpHeight;
106
        float factor = (float)height/bmpHeight;
107

    
108
        for(int i=NUM-1; i>=0; i--) 
109
          {
110
          bmp[i].move( new Static3D((width-NUM*w)/2 +i*w , 0, 0) );
111
          bmp[i].scale(factor);
112
          }
113
        }
114
      else
115
        {
116
        int w = width/NUM;  
117
        int h = (width*bmpHeight)/(bmpWidth*NUM);
118
        float factor = (float)width/(bmpWidth*NUM);
119

    
120
        for(int i=NUM-1; i>=0; i--) 
121
          {
122
          bmp[i].move( new Static3D(i*w, (height-h)/2, 0) );
123
          bmp[i].scale(factor);
124
          }
125
        }
126
       
127
      Distorted.onSurfaceChanged(width, height); 
128
      }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
    
132
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
133
      {
134
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
135
      Bitmap bitmap;
136
        
137
      try 
138
        {
139
        bitmap = BitmapFactory.decodeStream(is);
140
        } 
141
      finally 
142
        {
143
        try 
144
          {
145
          is.close();
146
          } 
147
        catch(IOException e) { }
148
        }  
149
      
150
      bmpHeight = bitmap.getHeight();
151
      bmpWidth  = bitmap.getWidth();
152
      
153
      bmp = new DistortedBitmap[NUM];
154
      bmp[0] = new DistortedBitmap(bmpWidth, bmpHeight, 30);
155
      
156
      for(int i=1; i<NUM; i++) bmp[i] = new DistortedBitmap(bmp[0], Distorted.CLONE_BITMAP);
157
      
158
      // setting the bitmap once is enough; others are cloned!
159
      bmp[0].setBitmap(bitmap);
160

    
161
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
162
      sink.add(new Static1D( 1));
163
      sink.add(new Static1D(10));
164

    
165
      bmp[0].sink(sink, pLeft, RegionEye);
166
      bmp[0].sink(sink, pRight,RegionEye);
167
      bmp[1].distort(mDI, pNose1);
168

    
169
      Dynamic1D macroblockDyn = new Dynamic1D(3000,0.0f);
170
      macroblockDyn.add(new Static1D(1));
171
      macroblockDyn.add(new Static1D(50));
172

    
173
      bmp[2].macroblock(macroblockDyn);
174
      
175
      try
176
        {
177
        Distorted.onSurfaceCreated(mView.getContext());
178
        }
179
      catch(Exception ex)
180
        {
181
        android.util.Log.e("DifferentEffects", ex.getMessage() );
182
        }
183
      }
184
}
(2-2/3)