Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ 630703d1

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 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
31 d218d64e leszek
import org.distorted.library.DistortedScreen;
32 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
33 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
34 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
35 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
36 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
37 b5cc7760 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
38 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40 427ab7bf Leszek Koltunski
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLSurfaceView;
44
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47
class BeanRenderer implements GLSurfaceView.Renderer 
48
{
49
   private GLSurfaceView mView;
50 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
51 d218d64e leszek
   private DistortedScreen mScreen;
52 b0ebdf5e Leszek Koltunski
   private DistortedTexture mTexture;
53 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
54
    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57 b56faf50 Leszek Koltunski
   BeanRenderer(GLSurfaceView v)
58 427ab7bf Leszek Koltunski
      {
59
      mView = v;
60
     
61 f6d884d5 Leszek Koltunski
      Static3D pLeft = new Static3D( 98, 183, 0);
62
      Static3D pRight= new Static3D(233, 140, 0);
63 427ab7bf Leszek Koltunski
      
64 f6d884d5 Leszek Koltunski
      Static4D rLeft = new Static4D( -3,-33,47,47);
65
      Static4D rRight= new Static4D(-14,-33,47,47);
66 427ab7bf Leszek Koltunski
     
67 f6d884d5 Leszek Koltunski
      Dynamic3D dLeft = new Dynamic3D(2000,0.0f);
68
      Dynamic3D dRight= new Dynamic3D(2000,0.0f);
69 f988589e Leszek Koltunski
70 b5cc7760 Leszek Koltunski
      Static3D p1 = new Static3D(  0,  0, 0);
71 cdc515b9 Leszek Koltunski
      Static3D p2 = new Static3D(-15,-30, 0);
72 427ab7bf Leszek Koltunski
      
73 b5cc7760 Leszek Koltunski
      dLeft.add(p1);
74
      dLeft.add(p1);
75
      dLeft.add(p1);
76
      dLeft.add(p1);
77
      dLeft.add(p2);
78
      dLeft.add(p2);
79 427ab7bf Leszek Koltunski
      
80 b5cc7760 Leszek Koltunski
      dRight.add(p1);
81
      dRight.add(p2);
82
      dRight.add(p2);
83
      dRight.add(p1);
84
      dRight.add(p1);
85
      dRight.add(p1);
86 f6d884d5 Leszek Koltunski
87 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
88 392e16fd Leszek Koltunski
      mEffects.distort(dLeft , pLeft , rLeft );
89
      mEffects.distort(dRight, pRight, rRight);
90
91 d218d64e leszek
      mScreen = new DistortedScreen();
92 427ab7bf Leszek Koltunski
      }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
   
96
    public void onDrawFrame(GL10 glUnused) 
97
      {
98 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
99 427ab7bf Leszek Koltunski
      }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
    
103
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
104
      { 
105 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
106 630703d1 leszek
         
107
      if( (float)bmpHeight/bmpWidth > (float)height/width )
108
        {
109
        int w = (height*bmpWidth)/bmpHeight;
110
        float factor = (float)height/bmpHeight;
111
112
        mEffects.move( new Static3D((width-w)/2,0,0) );
113
        mEffects.scale(factor);
114
        }
115
      else
116
        {
117
        int h = (width*bmpHeight)/bmpWidth;
118
        float factor = (float)width/bmpWidth;
119
120
        mEffects.move( new Static3D(0,(height-h)/2,0) );
121
        mEffects.scale(factor);
122
        }
123 427ab7bf Leszek Koltunski
      
124 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
125 427ab7bf Leszek Koltunski
      }
126
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
    
129
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
130
      {
131
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
132
      Bitmap bitmap;
133
        
134
      try 
135
        {
136
        bitmap = BitmapFactory.decodeStream(is);
137
        } 
138
      finally 
139
        {
140
        try 
141
          {
142
          is.close();
143
          } 
144
        catch(IOException e) { }
145
        }  
146
      
147
      bmpHeight = bitmap.getHeight();
148
      bmpWidth  = bitmap.getWidth();
149
      
150 b0ebdf5e Leszek Koltunski
      if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
151
      mTexture.setTexture(bitmap);
152 fe59d375 Leszek Koltunski
153
      mScreen.detachAll();
154 b0ebdf5e Leszek Koltunski
      mScreen.attach(mTexture,mEffects,new MeshFlat(25,25*bmpHeight/bmpWidth));
155 e8b6aa95 Leszek Koltunski
156 6637d0f2 Leszek Koltunski
      DistortedEffects.enableEffect(EffectNames.DISTORT);
157
158 427ab7bf Leszek Koltunski
      try
159
        {
160 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
161 427ab7bf Leszek Koltunski
        }
162
      catch(Exception ex)
163
        {
164
        android.util.Log.e("Bean", ex.getMessage() );
165
        }
166
      }
167
}