Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ 5055b5d4

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.Static3D;
35
import org.distorted.library.type.Static4D;
36 427ab7bf Leszek Koltunski
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 334c13fa Leszek Koltunski
   private Static3D pLeft, pRight;
49 b5cc7760 Leszek Koltunski
   private Dynamic3D dLeft, dRight;
50 7589635e Leszek Koltunski
   private Static4D rLeft, rRight;
51 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
52
    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55 b56faf50 Leszek Koltunski
   BeanRenderer(GLSurfaceView v)
56 427ab7bf Leszek Koltunski
      {
57
      mView = v;
58
     
59 cdc515b9 Leszek Koltunski
      pLeft = new Static3D( 98, 183, 0);
60
      pRight= new Static3D(233, 140, 0);
61 427ab7bf Leszek Koltunski
      
62 cdc515b9 Leszek Koltunski
      rLeft = new Static4D( -3,-33,47,47);
63
      rRight= new Static4D(-14,-33,47,47);
64 427ab7bf Leszek Koltunski
     
65 b041d424 Leszek Koltunski
      dLeft = new Dynamic3D(2000,0.0f);
66
      dRight= new Dynamic3D(2000,0.0f);
67 f988589e Leszek Koltunski
68 b5cc7760 Leszek Koltunski
      Static3D p1 = new Static3D(  0,  0, 0);
69 cdc515b9 Leszek Koltunski
      Static3D p2 = new Static3D(-15,-30, 0);
70 427ab7bf Leszek Koltunski
      
71 b5cc7760 Leszek Koltunski
      dLeft.add(p1);
72
      dLeft.add(p1);
73
      dLeft.add(p1);
74
      dLeft.add(p1);
75
      dLeft.add(p2);
76
      dLeft.add(p2);
77 427ab7bf Leszek Koltunski
      
78 b5cc7760 Leszek Koltunski
      dRight.add(p1);
79
      dRight.add(p2);
80
      dRight.add(p2);
81
      dRight.add(p1);
82
      dRight.add(p1);
83
      dRight.add(p1);
84 427ab7bf Leszek Koltunski
      }
85
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
   
88
    public void onDrawFrame(GL10 glUnused) 
89
      {
90
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
91
      mBean.draw(System.currentTimeMillis());
92
      }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
    
96
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
97
      { 
98 a8c3ada7 Leszek Koltunski
      mBean.abortEffects(EffectTypes.MATRIX);
99 427ab7bf Leszek Koltunski
         
100 5055b5d4 Leszek Koltunski
      if( (float)bmpHeight/bmpWidth > (float)height/width )
101 427ab7bf Leszek Koltunski
        {
102
        int w = (height*bmpWidth)/bmpHeight;
103 7589635e Leszek Koltunski
        float factor = (float)height/bmpHeight;
104
105
        mBean.move( new Static3D((width-w)/2,0,0) );
106 f988589e Leszek Koltunski
        mBean.scale(factor);
107 427ab7bf Leszek Koltunski
        }
108
      else
109
        {
110
        int h = (width*bmpHeight)/bmpWidth;
111 7589635e Leszek Koltunski
        float factor = (float)width/bmpWidth;
112
113
        mBean.move( new Static3D(0,(height-h)/2,0) );
114 f988589e Leszek Koltunski
        mBean.scale(factor);
115 427ab7bf Leszek Koltunski
        }
116
      
117
      Distorted.onSurfaceChanged(width, height); 
118
      }
119
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
123
      {
124 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
125
126 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
127
      Bitmap bitmap;
128
        
129
      try 
130
        {
131
        bitmap = BitmapFactory.decodeStream(is);
132
        } 
133
      finally 
134
        {
135
        try 
136
          {
137
          is.close();
138
          } 
139
        catch(IOException e) { }
140
        }  
141
      
142
      bmpHeight = bitmap.getHeight();
143
      bmpWidth  = bitmap.getWidth();
144
      
145 cdc515b9 Leszek Koltunski
      mBean = new DistortedBitmap(bitmap, 25);
146 b5cc7760 Leszek Koltunski
      mBean.distort(dLeft , pLeft , rLeft );
147
      mBean.distort(dRight, pRight, rRight);
148 427ab7bf Leszek Koltunski
      
149
      try
150
        {
151 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
152 427ab7bf Leszek Koltunski
        }
153
      catch(Exception ex)
154
        {
155
        android.util.Log.e("Bean", ex.getMessage() );
156
        }
157
      }
158
}