Project

General

Profile

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

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

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

    
40
import android.graphics.Bitmap;
41
import android.graphics.BitmapFactory;
42
import android.opengl.GLES30;
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 int bmpHeight, bmpWidth;
53
    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

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

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

    
67
    mScreen = new DistortedScreen();
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
   
72
  public void onDrawFrame(GL10 glUnused) 
73
    {
74
    GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
75
    mScreen.render( System.currentTimeMillis() );
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
    
80
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
81
    { 
82
    mEffects.abortEffects(EffectTypes.MATRIX);
83
         
84
    if( (float)bmpHeight/bmpWidth > (float)height/width )
85
      {
86
      int w = (height*bmpWidth)/bmpHeight;
87
      float factor = (float)height/bmpHeight;
88

    
89
      mEffects.move( new Static3D((width-w)/2,0,0) );
90
      mEffects.scale( factor );
91
      }
92
    else
93
      {
94
      int h = (width*bmpHeight)/bmpWidth;
95
      float factor = (float)width/bmpWidth;
96

    
97
      mEffects.move( new Static3D(0,(height-h)/2,0) );
98
      mEffects.scale( factor );
99
      }
100
      
101
    mScreen.resize(width, height);
102
    }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
    
106
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
107
    {
108
    GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
109

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

    
129
    MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
130
    DistortedTexture texture = new DistortedTexture(bmpWidth,bmpHeight);
131
    texture.setTexture(bitmap);
132

    
133
    mScreen.detachAll();
134
    mScreen.attach(texture,mEffects,mesh);
135

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