Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fa9b6494 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDistort;
32 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
33
import org.distorted.library.main.DistortedScreen;
34 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
35 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedTexture;
36 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
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 f3ca895f Leszek Koltunski
   private MeshRectangles mMesh;
54 849e0034 Leszek Koltunski
   private Static3D mScale;
55 a7a8f7bd Leszek Koltunski
   private float mBmpRatio;
56 fa9b6494 Leszek Koltunski
57 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
58 a7a8f7bd Leszek Koltunski
// bean bitmap is 366 x 480. Initialize two dynamic Distorts of the left and right brow.
59 427ab7bf Leszek Koltunski
60 b56faf50 Leszek Koltunski
   BeanRenderer(GLSurfaceView v)
61 427ab7bf Leszek Koltunski
      {
62
      mView = v;
63
     
64 a7a8f7bd Leszek Koltunski
      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 dfae5d27 Leszek Koltunski
      Dynamic3D dynLeft   = new Dynamic3D(2000,0.0f);
70
      Dynamic3D dynRight  = new Dynamic3D(2000,0.0f);
71 f988589e Leszek Koltunski
72 a7a8f7bd Leszek Koltunski
      Static3D vect1 = new Static3D(         0,         0, 0);
73
      Static3D vect2 = new Static3D(-15/366.0f, 30/480.0f, 0);
74 dfae5d27 Leszek Koltunski
75 a4d59c0b Leszek Koltunski
      dynLeft.add(vect1);
76
      dynLeft.add(vect1);
77
      dynLeft.add(vect1);
78
      dynLeft.add(vect1);
79
      dynLeft.add(vect2);
80
      dynLeft.add(vect2);
81 427ab7bf Leszek Koltunski
      
82 a4d59c0b Leszek Koltunski
      dynRight.add(vect1);
83
      dynRight.add(vect2);
84
      dynRight.add(vect2);
85
      dynRight.add(vect1);
86
      dynRight.add(vect1);
87
      dynRight.add(vect1);
88 f6d884d5 Leszek Koltunski
89 fa9b6494 Leszek Koltunski
      mScale= new Static3D(1,1,1);
90
91 698ad0a8 Leszek Koltunski
      mEffects = new DistortedEffects();
92 849e0034 Leszek Koltunski
      mEffects.apply( new VertexEffectDistort(dynLeft , pointLeft , regionLeft) );
93
      mEffects.apply( new VertexEffectDistort(dynRight, pointRight, regionRight));
94 dfae5d27 Leszek Koltunski
      mEffects.apply( new MatrixEffectScale(mScale) );
95 392e16fd Leszek Koltunski
96 4c42bc15 Leszek Koltunski
      mTexture = new DistortedTexture();
97
      mScreen  = new DistortedScreen();
98 427ab7bf Leszek Koltunski
      }
99
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
   
102 fa9b6494 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
103 427ab7bf Leszek Koltunski
      {
104 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
105 427ab7bf Leszek Koltunski
      }
106
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109 fa9b6494 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
110
     {
111 c7a31368 Leszek Koltunski
     if( width<height ) mScale.set( width,   width*mBmpRatio, 1 );
112
     else               mScale.set( height/mBmpRatio, height, 1 );
113 d381aa80 Leszek Koltunski
114
     mScreen.resize(width, height);
115 fa9b6494 Leszek Koltunski
     }
116 427ab7bf Leszek Koltunski
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119 fa9b6494 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
120
     {
121
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.bean);
122
     Bitmap bitmap;
123 427ab7bf Leszek Koltunski
        
124 fa9b6494 Leszek Koltunski
     try
125
       {
126
       bitmap = BitmapFactory.decodeStream(is);
127
       }
128
     finally
129
       {
130
       try
131
         {
132
         is.close();
133
         }
134
       catch(IOException e) { }
135
       }
136 427ab7bf Leszek Koltunski
      
137 a7a8f7bd Leszek Koltunski
     mBmpRatio = (float)bitmap.getHeight()/bitmap.getWidth();
138 fa9b6494 Leszek Koltunski
     mTexture.setTexture(bitmap);
139 26c4e181 Leszek Koltunski
140 a7a8f7bd Leszek Koltunski
     // we need a denser Mesh lattice this time for the Distorts to look good.
141 7c55263f Leszek Koltunski
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 a4d59c0b Leszek Koltunski
160 fa9b6494 Leszek Koltunski
     mScreen.detachAll();
161
     mScreen.attach(mTexture,mEffects,mMesh);
162
163 885b9cca leszek
     VertexEffectDistort.enable();
164 fa9b6494 Leszek Koltunski
165
     try
166
       {
167 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
168 fa9b6494 Leszek Koltunski
       }
169
     catch(Exception ex)
170
       {
171
       android.util.Log.e("Bean", ex.getMessage() );
172
       }
173
     }
174 427ab7bf Leszek Koltunski
}