Project

General

Profile

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

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

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
import org.distorted.library.Distorted;
30 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
31 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
32 d218d64e leszek
import org.distorted.library.DistortedScreen;
33 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
34 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
35 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
36 7bf107f7 Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
37 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39 427ab7bf Leszek Koltunski
40
import android.graphics.Bitmap;
41
import android.graphics.BitmapFactory;
42 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
43 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
44
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
47
class MonaLisaRenderer implements GLSurfaceView.Renderer 
48
{
49
    private GLSurfaceView mView;
50 f6d884d5 Leszek Koltunski
    private DistortedTexture mTexture;
51 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
52 d218d64e leszek
    private DistortedScreen mScreen;
53 b01acdaf Leszek Koltunski
    private MeshFlat mMesh;
54 8ff32d4d Leszek Koltunski
    private int bmpHeight, bmpWidth;
55 7bf107f7 Leszek Koltunski
56 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 f6d884d5 Leszek Koltunski
    MonaLisaRenderer(GLSurfaceView v)
59 427ab7bf Leszek Koltunski
      {
60
      mView = v;
61 8ff32d4d Leszek Koltunski
62 f6d884d5 Leszek Koltunski
      Static3D pLeft = new Static3D( 90, 258, 0);
63
      Static3D pRight= new Static3D(176, 255, 0);
64 8ff32d4d Leszek Koltunski
65 f6d884d5 Leszek Koltunski
      Static4D rLeft = new Static4D(-10,-10,25,25);
66
      Static4D rRight= new Static4D( 10, -5,25,25);
67
      Dynamic3D dLeft = new Dynamic3D(1000,0.0f);
68
      Dynamic3D dRight= new Dynamic3D(1000,0.0f);
69 8ff32d4d Leszek Koltunski
      dLeft.add ( new Static3D(  0,  0,0) );
70
      dLeft.add ( new Static3D(-20,-20,0) );
71
      dRight.add( new Static3D(  0,  0,0) );
72
      dRight.add( new Static3D( 20,-10,0) );
73 f6d884d5 Leszek Koltunski
74 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
75 392e16fd Leszek Koltunski
      mEffects.distort( dLeft, pLeft , rLeft );
76
      mEffects.distort(dRight, pRight, rRight);
77
78 d218d64e leszek
      mScreen = new DistortedScreen();
79 427ab7bf Leszek Koltunski
      }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
   
83
    public void onDrawFrame(GL10 glUnused) 
84
      {
85 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
86 b01acdaf Leszek Koltunski
      mScreen.renderTo(mTexture, mMesh, mEffects, System.currentTimeMillis() );
87 427ab7bf Leszek Koltunski
      }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
    
91
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
92
      { 
93 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
94 8ff32d4d Leszek Koltunski
95 5055b5d4 Leszek Koltunski
      if( (float)bmpHeight/bmpWidth > (float)height/width )
96 8ff32d4d Leszek Koltunski
        {
97
        int w = (height*bmpWidth)/bmpHeight;
98
        float factor = (float)height/bmpHeight;
99 392e16fd Leszek Koltunski
        mEffects.move( new Static3D((width-w)/2,0,0) );
100
        mEffects.scale(factor);
101 8ff32d4d Leszek Koltunski
        }
102
      else
103
        {
104
        int h = (width*bmpHeight)/bmpWidth;
105
        float factor = (float)width/bmpWidth;
106 392e16fd Leszek Koltunski
        mEffects.move( new Static3D(0,(height-h)/2,0) );
107
        mEffects.scale(factor);
108 8ff32d4d Leszek Koltunski
        }
109 7589635e Leszek Koltunski
110 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
111 427ab7bf Leszek Koltunski
      }
112
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
116
      {
117 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
118 e7a4ef16 Leszek Koltunski
119 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.monalisa);
120
      Bitmap bitmap;
121
        
122
      try 
123
        {
124
        bitmap = BitmapFactory.decodeStream(is);
125
        } 
126
      finally 
127
        {
128
        try 
129
          {
130
          is.close();
131
          } 
132
        catch(IOException e) { }
133
        }  
134 3c28857f Leszek Koltunski
135 8ff32d4d Leszek Koltunski
      bmpHeight = bitmap.getHeight();
136
      bmpWidth  = bitmap.getWidth();
137
138 b01acdaf Leszek Koltunski
      mMesh = new MeshFlat(9,9*bmpHeight/bmpWidth);
139 7451c98a Leszek Koltunski
      mTexture = new DistortedTexture(bmpWidth,bmpHeight);
140 f6d884d5 Leszek Koltunski
      mTexture.setTexture(bitmap);
141 7bf107f7 Leszek Koltunski
142 427ab7bf Leszek Koltunski
      try
143
        {
144 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
145 427ab7bf Leszek Koltunski
        }
146
      catch(Exception ex)
147
        {
148
        android.util.Log.e("MonaLisa", ex.getMessage() );
149
        }
150
      }
151
}