Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ 5055b5d4

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 334c13fa Leszek Koltunski
  private Static3D pLeft, pRight;
51 7589635e Leszek Koltunski
  private Static4D Region;
52 bc0a685b Leszek Koltunski
  private int bmpHeight, bmpWidth;
53 427ab7bf Leszek Koltunski
    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 e7a4ef16 Leszek Koltunski
  SinkRenderer(GLSurfaceView v)
57 bc0a685b Leszek Koltunski
    { 
58
    mView = v;
59 427ab7bf Leszek Koltunski
      
60 334c13fa Leszek Koltunski
    pLeft = new Static3D(214, 206, 0);
61
    pRight= new Static3D(390, 212, 0);
62 7589635e Leszek Koltunski
    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.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
70
    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 5055b5d4 Leszek Koltunski
    if( (float)bmpHeight/bmpWidth > (float)height/width )
80 bc0a685b Leszek Koltunski
      {
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 f988589e Leszek Koltunski
      sinkBmp.scale( 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 f988589e Leszek Koltunski
      sinkBmp.scale( 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 e7a4ef16 Leszek Koltunski
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
104
105 bc0a685b Leszek Koltunski
    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
  }