Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ 7589635e

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 7589635e Leszek Koltunski
import org.distorted.library.type.Static2D;
34
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36 427ab7bf Leszek Koltunski
37
import android.graphics.Bitmap;
38
import android.graphics.BitmapFactory;
39
import android.opengl.GLES20;
40
import android.opengl.GLSurfaceView;
41
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44
class SinkRenderer implements GLSurfaceView.Renderer 
45 bc0a685b Leszek Koltunski
  {
46
  private GLSurfaceView mView;
47
  private DistortedBitmap sinkBmp;
48 7589635e Leszek Koltunski
  private Static2D pLeft, pRight;
49
  private Static4D Region;
50 bc0a685b Leszek Koltunski
  private int bmpHeight, bmpWidth;
51 427ab7bf Leszek Koltunski
    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54 bc0a685b Leszek Koltunski
  public SinkRenderer(GLSurfaceView v) 
55
    { 
56
    mView = v;
57 427ab7bf Leszek Koltunski
      
58 7589635e Leszek Koltunski
    pLeft = new Static2D(214, 206);
59
    pRight= new Static2D(390, 212);
60
    Region= new Static4D(0,0,60,60);
61 bc0a685b Leszek Koltunski
    }
62 427ab7bf Leszek Koltunski
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
   
65 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
66
    {
67
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
68
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
69 427ab7bf Leszek Koltunski
      
70 bc0a685b Leszek Koltunski
    sinkBmp.draw(System.currentTimeMillis());
71
    }
72 427ab7bf Leszek Koltunski
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
    
75 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
76
    { 
77
    sinkBmp.abortEffects(EffectTypes.MATRIX);
78 427ab7bf Leszek Koltunski
         
79 bc0a685b Leszek Koltunski
    if( bmpHeight/bmpWidth > height/width )
80
      {
81
      int w = (height*bmpWidth)/bmpHeight;
82 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
83
84
      sinkBmp.move( new Static3D((width-w)/2,0,0) );
85
      sinkBmp.scale( new Static3D(factor,factor,factor) );
86 bc0a685b Leszek Koltunski
      }
87
    else
88
      {
89
      int h = (width*bmpHeight)/bmpWidth;
90 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
91
92
      sinkBmp.move( new Static3D(0,(height-h)/2,0) );
93
      sinkBmp.scale( new Static3D(factor,factor,factor) );
94 427ab7bf Leszek Koltunski
      }
95 bc0a685b Leszek Koltunski
      
96
    Distorted.onSurfaceChanged(width, height); 
97
    }
98 427ab7bf Leszek Koltunski
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
    
101 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
102
    {
103
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
104
    Bitmap bitmap;
105 427ab7bf Leszek Koltunski
        
106 bc0a685b Leszek Koltunski
    try 
107
      {
108
      bitmap = BitmapFactory.decodeStream(is);
109
      } 
110
    finally 
111
      {
112 427ab7bf Leszek Koltunski
      try 
113
        {
114 bc0a685b Leszek Koltunski
        is.close();
115 427ab7bf Leszek Koltunski
        } 
116 bc0a685b Leszek Koltunski
      catch(IOException e) { }
117
      }  
118 427ab7bf Leszek Koltunski
      
119 bc0a685b Leszek Koltunski
    bmpHeight = bitmap.getHeight();
120
    bmpWidth  = bitmap.getWidth();
121 427ab7bf Leszek Koltunski
      
122 bc0a685b Leszek Koltunski
    sinkBmp = new DistortedBitmap(bitmap, 30);
123
    sinkBmp.sink( 10.0f, Region, pLeft, 2000, 0.0f);
124
    sinkBmp.sink(10.0f, Region, pRight,2000, 0.0f);
125 427ab7bf Leszek Koltunski
      
126 bc0a685b Leszek Koltunski
    try
127
      {
128
      Distorted.onSurfaceCreated(mView.getContext());
129
      }
130
    catch(Exception ex)
131
      {
132
      android.util.Log.e("Sink", ex.getMessage() );
133 427ab7bf Leszek Koltunski
      }
134 bc0a685b Leszek Koltunski
    }
135
  }