Project

General

Profile

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

examples / src / main / java / org / distorted / examples / sink / SinkRenderer.java @ 10b7e588

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