Project

General

Profile

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

examples / src / main / java / org / distorted / examples / monalisa / MonaLisaRenderer.java @ 10b7e588

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.DistortedObject;
31
import org.distorted.library.GridFlat;
32
import org.distorted.library.EffectTypes;
33
import org.distorted.library.type.Dynamic3D;
34
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36

    
37
import android.graphics.Bitmap;
38
import android.graphics.BitmapFactory;
39
import android.opengl.GLES20;
40
import android.opengl.GLSurfaceView;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
class MonaLisaRenderer implements GLSurfaceView.Renderer 
45
{
46
    private GLSurfaceView mView;
47
    private DistortedObject mObject;
48
    private GridFlat mGrid;
49
    private Static3D pLeft, pRight;
50
    private Static4D rLeft, rRight;
51
    private Dynamic3D dLeft, dRight;
52

    
53
    private int bmpHeight, bmpWidth;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

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

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

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
   
76
    public void onDrawFrame(GL10 glUnused) 
77
      {
78
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
79
      mObject.draw(System.currentTimeMillis(),mGrid);
80
      }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83
    
84
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
85
      { 
86
      mObject.abortEffects(EffectTypes.MATRIX);
87

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

    
103
      Distorted.onSurfaceChanged(width, height); 
104
      }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
109
      {
110
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
111

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

    
128
      bmpHeight = bitmap.getHeight();
129
      bmpWidth  = bitmap.getWidth();
130

    
131
      mGrid = new GridFlat(9,9*bmpHeight/bmpWidth);
132
      mObject = new DistortedObject(bmpWidth,bmpHeight,1);
133
      mObject.setTexture(bitmap);
134
      mObject.distort( dLeft, pLeft , rLeft );
135
      mObject.distort(dRight, pRight, rRight);
136

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