Project

General

Profile

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

examples / src / main / java / org / distorted / examples / monalisa / MonaLisaRenderer.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.monalisa;
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
import org.distorted.library.Distorted;
30
import org.distorted.library.DistortedEffects;
31
import org.distorted.library.DistortedScreen;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.EffectNames;
34
import org.distorted.library.MeshFlat;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
40
import android.graphics.Bitmap;
41
import android.graphics.BitmapFactory;
42
import android.opengl.GLSurfaceView;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
class MonaLisaRenderer implements GLSurfaceView.Renderer 
47
{
48
    private GLSurfaceView mView;
49
    private DistortedEffects mEffects;
50
    private DistortedTexture mTexture;
51
    private DistortedScreen mScreen;
52
    private int bmpHeight, bmpWidth;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
    MonaLisaRenderer(GLSurfaceView v)
57
      {
58
      mView = v;
59

    
60
      Static3D pLeft = new Static3D( 90, 258, 0);
61
      Static3D pRight= new Static3D(176, 255, 0);
62

    
63
      Static4D rLeft = new Static4D(-10,-10,25,25);
64
      Static4D rRight= new Static4D( 10, -5,25,25);
65
      Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
66
      Dynamic3D dRight= new Dynamic3D(1000,0.0f);
67
      dLeft.add ( new Static3D(  0,  0,0) );
68
      dLeft.add ( new Static3D(-20,-20,0) );
69
      dRight.add( new Static3D(  0,  0,0) );
70
      dRight.add( new Static3D( 20,-10,0) );
71

    
72
      mEffects = new DistortedEffects();
73
      mEffects.distort( dLeft, pLeft , rLeft );
74
      mEffects.distort(dRight, pRight, rRight);
75

    
76
      mScreen = new DistortedScreen();
77
      }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
   
81
    public void onDrawFrame(GL10 glUnused) 
82
      {
83
      mScreen.render( System.currentTimeMillis() );
84
      }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
    
88
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
89
      {
90
      float qx = (float)width /bmpWidth;
91
      float qy = (float)height/bmpHeight;
92

    
93
      mEffects.abortEffects(EffectTypes.MATRIX);
94
      mEffects.scale(  qx<qy ? (new Static3D(1,qx/qy,1)) : (new Static3D(qy/qx,1,1)) );
95

    
96
      mScreen.resize(width, height);
97
      }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
    
101
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
102
      {
103
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
104
      Bitmap bitmap;
105
        
106
      try 
107
        {
108
        bitmap = BitmapFactory.decodeStream(is);
109
        } 
110
      finally 
111
        {
112
        try 
113
          {
114
          is.close();
115
          } 
116
        catch(IOException e) { }
117
        }  
118

    
119
      bmpHeight = bitmap.getHeight();
120
      bmpWidth  = bitmap.getWidth();
121

    
122
      // We could have gotten here after the activity went to the background
123
      // for a brief amount of time; in this case mTexture is already created.
124
      // Do not leak memory by creating it the second time around.
125
      if( mTexture==null ) mTexture = new DistortedTexture(bmpWidth,bmpHeight);
126

    
127
      // even if mTexture wasn't null, we still need to call setTexture() on it
128
      // because every time activity goes to background, its OpenGL resources
129
      // - including Textures - get deleted. We always need to call setTexture()
130
      // to recreate the internal OpenGL textures.
131
      mTexture.setTexture(bitmap);
132

    
133
      mScreen.detachAll();
134
      mScreen.attach(mTexture,mEffects,new MeshFlat(9,9*bmpHeight/bmpWidth));
135

    
136
      DistortedEffects.enableEffect(EffectNames.DISTORT);
137

    
138
      try
139
        {
140
        Distorted.onCreate(mView.getContext());
141
        }
142
      catch(Exception ex)
143
        {
144
        android.util.Log.e("MonaLisa", ex.getMessage() );
145
        }
146
      }
147
}
(2-2/3)