Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ 7589635e

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.bean;
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 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
31 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic2D;
32 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
33
import org.distorted.library.DistortedBitmap;
34 7589635e Leszek Koltunski
import org.distorted.library.type.Static2D;
35
import org.distorted.library.type.Static3D;
36
import org.distorted.library.type.Static4D;
37 427ab7bf Leszek Koltunski
38
import android.graphics.Bitmap;
39
import android.graphics.BitmapFactory;
40
import android.opengl.GLES20;
41
import android.opengl.GLSurfaceView;
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45
class BeanRenderer implements GLSurfaceView.Renderer 
46
{
47
   private GLSurfaceView mView;
48
   private DistortedBitmap mBean;
49 7589635e Leszek Koltunski
   private Static2D pLeft, pRight;
50
   private Dynamic2D iLeft, iRight;
51
   private Static4D rLeft, rRight;
52 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
53
    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56
   public BeanRenderer(GLSurfaceView v) 
57
      {
58
      mView = v;
59
     
60 7589635e Leszek Koltunski
      pLeft = new Static2D(100, 199);
61
      pRight= new Static2D(230, 150);
62 427ab7bf Leszek Koltunski
      
63 7589635e Leszek Koltunski
      rLeft = new Static4D(-9,-31,35,35);
64
      rRight= new Static4D(-9,-31,35,35);
65 427ab7bf Leszek Koltunski
     
66 7589635e Leszek Koltunski
      iLeft = new Dynamic2D();
67
      iRight= new Dynamic2D();
68 427ab7bf Leszek Koltunski
     
69
      iLeft.setCount(0.0f);
70
      iRight.setCount(0.0f);
71
      iLeft.setDuration(1500);
72
      iRight.setDuration(1500);
73
      
74 7589635e Leszek Koltunski
      Static2D p1 = new Static2D(0,0);
75
      Static2D p2 = new Static2D(-10,-34);
76 427ab7bf Leszek Koltunski
      
77
      iLeft.add(p1);
78
      iLeft.add(p1);
79
      iLeft.add(p1);
80
      iLeft.add(p1);
81
      iLeft.add(p2);
82
      iLeft.add(p2);
83
      
84
      iRight.add(p1);
85
      iRight.add(p2);
86
      iRight.add(p2);
87
      iRight.add(p1);
88
      iRight.add(p1);
89
      iRight.add(p1);
90
      }
91
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
   
94
    public void onDrawFrame(GL10 glUnused) 
95
      {
96
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
97
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
98
      
99
      mBean.draw(System.currentTimeMillis());
100
      }
101
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
    
104
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
105
      { 
106 a8c3ada7 Leszek Koltunski
      mBean.abortEffects(EffectTypes.MATRIX);
107 427ab7bf Leszek Koltunski
         
108
      if( bmpHeight/bmpWidth > height/width )
109
        {
110
        int w = (height*bmpWidth)/bmpHeight;
111 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
112
113
        mBean.move( new Static3D((width-w)/2,0,0) );
114
        mBean.scale( new Static3D(factor,factor,factor) );
115 427ab7bf Leszek Koltunski
        }
116
      else
117
        {
118
        int h = (width*bmpHeight)/bmpWidth;
119 7589635e Leszek Koltunski
        float factor = (float)width/bmpWidth;
120
121
        mBean.move( new Static3D(0,(height-h)/2,0) );
122
        mBean.scale( new Static3D(factor,factor,factor) );
123 427ab7bf Leszek Koltunski
        }
124
      
125
      Distorted.onSurfaceChanged(width, height); 
126
      }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
    
130
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
131
      {
132
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
133
      Bitmap bitmap;
134
        
135
      try 
136
        {
137
        bitmap = BitmapFactory.decodeStream(is);
138
        } 
139
      finally 
140
        {
141
        try 
142
          {
143
          is.close();
144
          } 
145
        catch(IOException e) { }
146
        }  
147
      
148
      bmpHeight = bitmap.getHeight();
149
      bmpWidth  = bitmap.getWidth();
150
      
151
      mBean = new DistortedBitmap(bitmap, 10);     
152
      mBean.distort(iLeft , rLeft , pLeft );
153
      mBean.distort(iRight, rRight, pRight);
154
      
155
      try
156
        {
157 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
158 427ab7bf Leszek Koltunski
        }
159
      catch(Exception ex)
160
        {
161
        android.util.Log.e("Bean", ex.getMessage() );
162
        }
163
      }
164
}