Project

General

Profile

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

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

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.DistortedEffects;
31
import org.distorted.library.DistortedScreen;
32
import org.distorted.library.EffectNames;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.Distorted;
35
import org.distorted.library.DistortedTexture;
36
import org.distorted.library.MeshFlat;
37
import org.distorted.library.type.Dynamic3D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

    
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
   private DistortedEffects mEffects;
51
   private DistortedScreen mScreen;
52
   private DistortedTexture mTexture;
53
   private int bmpHeight, bmpWidth;
54
    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
70
      Static3D p1 = new Static3D(  0,  0, 0);
71
      Static3D p2 = new Static3D(-15,-30, 0);
72
      
73
      dLeft.add(p1);
74
      dLeft.add(p1);
75
      dLeft.add(p1);
76
      dLeft.add(p1);
77
      dLeft.add(p2);
78
      dLeft.add(p2);
79
      
80
      dRight.add(p1);
81
      dRight.add(p2);
82
      dRight.add(p2);
83
      dRight.add(p1);
84
      dRight.add(p1);
85
      dRight.add(p1);
86

    
87
      mEffects = new DistortedEffects();
88
      mEffects.distort(dLeft , pLeft , rLeft );
89
      mEffects.distort(dRight, pRight, rRight);
90

    
91
      mScreen = new DistortedScreen();
92
      }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
   
96
    public void onDrawFrame(GL10 glUnused) 
97
      {
98
      mScreen.render( System.currentTimeMillis() );
99
      }
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
    
103
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
104
      { 
105
      float qx = (float)width /bmpWidth;
106
      float qy = (float)height/bmpHeight;
107

    
108
      mEffects.abortEffects(EffectTypes.MATRIX);
109
      mEffects.scale(  qx<qy ? (new Static3D(1,qx/qy,1)) : (new Static3D(qy/qx,1,1)) );
110
      
111
      mScreen.resize(width, height);
112
      }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115
    
116
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
117
      {
118
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
119
      Bitmap bitmap;
120
        
121
      try 
122
        {
123
        bitmap = BitmapFactory.decodeStream(is);
124
        } 
125
      finally 
126
        {
127
        try 
128
          {
129
          is.close();
130
          } 
131
        catch(IOException e) { }
132
        }  
133
      
134
      bmpHeight = bitmap.getHeight();
135
      bmpWidth  = bitmap.getWidth();
136
      
137
      if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
138
      mTexture.setTexture(bitmap);
139

    
140
      mScreen.detachAll();
141
      mScreen.attach(mTexture,mEffects,new MeshFlat(25,25*bmpHeight/bmpWidth));
142

    
143
      DistortedEffects.enableEffect(EffectNames.DISTORT);
144

    
145
      try
146
        {
147
        Distorted.onCreate(mView.getContext());
148
        }
149
      catch(Exception ex)
150
        {
151
        android.util.Log.e("Bean", ex.getMessage() );
152
        }
153
      }
154
}
(2-2/3)