Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ 3873e8e6

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