Project

General

Profile

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

examples / src / main / java / org / distorted / examples / bean / BeanRenderer.java @ 7c55263f

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.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDistort;
32
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.main.DistortedLibrary;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.mesh.MeshRectangles;
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 MeshRectangles mMesh;
54
   private Static3D mScale;
55
   private float mBmpRatio;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
// bean bitmap is 366 x 480. Initialize two dynamic Distorts of the left and right brow.
59

    
60
   BeanRenderer(GLSurfaceView v)
61
      {
62
      mView = v;
63
     
64
      Static3D pointLeft  = new Static3D(-85/366.0f,  57/480.0f, 0);
65
      Static3D pointRight = new Static3D( 50/366.0f, 100/480.0f, 0);
66
      Static4D regionLeft = new Static4D( -3/366.0f,  33/480.0f, 0, 47/400.0f);
67
      Static4D regionRight= new Static4D(-14/366.0f,  33/480.0f, 0, 47/400.0f);
68

    
69
      Dynamic3D dynLeft   = new Dynamic3D(2000,0.0f);
70
      Dynamic3D dynRight  = new Dynamic3D(2000,0.0f);
71

    
72
      Static3D vect1 = new Static3D(         0,         0, 0);
73
      Static3D vect2 = new Static3D(-15/366.0f, 30/480.0f, 0);
74

    
75
      dynLeft.add(vect1);
76
      dynLeft.add(vect1);
77
      dynLeft.add(vect1);
78
      dynLeft.add(vect1);
79
      dynLeft.add(vect2);
80
      dynLeft.add(vect2);
81
      
82
      dynRight.add(vect1);
83
      dynRight.add(vect2);
84
      dynRight.add(vect2);
85
      dynRight.add(vect1);
86
      dynRight.add(vect1);
87
      dynRight.add(vect1);
88

    
89
      mScale= new Static3D(1,1,1);
90

    
91
      mEffects = new DistortedEffects();
92
      mEffects.apply( new VertexEffectDistort(dynLeft , pointLeft , regionLeft) );
93
      mEffects.apply( new VertexEffectDistort(dynRight, pointRight, regionRight));
94
      mEffects.apply( new MatrixEffectScale(mScale) );
95

    
96
      mTexture = new DistortedTexture();
97
      mScreen  = new DistortedScreen();
98
      }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
   
102
   public void onDrawFrame(GL10 glUnused)
103
      {
104
      mScreen.render( System.currentTimeMillis() );
105
      }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
110
     {
111
     if( width<height ) mScale.set( width,   width*mBmpRatio, 1 );
112
     else               mScale.set( height/mBmpRatio, height, 1 );
113

    
114
     mScreen.resize(width, height);
115
     }
116

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

    
140
     // we need a denser Mesh lattice this time for the Distorts to look good.
141

    
142
     float PART = 0.47f;
143

    
144
     int width = 25;
145
     int height= (int)((25*mBmpRatio)*PART);
146

    
147
     float[] xLoc = new float[width +1];
148
     float[] yLoc = new float[height+1];
149

    
150
     xLoc[0] = -0.5f;
151
     yLoc[0] = -0.5f;
152

    
153
     for(int i=1; i<=width; i++) xLoc[i] = 1.0f/width;
154
     for(int i=1; i<height; i++) yLoc[i] = PART/height;
155

    
156
     yLoc[height] = (height-(height-1)*PART)/height;
157

    
158
     if( mMesh==null ) mMesh = new MeshRectangles(xLoc,yLoc);
159

    
160
     mScreen.detachAll();
161
     mScreen.attach(mTexture,mEffects,mMesh);
162

    
163
     VertexEffectDistort.enable();
164

    
165
     try
166
       {
167
       DistortedLibrary.onCreate(mView.getContext());
168
       }
169
     catch(Exception ex)
170
       {
171
       android.util.Log.e("Bean", ex.getMessage() );
172
       }
173
     }
174
}
(2-2/3)