Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ b041d424

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 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
32
import org.distorted.library.DistortedBitmap;
33 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
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 b5cc7760 Leszek Koltunski
   private Dynamic3D dLeft, dRight;
51 7589635e Leszek Koltunski
   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 b041d424 Leszek Koltunski
      pLeft = new Static2D(106, 192);
61 7589635e Leszek Koltunski
      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 b041d424 Leszek Koltunski
      dLeft = new Dynamic3D(2000,0.0f);
67
      dRight= new Dynamic3D(2000,0.0f);
68 f988589e Leszek Koltunski
69 b5cc7760 Leszek Koltunski
      Static3D p1 = new Static3D(  0,  0, 0);
70 b041d424 Leszek Koltunski
      Static3D p2 = new Static3D(-10,-30, 0);
71 427ab7bf Leszek Koltunski
      
72 b5cc7760 Leszek Koltunski
      dLeft.add(p1);
73
      dLeft.add(p1);
74
      dLeft.add(p1);
75
      dLeft.add(p1);
76
      dLeft.add(p2);
77
      dLeft.add(p2);
78 427ab7bf Leszek Koltunski
      
79 b5cc7760 Leszek Koltunski
      dRight.add(p1);
80
      dRight.add(p2);
81
      dRight.add(p2);
82
      dRight.add(p1);
83
      dRight.add(p1);
84
      dRight.add(p1);
85 427ab7bf Leszek Koltunski
      }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
   
89
    public void onDrawFrame(GL10 glUnused) 
90
      {
91
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
92
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
93
      
94
      mBean.draw(System.currentTimeMillis());
95
      }
96
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
    
99
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
100
      { 
101 a8c3ada7 Leszek Koltunski
      mBean.abortEffects(EffectTypes.MATRIX);
102 427ab7bf Leszek Koltunski
         
103
      if( bmpHeight/bmpWidth > height/width )
104
        {
105
        int w = (height*bmpWidth)/bmpHeight;
106 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
107
108
        mBean.move( new Static3D((width-w)/2,0,0) );
109 f988589e Leszek Koltunski
        mBean.scale(factor);
110 427ab7bf Leszek Koltunski
        }
111
      else
112
        {
113
        int h = (width*bmpHeight)/bmpWidth;
114 7589635e Leszek Koltunski
        float factor = (float)width/bmpWidth;
115
116
        mBean.move( new Static3D(0,(height-h)/2,0) );
117 f988589e Leszek Koltunski
        mBean.scale(factor);
118 427ab7bf Leszek Koltunski
        }
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 b041d424 Leszek Koltunski
      mBean = new DistortedBitmap(bitmap, 30);
147 b5cc7760 Leszek Koltunski
      mBean.distort(dLeft , pLeft , rLeft );
148
      mBean.distort(dRight, pRight, rRight);
149 427ab7bf Leszek Koltunski
      
150
      try
151
        {
152 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
153 427ab7bf Leszek Koltunski
        }
154
      catch(Exception ex)
155
        {
156
        android.util.Log.e("Bean", ex.getMessage() );
157
        }
158
      }
159
}