Project

General

Profile

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

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

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