Project

General

Profile

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

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

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.Distorted;
32
import org.distorted.library.DistortedBitmap;
33
import org.distorted.library.type.Dynamic3D;
34
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
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 Static3D pLeft, pRight;
49
   private Dynamic3D dLeft, dRight;
50
   private Static4D rLeft, rRight;
51
   private int bmpHeight, bmpWidth;
52
    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
   BeanRenderer(GLSurfaceView v)
56
      {
57
      mView = v;
58
     
59
      pLeft = new Static3D( 98, 183, 0);
60
      pRight= new Static3D(233, 140, 0);
61
      
62
      rLeft = new Static4D( -3,-33,47,47);
63
      rRight= new Static4D(-14,-33,47,47);
64
     
65
      dLeft = new Dynamic3D(2000,0.0f);
66
      dRight= new Dynamic3D(2000,0.0f);
67

    
68
      Static3D p1 = new Static3D(  0,  0, 0);
69
      Static3D p2 = new Static3D(-15,-30, 0);
70
      
71
      dLeft.add(p1);
72
      dLeft.add(p1);
73
      dLeft.add(p1);
74
      dLeft.add(p1);
75
      dLeft.add(p2);
76
      dLeft.add(p2);
77
      
78
      dRight.add(p1);
79
      dRight.add(p2);
80
      dRight.add(p2);
81
      dRight.add(p1);
82
      dRight.add(p1);
83
      dRight.add(p1);
84
      }
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
      mBean.abortEffects(EffectTypes.MATRIX);
99
         
100
      if( (float)bmpHeight/bmpWidth > (float)height/width )
101
        {
102
        int w = (height*bmpWidth)/bmpHeight;
103
        float factor = (float)height/bmpHeight;
104

    
105
        mBean.move( new Static3D((width-w)/2,0,0) );
106
        mBean.scale(factor);
107
        }
108
      else
109
        {
110
        int h = (width*bmpHeight)/bmpWidth;
111
        float factor = (float)width/bmpWidth;
112

    
113
        mBean.move( new Static3D(0,(height-h)/2,0) );
114
        mBean.scale(factor);
115
        }
116
      
117
      Distorted.onSurfaceChanged(width, height); 
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
123
      {
124
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
125

    
126
      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
      mBean = new DistortedBitmap(bitmap, 25);
146
      mBean.distort(dLeft , pLeft , rLeft );
147
      mBean.distort(dRight, pRight, rRight);
148
      
149
      try
150
        {
151
        Distorted.onSurfaceCreated(mView.getContext());
152
        }
153
      catch(Exception ex)
154
        {
155
        android.util.Log.e("Bean", ex.getMessage() );
156
        }
157
      }
158
}
(2-2/3)