Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ 08eabc44

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.bean;
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.EffectTypes;
31
import org.distorted.library.type.Interpolator2D;
32
import org.distorted.library.Distorted;
33
import org.distorted.library.DistortedBitmap;
34
import org.distorted.library.type.Float2D;
35
import org.distorted.library.type.Float4D;
36

    
37
import android.graphics.Bitmap;
38
import android.graphics.BitmapFactory;
39
import android.opengl.GLES20;
40
import android.opengl.GLSurfaceView;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
class BeanRenderer implements GLSurfaceView.Renderer 
45
{
46
   private GLSurfaceView mView;
47
   private DistortedBitmap mBean;
48
   private Float2D pLeft, pRight;
49
   private Interpolator2D iLeft, iRight;
50
   private Float4D rLeft, rRight;
51
   private int bmpHeight, bmpWidth;
52
    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
   public BeanRenderer(GLSurfaceView v) 
56
      {
57
      mView = v;
58
     
59
      pLeft = new Float2D(100, 199);
60
      pRight= new Float2D(230, 150);
61
      
62
      rLeft = new Float4D(-9,-31,35,35);
63
      rRight= new Float4D(-9,-31,35,35);
64
     
65
      iLeft = new Interpolator2D();
66
      iRight= new Interpolator2D();
67
     
68
      iLeft.setCount(0.0f);
69
      iRight.setCount(0.0f);
70
      iLeft.setDuration(1500);
71
      iRight.setDuration(1500);
72
      
73
      Float2D p1 = new Float2D(0,0);
74
      Float2D p2 = new Float2D(-10,-34);
75
      
76
      iLeft.add(p1);
77
      iLeft.add(p1);
78
      iLeft.add(p1);
79
      iLeft.add(p1);
80
      iLeft.add(p2);
81
      iLeft.add(p2);
82
      
83
      iRight.add(p1);
84
      iRight.add(p2);
85
      iRight.add(p2);
86
      iRight.add(p1);
87
      iRight.add(p1);
88
      iRight.add(p1);
89
      }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
   
93
    public void onDrawFrame(GL10 glUnused) 
94
      {
95
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
96
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
97
      
98
      mBean.draw(System.currentTimeMillis());
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
    
103
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
104
      { 
105
      mBean.abortEffects(EffectTypes.MATRIX);
106
         
107
      if( bmpHeight/bmpWidth > height/width )
108
        {
109
        int w = (height*bmpWidth)/bmpHeight;
110
        mBean.move((width-w)/2 ,0, 0);
111
        mBean.scale((float)height/bmpHeight);
112
        }
113
      else
114
        {
115
        int h = (width*bmpHeight)/bmpWidth;
116
        mBean.move(0 ,(height-h)/2, 0);
117
        mBean.scale((float)width/bmpWidth);
118
        }
119
      
120
      Distorted.onSurfaceChanged(width, height); 
121
      }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
    
125
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
126
      {
127
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
128
      Bitmap bitmap;
129
        
130
      try 
131
        {
132
        bitmap = BitmapFactory.decodeStream(is);
133
        } 
134
      finally 
135
        {
136
        try 
137
          {
138
          is.close();
139
          } 
140
        catch(IOException e) { }
141
        }  
142
      
143
      bmpHeight = bitmap.getHeight();
144
      bmpWidth  = bitmap.getWidth();
145
      
146
      mBean = new DistortedBitmap(bitmap, 10);     
147
      mBean.distort(iLeft , rLeft , pLeft );
148
      mBean.distort(iRight, rRight, pRight);
149
      
150
      try
151
        {
152
        Distorted.onSurfaceCreated(mView.getContext());
153
        }
154
      catch(Exception ex)
155
        {
156
        android.util.Log.e("Bean", ex.getMessage() );
157
        }
158
      }
159
}
(2-2/3)