Project

General

Profile

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

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

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.MeshFlat;
34
import org.distorted.library.EffectTypes;
35
import org.distorted.library.type.Dynamic3D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.type.Static4D;
38

    
39
import android.graphics.Bitmap;
40
import android.graphics.BitmapFactory;
41
import android.opengl.GLES30;
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 DistortedScreen mScreen;
51
    private int bmpHeight, bmpWidth;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

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

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

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

    
75
      mScreen = new DistortedScreen();
76
      }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79
   
80
    public void onDrawFrame(GL10 glUnused) 
81
      {
82
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
83
      mScreen.render( System.currentTimeMillis() );
84
      }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
    
88
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
89
      { 
90
      mEffects.abortEffects(EffectTypes.MATRIX);
91

    
92
      if( (float)bmpHeight/bmpWidth > (float)height/width )
93
        {
94
        int w = (height*bmpWidth)/bmpHeight;
95
        float factor = (float)height/bmpHeight;
96
        mEffects.move( new Static3D((width-w)/2,0,0) );
97
        mEffects.scale(factor);
98
        }
99
      else
100
        {
101
        int h = (width*bmpHeight)/bmpWidth;
102
        float factor = (float)width/bmpWidth;
103
        mEffects.move( new Static3D(0,(height-h)/2,0) );
104
        mEffects.scale(factor);
105
        }
106

    
107
      mScreen.resize(width, height);
108
      }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
113
      {
114
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
115

    
116
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
117
      Bitmap bitmap;
118
        
119
      try 
120
        {
121
        bitmap = BitmapFactory.decodeStream(is);
122
        } 
123
      finally 
124
        {
125
        try 
126
          {
127
          is.close();
128
          } 
129
        catch(IOException e) { }
130
        }  
131

    
132
      bmpHeight = bitmap.getHeight();
133
      bmpWidth  = bitmap.getWidth();
134

    
135
      MeshFlat mesh = new MeshFlat(9,9*bmpHeight/bmpWidth);
136
      DistortedTexture texture = new DistortedTexture(bmpWidth,bmpHeight);
137
      texture.setTexture(bitmap);
138

    
139
      mScreen.detachAll();
140
      mScreen.attach(texture,mEffects,mesh);
141

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