Project

General

Profile

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

examples / src / main / java / org / distorted / examples / monalisa / MonaLisaRenderer.java @ 107e4b72

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.monalisa;
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 c6526577 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectScale;
31
import org.distorted.library.effect.VertexEffectDistort;
32 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshFlat;
37 7bf107f7 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 MonaLisaRenderer implements GLSurfaceView.Renderer 
48
{
49
    private GLSurfaceView mView;
50 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
51 b0ebdf5e Leszek Koltunski
    private DistortedTexture mTexture;
52 5f2a53b6 leszek
    private MeshFlat mMesh;
53 d218d64e leszek
    private DistortedScreen mScreen;
54 8ff32d4d Leszek Koltunski
    private int bmpHeight, bmpWidth;
55 c6526577 Leszek Koltunski
    private Static3D mMove, mScale;
56 7bf107f7 Leszek Koltunski
57 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59 f6d884d5 Leszek Koltunski
    MonaLisaRenderer(GLSurfaceView v)
60 427ab7bf Leszek Koltunski
      {
61
      mView = v;
62 8ff32d4d Leszek Koltunski
63 f6d884d5 Leszek Koltunski
      Static3D pLeft = new Static3D( 90, 258, 0);
64
      Static3D pRight= new Static3D(176, 255, 0);
65 8ff32d4d Leszek Koltunski
66 f6d884d5 Leszek Koltunski
      Static4D rLeft = new Static4D(-10,-10,25,25);
67
      Static4D rRight= new Static4D( 10, -5,25,25);
68
      Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
69
      Dynamic3D dRight= new Dynamic3D(1000,0.0f);
70 8ff32d4d Leszek Koltunski
      dLeft.add ( new Static3D(  0,  0,0) );
71
      dLeft.add ( new Static3D(-20,-20,0) );
72
      dRight.add( new Static3D(  0,  0,0) );
73
      dRight.add( new Static3D( 20,-10,0) );
74 f6d884d5 Leszek Koltunski
75 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
76 c6526577 Leszek Koltunski
      mEffects.apply( new VertexEffectDistort(dLeft , pLeft , rLeft ) );
77
      mEffects.apply( new VertexEffectDistort(dRight, pRight, rRight) );
78
79
      mMove = new Static3D(0,0,0);
80
      mScale= new Static3D(1,1,1);
81
      mEffects.apply(new MatrixEffectMove(mMove));
82
      mEffects.apply(new MatrixEffectScale(mScale));
83 392e16fd Leszek Koltunski
84 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
85 427ab7bf Leszek Koltunski
      }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
   
89
    public void onDrawFrame(GL10 glUnused) 
90
      {
91 fe59d375 Leszek Koltunski
      mScreen.render( System.currentTimeMillis() );
92 427ab7bf Leszek Koltunski
      }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
    
96
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
97 630703d1 leszek
      { 
98
      if( (float)bmpHeight/bmpWidth > (float)height/width )
99
        {
100
        int w = (height*bmpWidth)/bmpHeight;
101
        float factor = (float)height/bmpHeight;
102 c6526577 Leszek Koltunski
103
        mMove.set((width-w)/2,0,0);
104
        mScale.set( factor,factor,factor );
105 630703d1 leszek
        }
106
      else
107
        {
108
        int h = (width*bmpHeight)/bmpWidth;
109
        float factor = (float)width/bmpWidth;
110 c6526577 Leszek Koltunski
111
        mMove.set(0,(height-h)/2,0);
112
        mScale.set( factor,factor,factor );
113 630703d1 leszek
        }
114 7589635e Leszek Koltunski
115 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
116 427ab7bf Leszek Koltunski
      }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
    
120
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
121
      {
122
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
123
      Bitmap bitmap;
124
        
125
      try 
126
        {
127
        bitmap = BitmapFactory.decodeStream(is);
128
        } 
129
      finally 
130
        {
131
        try 
132
          {
133
          is.close();
134
          } 
135
        catch(IOException e) { }
136
        }  
137 3c28857f Leszek Koltunski
138 8ff32d4d Leszek Koltunski
      bmpHeight = bitmap.getHeight();
139
      bmpWidth  = bitmap.getWidth();
140
141 b0ebdf5e Leszek Koltunski
      // We could have gotten here after the activity went to the background
142
      // for a brief amount of time; in this case mTexture is already created.
143
      // Do not leak memory by creating it the second time around.
144
      if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
145
146 5f2a53b6 leszek
      // likewise the Mesh
147 5c604561 Leszek Koltunski
      if( mMesh==null ) mMesh = new MeshFlat(9,9*bmpHeight/bmpWidth);
148 5f2a53b6 leszek
149 b0ebdf5e Leszek Koltunski
      // even if mTexture wasn't null, we still need to call setTexture() on it
150
      // because every time activity goes to background, its OpenGL resources
151
      // - including Textures - get deleted. We always need to call setTexture()
152
      // to recreate the internal OpenGL textures.
153
      mTexture.setTexture(bitmap);
154 fe59d375 Leszek Koltunski
155
      mScreen.detachAll();
156 5f2a53b6 leszek
      mScreen.attach(mTexture,mEffects,mMesh);
157 7bf107f7 Leszek Koltunski
158 885b9cca leszek
      VertexEffectDistort.enable();
159 6637d0f2 Leszek Koltunski
160 427ab7bf Leszek Koltunski
      try
161
        {
162 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
163 427ab7bf Leszek Koltunski
        }
164
      catch(Exception ex)
165
        {
166
        android.util.Log.e("MonaLisa", ex.getMessage() );
167
        }
168
      }
169
}