Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ 08eabc44

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.Float2D;
34
import org.distorted.library.type.Float4D;
35

    
36
import android.graphics.Bitmap;
37
import android.graphics.BitmapFactory;
38
import android.opengl.GLES20;
39
import android.opengl.GLSurfaceView;
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
class SinkRenderer implements GLSurfaceView.Renderer 
44
  {
45
  private GLSurfaceView mView;
46
  private DistortedBitmap sinkBmp;
47
  private Float2D pLeft, pRight;
48
  private Float4D Region;
49
  private int bmpHeight, bmpWidth;
50
    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  public SinkRenderer(GLSurfaceView v) 
54
    { 
55
    mView = v;
56
      
57
    pLeft = new Float2D(214, 206);
58
    pRight= new Float2D(390, 212);
59
    Region= new Float4D(0,0,60,60);
60
    }
61

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

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
    
74
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
75
    { 
76
    sinkBmp.abortEffects(EffectTypes.MATRIX);
77
         
78
    if( bmpHeight/bmpWidth > height/width )
79
      {
80
      int w = (height*bmpWidth)/bmpHeight;
81
      sinkBmp.move((width-w)/2 ,0, 0);
82
      sinkBmp.scale((float)height/bmpHeight);
83
      }
84
    else
85
      {
86
      int h = (width*bmpHeight)/bmpWidth;
87
      sinkBmp.move(0 ,(height-h)/2, 0);
88
      sinkBmp.scale((float)width/bmpWidth);
89
      }
90
      
91
    Distorted.onSurfaceChanged(width, height); 
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
    
96
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
97
    {
98
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
99
    Bitmap bitmap;
100
        
101
    try 
102
      {
103
      bitmap = BitmapFactory.decodeStream(is);
104
      } 
105
    finally 
106
      {
107
      try 
108
        {
109
        is.close();
110
        } 
111
      catch(IOException e) { }
112
      }  
113
      
114
    bmpHeight = bitmap.getHeight();
115
    bmpWidth  = bitmap.getWidth();
116
      
117
    sinkBmp = new DistortedBitmap(bitmap, 30);
118
    sinkBmp.sink( 10.0f, Region, pLeft, 2000, 0.0f);
119
    sinkBmp.sink(10.0f, Region, pRight,2000, 0.0f);
120
      
121
    try
122
      {
123
      Distorted.onSurfaceCreated(mView.getContext());
124
      }
125
    catch(Exception ex)
126
      {
127
      android.util.Log.e("Sink", ex.getMessage() );
128
      }
129
    }
130
  }
(2-2/3)