Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ b5cc7760

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.sink;
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.Static1D;
35
import org.distorted.library.type.Static2D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38

    
39
import android.graphics.Bitmap;
40
import android.graphics.BitmapFactory;
41
import android.opengl.GLES20;
42
import android.opengl.GLSurfaceView;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
class SinkRenderer implements GLSurfaceView.Renderer 
47
  {
48
  private GLSurfaceView mView;
49
  private DistortedBitmap sinkBmp;
50
  private Static2D pLeft, pRight;
51
  private Static4D Region;
52
  private int bmpHeight, bmpWidth;
53
    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  public SinkRenderer(GLSurfaceView v) 
57
    { 
58
    mView = v;
59
      
60
    pLeft = new Static2D(214, 206);
61
    pRight= new Static2D(390, 212);
62
    Region= new Static4D(0,0,60,60);
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
   
67
  public void onDrawFrame(GL10 glUnused) 
68
    {
69
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
70
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
71
      
72
    sinkBmp.draw(System.currentTimeMillis());
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
    
77
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
78
    { 
79
    sinkBmp.abortEffects(EffectTypes.MATRIX);
80
         
81
    if( bmpHeight/bmpWidth > height/width )
82
      {
83
      int w = (height*bmpWidth)/bmpHeight;
84
      float factor = (float)height/bmpHeight;
85

    
86
      sinkBmp.move( new Static3D((width-w)/2,0,0) );
87
      sinkBmp.scale( new Static3D(factor,factor,factor) );
88
      }
89
    else
90
      {
91
      int h = (width*bmpHeight)/bmpWidth;
92
      float factor = (float)width/bmpWidth;
93

    
94
      sinkBmp.move( new Static3D(0,(height-h)/2,0) );
95
      sinkBmp.scale( new Static3D(factor,factor,factor) );
96
      }
97
      
98
    Distorted.onSurfaceChanged(width, height); 
99
    }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
    
103
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
104
    {
105
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
106
    Bitmap bitmap;
107
        
108
    try 
109
      {
110
      bitmap = BitmapFactory.decodeStream(is);
111
      } 
112
    finally 
113
      {
114
      try 
115
        {
116
        is.close();
117
        } 
118
      catch(IOException e) { }
119
      }  
120
      
121
    bmpHeight = bitmap.getHeight();
122
    bmpWidth  = bitmap.getWidth();
123

    
124
    Dynamic1D dSink = new Dynamic1D();
125
    dSink.setDuration(2000);
126
    dSink.setCount(0);
127
    dSink.add(new Static1D( 1));
128
    dSink.add(new Static1D(10));
129

    
130
    sinkBmp = new DistortedBitmap(bitmap, 30);
131
    sinkBmp.sink( dSink, pLeft, Region);
132
    sinkBmp.sink( dSink, pRight,Region);
133
      
134
    try
135
      {
136
      Distorted.onSurfaceCreated(mView.getContext());
137
      }
138
    catch(Exception ex)
139
      {
140
      android.util.Log.e("Sink", ex.getMessage() );
141
      }
142
    }
143
  }
(2-2/3)