Project

General

Profile

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

examples / src / main / java / org / distorted / examples / monalisa / MonaLisaRenderer.java @ e4330c89

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.effect.EffectName;
30
import org.distorted.library.effect.MatrixEffectMove;
31
import org.distorted.library.effect.MatrixEffectScale;
32
import org.distorted.library.effect.VertexEffectDistort;
33
import org.distorted.library.main.Distorted;
34
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37
import org.distorted.library.main.MeshFlat;
38
import org.distorted.library.type.Dynamic3D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

    
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLSurfaceView;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
class MonaLisaRenderer implements GLSurfaceView.Renderer 
49
{
50
    private GLSurfaceView mView;
51
    private DistortedEffects mEffects;
52
    private DistortedTexture mTexture;
53
    private MeshFlat mMesh;
54
    private DistortedScreen mScreen;
55
    private int bmpHeight, bmpWidth;
56
    private Static3D mMove, mScale;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
    MonaLisaRenderer(GLSurfaceView v)
61
      {
62
      mView = v;
63

    
64
      Static3D pLeft = new Static3D( 90, 258, 0);
65
      Static3D pRight= new Static3D(176, 255, 0);
66

    
67
      Static4D rLeft = new Static4D(-10,-10,25,25);
68
      Static4D rRight= new Static4D( 10, -5,25,25);
69
      Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
70
      Dynamic3D dRight= new Dynamic3D(1000,0.0f);
71
      dLeft.add ( new Static3D(  0,  0,0) );
72
      dLeft.add ( new Static3D(-20,-20,0) );
73
      dRight.add( new Static3D(  0,  0,0) );
74
      dRight.add( new Static3D( 20,-10,0) );
75

    
76
      mEffects = new DistortedEffects();
77
      mEffects.apply( new VertexEffectDistort(dLeft , pLeft , rLeft ) );
78
      mEffects.apply( new VertexEffectDistort(dRight, pRight, rRight) );
79

    
80
      mMove = new Static3D(0,0,0);
81
      mScale= new Static3D(1,1,1);
82
      mEffects.apply(new MatrixEffectMove(mMove));
83
      mEffects.apply(new MatrixEffectScale(mScale));
84

    
85
      mScreen = new DistortedScreen();
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
   
90
    public void onDrawFrame(GL10 glUnused) 
91
      {
92
      mScreen.render( System.currentTimeMillis() );
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
    
97
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
98
      { 
99
      if( (float)bmpHeight/bmpWidth > (float)height/width )
100
        {
101
        int w = (height*bmpWidth)/bmpHeight;
102
        float factor = (float)height/bmpHeight;
103

    
104
        mMove.set((width-w)/2,0,0);
105
        mScale.set( factor,factor,factor );
106
        }
107
      else
108
        {
109
        int h = (width*bmpHeight)/bmpWidth;
110
        float factor = (float)width/bmpWidth;
111

    
112
        mMove.set(0,(height-h)/2,0);
113
        mScale.set( factor,factor,factor );
114
        }
115

    
116
      mScreen.resize(width, height);
117
      }
118

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

    
139
      bmpHeight = bitmap.getHeight();
140
      bmpWidth  = bitmap.getWidth();
141

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

    
147
      // likewise the Mesh
148
      if( mMesh==null ) mMesh = new MeshFlat(9,9*bmpHeight/bmpWidth);
149

    
150
      // even if mTexture wasn't null, we still need to call setTexture() on it
151
      // because every time activity goes to background, its OpenGL resources
152
      // - including Textures - get deleted. We always need to call setTexture()
153
      // to recreate the internal OpenGL textures.
154
      mTexture.setTexture(bitmap);
155

    
156
      mScreen.detachAll();
157
      mScreen.attach(mTexture,mEffects,mMesh);
158

    
159
      DistortedEffects.enableEffect(EffectName.DISTORT);
160

    
161
      try
162
        {
163
        Distorted.onCreate(mView.getContext());
164
        }
165
      catch(Exception ex)
166
        {
167
        android.util.Log.e("MonaLisa", ex.getMessage() );
168
        }
169
      }
170
}
(2-2/3)