Project

General

Profile

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

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

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.DistortedEffects;
32
import org.distorted.library.DistortedScreen;
33
import org.distorted.library.EffectNames;
34
import org.distorted.library.MeshFlat;
35
import org.distorted.library.DistortedTexture;
36
import org.distorted.library.EffectTypes;
37
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static3D;
40

    
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLSurfaceView;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class SinkRenderer implements GLSurfaceView.Renderer 
48
  {
49
  private GLSurfaceView mView;
50
  private DistortedEffects mEffects;
51
  private DistortedScreen mScreen;
52
  private DistortedTexture mTexture;
53
  private int bmpHeight, bmpWidth;
54
    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  SinkRenderer(GLSurfaceView v)
58
    { 
59
    mView = v;
60

    
61
    Dynamic1D sink = new Dynamic1D(2000,0.0f);
62
    sink.add(new Static1D(1.0f));
63
    sink.add(new Static1D(0.2f));
64

    
65
    mEffects = new DistortedEffects();
66
    mEffects.sink( sink, new Static3D(297, 320, 0), null);
67

    
68
    mScreen = new DistortedScreen();
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
   
73
  public void onDrawFrame(GL10 glUnused) 
74
    {
75
    mScreen.render( System.currentTimeMillis() );
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
    
80
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
81
    {
82
    float qx = (float)width /bmpWidth;
83
    float qy = (float)height/bmpHeight;
84

    
85
    mEffects.abortEffects(EffectTypes.MATRIX);
86
    mEffects.scale(  qx<qy ? (new Static3D(1,qx/qy,1)) : (new Static3D(qy/qx,1,1)) );
87

    
88
    mScreen.resize(width, height);
89
    }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
    
93
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
94
    {
95
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
96
    Bitmap bitmap;
97
        
98
    try 
99
      {
100
      bitmap = BitmapFactory.decodeStream(is);
101
      } 
102
    finally 
103
      {
104
      try 
105
        {
106
        is.close();
107
        } 
108
      catch(IOException e) { }
109
      }  
110
      
111
    bmpHeight = bitmap.getHeight();
112
    bmpWidth  = bitmap.getWidth();
113

    
114
    if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
115
    mTexture.setTexture(bitmap);
116

    
117
    mScreen.detachAll();
118
    mScreen.attach(mTexture,mEffects,new MeshFlat(30,30*bmpHeight/bmpWidth));
119

    
120
    DistortedEffects.enableEffect(EffectNames.SINK);
121

    
122
    try
123
      {
124
      Distorted.onCreate(mView.getContext());
125
      }
126
    catch(Exception ex)
127
      {
128
      android.util.Log.e("Sink", ex.getMessage() );
129
      }
130
    }
131
  }
(2-2/3)