Project

General

Profile

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

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

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 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
32 d218d64e leszek
import org.distorted.library.DistortedScreen;
33 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
34 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
35 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
36 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
37 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
38
import org.distorted.library.type.Static1D;
39 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
40 427ab7bf Leszek Koltunski
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 bc0a685b Leszek Koltunski
  {
49
  private GLSurfaceView mView;
50 d04a4886 Leszek Koltunski
  private DistortedEffects mEffects;
51 d218d64e leszek
  private DistortedScreen mScreen;
52 b0ebdf5e Leszek Koltunski
  private DistortedTexture mTexture;
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 f6d884d5 Leszek Koltunski
61 13155c68 Leszek Koltunski
    Dynamic1D sink = new Dynamic1D(2000,0.0f);
62
    sink.add(new Static1D(1.0f));
63
    sink.add(new Static1D(0.2f));
64 f6d884d5 Leszek Koltunski
65 d04a4886 Leszek Koltunski
    mEffects = new DistortedEffects();
66 13155c68 Leszek Koltunski
    mEffects.sink( sink, new Static3D(297, 320, 0), null);
67 392e16fd Leszek Koltunski
68 d218d64e leszek
    mScreen = new DistortedScreen();
69 bc0a685b Leszek Koltunski
    }
70 427ab7bf Leszek Koltunski
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72
   
73 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
74
    {
75 fe59d375 Leszek Koltunski
    mScreen.render( System.currentTimeMillis() );
76 bc0a685b Leszek Koltunski
    }
77 427ab7bf Leszek Koltunski
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
    
80 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
81
    { 
82 392e16fd Leszek Koltunski
    mEffects.abortEffects(EffectTypes.MATRIX);
83 427ab7bf Leszek Koltunski
         
84 5055b5d4 Leszek Koltunski
    if( (float)bmpHeight/bmpWidth > (float)height/width )
85 bc0a685b Leszek Koltunski
      {
86
      int w = (height*bmpWidth)/bmpHeight;
87 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
88
89 392e16fd Leszek Koltunski
      mEffects.move( new Static3D((width-w)/2,0,0) );
90
      mEffects.scale( factor );
91 bc0a685b Leszek Koltunski
      }
92
    else
93
      {
94
      int h = (width*bmpHeight)/bmpWidth;
95 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
96
97 392e16fd Leszek Koltunski
      mEffects.move( new Static3D(0,(height-h)/2,0) );
98
      mEffects.scale( factor );
99 427ab7bf Leszek Koltunski
      }
100 bc0a685b Leszek Koltunski
      
101 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
102 bc0a685b Leszek Koltunski
    }
103 427ab7bf Leszek Koltunski
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
    
106 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
107
    {
108 13155c68 Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.cat);
109 bc0a685b Leszek Koltunski
    Bitmap bitmap;
110 427ab7bf Leszek Koltunski
        
111 bc0a685b Leszek Koltunski
    try 
112
      {
113
      bitmap = BitmapFactory.decodeStream(is);
114
      } 
115
    finally 
116
      {
117 427ab7bf Leszek Koltunski
      try 
118
        {
119 bc0a685b Leszek Koltunski
        is.close();
120 427ab7bf Leszek Koltunski
        } 
121 bc0a685b Leszek Koltunski
      catch(IOException e) { }
122
      }  
123 427ab7bf Leszek Koltunski
      
124 bc0a685b Leszek Koltunski
    bmpHeight = bitmap.getHeight();
125
    bmpWidth  = bitmap.getWidth();
126 b5cc7760 Leszek Koltunski
127 b0ebdf5e Leszek Koltunski
    if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
128
    mTexture.setTexture(bitmap);
129 fe59d375 Leszek Koltunski
130
    mScreen.detachAll();
131 b0ebdf5e Leszek Koltunski
    mScreen.attach(mTexture,mEffects,new MeshFlat(30,30*bmpHeight/bmpWidth));
132 b5cc7760 Leszek Koltunski
133 6637d0f2 Leszek Koltunski
    DistortedEffects.enableEffect(EffectNames.SINK);
134
135 bc0a685b Leszek Koltunski
    try
136
      {
137 76f9798b Leszek Koltunski
      Distorted.onCreate(mView.getContext());
138 bc0a685b Leszek Koltunski
      }
139
    catch(Exception ex)
140
      {
141
      android.util.Log.e("Sink", ex.getMessage() );
142 427ab7bf Leszek Koltunski
      }
143 bc0a685b Leszek Koltunski
    }
144
  }