Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 6c097dd3

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 int bmpHeight, bmpWidth;
57
    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

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

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

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

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

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

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

    
157
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
158
      sink.add(new Static1D( 1));
159
      sink.add(new Static1D(10));
160

    
161
      bmp[0].sink(sink, pLeft, RegionEye);
162
      bmp[0].sink(sink, pRight,RegionEye);
163
      bmp[1].distort(mDI, pNose1);
164

    
165
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
166
      chromaDyn.add(new Static1D(0));
167
      chromaDyn.add(new Static1D(1));
168

    
169
      bmp[2].chroma(chromaDyn, new Static3D(0,1,0) );
170
      
171
      try
172
        {
173
        Distorted.onSurfaceCreated(mView.getContext());
174
        }
175
      catch(Exception ex)
176
        {
177
        android.util.Log.e("DifferentEffects", ex.getMessage() );
178
        }
179
      }
180
}
(2-2/3)