Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
20
package org.distorted.examples.sink;
21

    
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
import org.distorted.examples.R;
29

    
30
import org.distorted.library.Distorted;
31
import org.distorted.library.GridFlat;
32
import org.distorted.library.DistortedObject;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Static1D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38

    
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
  {
48
  private GLSurfaceView mView;
49
  private DistortedObject mObject;
50
  private GridFlat mGrid;
51
  private Static3D pLeft, pRight;
52
  private Static4D Region;
53
  private int bmpHeight, bmpWidth;
54
    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  SinkRenderer(GLSurfaceView v)
58
    { 
59
    mView = v;
60
      
61
    pLeft = new Static3D(214, 206, 0);
62
    pRight= new Static3D(390, 212, 0);
63
    Region= new Static4D(0,0,60,60);
64
    }
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67
   
68
  public void onDrawFrame(GL10 glUnused) 
69
    {
70
    GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
71
    mObject.draw(System.currentTimeMillis(), mGrid);
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
    
76
  public void onSurfaceChanged(GL10 glUnused, int width, int height) 
77
    { 
78
    mObject.abortEffects(EffectTypes.MATRIX);
79
         
80
    if( (float)bmpHeight/bmpWidth > (float)height/width )
81
      {
82
      int w = (height*bmpWidth)/bmpHeight;
83
      float factor = (float)height/bmpHeight;
84

    
85
      mObject.move( new Static3D((width-w)/2,0,0) );
86
      mObject.scale( factor );
87
      }
88
    else
89
      {
90
      int h = (width*bmpHeight)/bmpWidth;
91
      float factor = (float)width/bmpWidth;
92

    
93
      mObject.move( new Static3D(0,(height-h)/2,0) );
94
      mObject.scale( factor );
95
      }
96
      
97
    Distorted.onSurfaceChanged(width, height); 
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
    
102
  public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
103
    {
104
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
105

    
106
    InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
107
    Bitmap bitmap;
108
        
109
    try 
110
      {
111
      bitmap = BitmapFactory.decodeStream(is);
112
      } 
113
    finally 
114
      {
115
      try 
116
        {
117
        is.close();
118
        } 
119
      catch(IOException e) { }
120
      }  
121
      
122
    bmpHeight = bitmap.getHeight();
123
    bmpWidth  = bitmap.getWidth();
124

    
125
    Dynamic1D dSink = new Dynamic1D(2000,0.0f);
126
    dSink.add(new Static1D( 1));
127
    dSink.add(new Static1D(10));
128

    
129
    mGrid = new GridFlat(30,30*bmpHeight/bmpWidth);
130
    mObject = new DistortedObject(bmpWidth,bmpHeight,1);
131
    mObject.setTexture(bitmap);
132
    
133
    mObject.sink( dSink, pLeft, Region);
134
    mObject.sink( dSink, pRight,Region);
135
      
136
    try
137
      {
138
      Distorted.onSurfaceCreated(mView.getContext());
139
      }
140
    catch(Exception ex)
141
      {
142
      android.util.Log.e("Sink", ex.getMessage() );
143
      }
144
    }
145
  }
(2-2/3)