Project

General

Profile

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

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

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
      mScreen = new DistortedScreen();
97
      }
98

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

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
109
     {
110
     float min= width>height ? height : width;
111

    
112
     mScale.set( min, min*mBmpRatio, 1 );
113
     mScreen.resize(width, height);
114
     }
115

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

    
138
     if( mTexture==null ) mTexture = new DistortedTexture();
139
     mTexture.setTexture(bitmap);
140

    
141
     // we need a denser Mesh lattice this time for the Distorts to look good.
142
     if( mMesh==null ) mMesh = new MeshRectangles(25, (int)(25*mBmpRatio));
143

    
144
     mScreen.detachAll();
145
     mScreen.attach(mTexture,mEffects,mMesh);
146

    
147
     VertexEffectDistort.enable();
148

    
149
     try
150
       {
151
       DistortedLibrary.onCreate(mView.getContext());
152
       }
153
     catch(Exception ex)
154
       {
155
       android.util.Log.e("Bean", ex.getMessage() );
156
       }
157
     }
158
}
(2-2/3)