Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ 392e16fd

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 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
32 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
33 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
34
import org.distorted.library.DistortedEffectQueues;
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
import org.distorted.library.type.Static4D;
40 427ab7bf Leszek Koltunski
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLES20;
44
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 392e16fd Leszek Koltunski
  private DistortedEffectQueues mEffects;
53
  private DistortedFramebuffer mScreen;
54 10b7e588 Leszek Koltunski
  private GridFlat mGrid;
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 427ab7bf Leszek Koltunski
      
63 f6d884d5 Leszek Koltunski
    Static3D pLeft = new Static3D(214, 206, 0);
64
    Static3D pRight= new Static3D(390, 212, 0);
65
    Static4D Region= new Static4D(0,0,60,60);
66
67
    Dynamic1D dSink = new Dynamic1D(2000,0.0f);
68
    dSink.add(new Static1D( 1));
69
    dSink.add(new Static1D(10));
70
71 392e16fd Leszek Koltunski
    mEffects = new DistortedEffectQueues();
72
    mEffects.sink( dSink, pLeft, Region);
73
    mEffects.sink( dSink, pRight,Region);
74
75
    mScreen = new DistortedFramebuffer(0);
76 bc0a685b Leszek Koltunski
    }
77 427ab7bf Leszek Koltunski
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
   
80 bc0a685b Leszek Koltunski
  public void onDrawFrame(GL10 glUnused) 
81
    {
82
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
83 392e16fd Leszek Koltunski
    mEffects.draw(System.currentTimeMillis(), mTexture,mGrid,mScreen);
84 bc0a685b Leszek Koltunski
    }
85 427ab7bf Leszek Koltunski
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
    
88 bc0a685b Leszek Koltunski
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
89
    { 
90 392e16fd Leszek Koltunski
    mEffects.abortEffects(EffectTypes.MATRIX);
91 427ab7bf Leszek Koltunski
         
92 5055b5d4 Leszek Koltunski
    if( (float)bmpHeight/bmpWidth > (float)height/width )
93 bc0a685b Leszek Koltunski
      {
94
      int w = (height*bmpWidth)/bmpHeight;
95 7589635e Leszek Koltunski
      float factor = (float)height/bmpHeight;
96
97 392e16fd Leszek Koltunski
      mEffects.move( new Static3D((width-w)/2,0,0) );
98
      mEffects.scale( factor );
99 bc0a685b Leszek Koltunski
      }
100
    else
101
      {
102
      int h = (width*bmpHeight)/bmpWidth;
103 7589635e Leszek Koltunski
      float factor = (float)width/bmpWidth;
104
105 392e16fd Leszek Koltunski
      mEffects.move( new Static3D(0,(height-h)/2,0) );
106
      mEffects.scale( factor );
107 427ab7bf Leszek Koltunski
      }
108 bc0a685b Leszek Koltunski
      
109 392e16fd Leszek Koltunski
    mScreen.resize(width, height);
110 bc0a685b Leszek Koltunski
    }
111 427ab7bf Leszek Koltunski
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114 bc0a685b Leszek Koltunski
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
115
    {
116 e7a4ef16 Leszek Koltunski
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
117
118 bc0a685b Leszek Koltunski
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
119
    Bitmap bitmap;
120 427ab7bf Leszek Koltunski
        
121 bc0a685b Leszek Koltunski
    try 
122
      {
123
      bitmap = BitmapFactory.decodeStream(is);
124
      } 
125
    finally 
126
      {
127 427ab7bf Leszek Koltunski
      try 
128
        {
129 bc0a685b Leszek Koltunski
        is.close();
130 427ab7bf Leszek Koltunski
        } 
131 bc0a685b Leszek Koltunski
      catch(IOException e) { }
132
      }  
133 427ab7bf Leszek Koltunski
      
134 bc0a685b Leszek Koltunski
    bmpHeight = bitmap.getHeight();
135
    bmpWidth  = bitmap.getWidth();
136 b5cc7760 Leszek Koltunski
137 f6d884d5 Leszek Koltunski
    mGrid    = new GridFlat(30,30*bmpHeight/bmpWidth);
138 7451c98a Leszek Koltunski
    mTexture = new DistortedTexture(bmpWidth,bmpHeight);
139 f6d884d5 Leszek Koltunski
    mTexture.setTexture(bitmap);
140 b5cc7760 Leszek Koltunski
141 bc0a685b Leszek Koltunski
    try
142
      {
143
      Distorted.onSurfaceCreated(mView.getContext());
144
      }
145
    catch(Exception ex)
146
      {
147
      android.util.Log.e("Sink", ex.getMessage() );
148 427ab7bf Leszek Koltunski
      }
149 bc0a685b Leszek Koltunski
    }
150
  }