Project

General

Profile

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

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

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.sink;
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 5068fa06 Leszek Koltunski
import org.distorted.library.Float2D;
34
import org.distorted.library.Float4D;
35 427ab7bf Leszek Koltunski
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 bc0a685b Leszek Koltunski
  {
45
  private GLSurfaceView mView;
46
  private DistortedBitmap sinkBmp;
47
  private Float2D pLeft, pRight;
48
  private Float4D Region;
49
  private int bmpHeight, bmpWidth;
50 427ab7bf Leszek Koltunski
    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 bc0a685b Leszek Koltunski
  public SinkRenderer(GLSurfaceView v) 
54
    { 
55
    mView = v;
56 427ab7bf Leszek Koltunski
      
57 bc0a685b Leszek Koltunski
    pLeft = new Float2D(214, 206);
58
    pRight= new Float2D(390, 212);
59
    Region= new Float4D(0,0,60,60);
60
    }
61 427ab7bf Leszek Koltunski
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
   
64 bc0a685b Leszek Koltunski
  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 427ab7bf Leszek Koltunski
      
69 bc0a685b Leszek Koltunski
    sinkBmp.draw(System.currentTimeMillis());
70
    }
71 427ab7bf Leszek Koltunski
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
    
74 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
75
    { 
76
    sinkBmp.abortEffects(EffectTypes.MATRIX);
77 427ab7bf Leszek Koltunski
         
78 bc0a685b Leszek Koltunski
    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 427ab7bf Leszek Koltunski
      }
90 bc0a685b Leszek Koltunski
      
91
    Distorted.onSurfaceChanged(width, height); 
92
    }
93 427ab7bf Leszek Koltunski
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
    
96 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
97
    {
98
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
99
    Bitmap bitmap;
100 427ab7bf Leszek Koltunski
        
101 bc0a685b Leszek Koltunski
    try 
102
      {
103
      bitmap = BitmapFactory.decodeStream(is);
104
      } 
105
    finally 
106
      {
107 427ab7bf Leszek Koltunski
      try 
108
        {
109 bc0a685b Leszek Koltunski
        is.close();
110 427ab7bf Leszek Koltunski
        } 
111 bc0a685b Leszek Koltunski
      catch(IOException e) { }
112
      }  
113 427ab7bf Leszek Koltunski
      
114 bc0a685b Leszek Koltunski
    bmpHeight = bitmap.getHeight();
115
    bmpWidth  = bitmap.getWidth();
116 427ab7bf Leszek Koltunski
      
117 bc0a685b Leszek Koltunski
    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 427ab7bf Leszek Koltunski
      
121 bc0a685b Leszek Koltunski
    try
122
      {
123
      Distorted.onSurfaceCreated(mView.getContext());
124
      }
125
    catch(Exception ex)
126
      {
127
      android.util.Log.e("Sink", ex.getMessage() );
128 427ab7bf Leszek Koltunski
      }
129 bc0a685b Leszek Koltunski
    }
130
  }