Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
20
package org.distorted.examples.sink;
21

    
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
import org.distorted.examples.R;
29

    
30
import org.distorted.library.Distorted;
31
import org.distorted.library.GridFlat;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.DistortedEffectQueues;
34
import org.distorted.library.EffectTypes;
35
import org.distorted.library.type.Dynamic1D;
36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
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
  {
49
  private GLSurfaceView mView;
50
  private DistortedTexture mTexture;
51
  private DistortedEffectQueues mQueues;
52
  private GridFlat mGrid;
53
  private int bmpHeight, bmpWidth;
54
    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  SinkRenderer(GLSurfaceView v)
58
    { 
59
    mView = v;
60
      
61
    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
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
   
76
  public void onDrawFrame(GL10 glUnused) 
77
    {
78
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
79
    mQueues.draw(System.currentTimeMillis(), mTexture,mGrid);
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
    
84
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
85
    { 
86
    mQueues.abortEffects(EffectTypes.MATRIX);
87
         
88
    if( (float)bmpHeight/bmpWidth > (float)height/width )
89
      {
90
      int w = (height*bmpWidth)/bmpHeight;
91
      float factor = (float)height/bmpHeight;
92

    
93
      mQueues.move( new Static3D((width-w)/2,0,0) );
94
      mQueues.scale( factor );
95
      }
96
    else
97
      {
98
      int h = (width*bmpHeight)/bmpWidth;
99
      float factor = (float)width/bmpWidth;
100

    
101
      mQueues.move( new Static3D(0,(height-h)/2,0) );
102
      mQueues.scale( factor );
103
      }
104
      
105
    Distorted.onSurfaceChanged(width, height); 
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
    
110
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
111
    {
112
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
113

    
114
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
115
    Bitmap bitmap;
116
        
117
    try 
118
      {
119
      bitmap = BitmapFactory.decodeStream(is);
120
      } 
121
    finally 
122
      {
123
      try 
124
        {
125
        is.close();
126
        } 
127
      catch(IOException e) { }
128
      }  
129
      
130
    bmpHeight = bitmap.getHeight();
131
    bmpWidth  = bitmap.getWidth();
132

    
133
    mGrid    = new GridFlat(30,30*bmpHeight/bmpWidth);
134
    mTexture = new DistortedTexture(bmpWidth,bmpHeight,0);
135
    mTexture.setTexture(bitmap);
136

    
137
    try
138
      {
139
      Distorted.onSurfaceCreated(mView.getContext());
140
      }
141
    catch(Exception ex)
142
      {
143
      android.util.Log.e("Sink", ex.getMessage() );
144
      }
145
    }
146
  }
(2-2/3)