Project

General

Profile

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

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

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