Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ 7451c98a

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