Project

General

Profile

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

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

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.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectSink;
32
import org.distorted.library.main.DistortedLibrary;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.mesh.MeshRectangles;
36
import org.distorted.library.main.DistortedTexture;
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 MeshRectangles mMesh;
54
  private Static3D mScale;
55
  private float mBmpRatio;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
  SinkRenderer(GLSurfaceView v)
60
    { 
61
    mView    = v;
62
    mTexture = new DistortedTexture();
63
    mEffects = new DistortedEffects();
64
    mScreen  = new DistortedScreen();
65

    
66
    Dynamic1D sinkStrength = new Dynamic1D(2000,0.0f);
67
    sinkStrength.add(new Static1D(1.0f));
68
    sinkStrength.add(new Static1D(0.2f));
69

    
70
    // strength [changing in time from 1.0 to 0.2 and back],
71
    // center   [(0,0,0), i.e. center of the bitmap],
72
    // region   [null - apply to the whole bitmap
73
    VertexEffectSink sinkEffect = new VertexEffectSink(sinkStrength, new Static3D(0,0,0), null);
74
    mEffects.apply(sinkEffect);
75

    
76
    mScale = new Static3D(1,1,1);
77
    mEffects.apply(new MatrixEffectScale(mScale));
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
   
82
  public void onDrawFrame(GL10 glUnused) 
83
    {
84
    mScreen.render( System.currentTimeMillis() );
85
    }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
    
89
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
90
    {
91
    if( width<height ) mScale.set( width,   width*mBmpRatio, 1 );
92
    else               mScale.set( height/mBmpRatio, height, 1 );
93

    
94
    mScreen.resize(width, height);
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
    
99
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
100
    {
101
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
102
    Bitmap bitmap;
103
        
104
    try 
105
      {
106
      bitmap = BitmapFactory.decodeStream(is);
107
      } 
108
    finally 
109
      {
110
      try 
111
        {
112
        is.close();
113
        } 
114
      catch(IOException e) { }
115
      }  
116
      
117
    mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
118

    
119
    mTexture.setTexture(bitmap);
120

    
121
    if( mMesh==null ) mMesh = new MeshRectangles(30,(int)(30*mBmpRatio));
122

    
123
    mScreen.detachAll();
124
    mScreen.attach(mTexture,mEffects,mMesh);
125

    
126
    VertexEffectSink.enable();
127

    
128
    try
129
      {
130
      DistortedLibrary.onCreate(mView.getContext());
131
      }
132
    catch(Exception ex)
133
      {
134
      android.util.Log.e("Sink", ex.getMessage() );
135
      }
136
    }
137
  }
(2-2/3)