Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ 10b7e588

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.fbo;
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 427ab7bf Leszek Koltunski
30 10b7e588 Leszek Koltunski
import org.distorted.library.DistortedObjectTree;
31 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
32 10b7e588 Leszek Koltunski
import org.distorted.library.GridFlat;
33 e8b6aa95 Leszek Koltunski
import org.distorted.library.DistortedObject;
34 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
35 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
36
import org.distorted.library.type.Static1D;
37 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
38 427ab7bf Leszek Koltunski
39
import android.graphics.Bitmap;
40
import android.graphics.BitmapFactory;
41
import android.opengl.GLES20;
42
import android.opengl.GLSurfaceView;
43
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
46
class FBORenderer implements GLSurfaceView.Renderer 
47
{
48
   private GLSurfaceView mView;
49 e8b6aa95 Leszek Koltunski
   private DistortedObject mLisa;
50 427ab7bf Leszek Koltunski
   private int lisaHeight, lisaWidth;
51
    
52 10b7e588 Leszek Koltunski
   private DistortedObjectTree mRoot;
53 427ab7bf Leszek Koltunski
    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56 7e6110e2 Leszek Koltunski
   FBORenderer(GLSurfaceView v)
57 427ab7bf Leszek Koltunski
      {
58
      mView = v;
59
      }
60
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
   
63
    public void onDrawFrame(GL10 glUnused) 
64
      {
65
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
66
      mRoot.draw(System.currentTimeMillis());
67
      }
68
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
    
71
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
72
      { 
73 a8c3ada7 Leszek Koltunski
      mLisa.abortEffects(EffectTypes.MATRIX);
74 427ab7bf Leszek Koltunski
         
75 5055b5d4 Leszek Koltunski
      if( (float)lisaHeight/lisaWidth > (float)height/width )
76 427ab7bf Leszek Koltunski
        {
77
        int w = (height*lisaWidth)/lisaHeight;
78 7589635e Leszek Koltunski
        float factor = (float)height/lisaHeight;
79
80
        mLisa.move( new Static3D((width-w)/2,0,0) );
81 f988589e Leszek Koltunski
        mLisa.scale(factor);
82 427ab7bf Leszek Koltunski
        }
83
      else
84
        {
85
        int h = (width*lisaHeight)/lisaWidth;
86 7589635e Leszek Koltunski
        float factor = (float)width/lisaWidth;
87
88 f988589e Leszek Koltunski
        mLisa.move( new Static3D(0,(height-h)/2,0) );
89
        mLisa.scale(factor);
90 427ab7bf Leszek Koltunski
        }
91
      
92
      Distorted.onSurfaceChanged(width, height); 
93
      }
94
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
    
97
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
98
      {
99 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
100
101 427ab7bf Leszek Koltunski
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
102
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.fbo);
103
      
104
      Bitmap bitmap1, bitmap2;
105
       
106
      try 
107
        {
108
        bitmap1 = BitmapFactory.decodeStream(is1);
109
        bitmap2 = BitmapFactory.decodeStream(is2);
110
        } 
111
      finally 
112
        {
113
        try 
114
          {
115
          is1.close();
116
          is2.close();
117
          } 
118
        catch(IOException e) { }
119
        }  
120
      
121
      lisaHeight = bitmap1.getHeight();
122
      lisaWidth  = bitmap1.getWidth();
123
      
124 e8b6aa95 Leszek Koltunski
      mLisa = new DistortedObject(lisaWidth, lisaHeight, 1);
125
      mLisa.setTexture(bitmap1);
126
      DistortedObject text = new DistortedObject(bitmap2.getWidth(),bitmap2.getHeight(),1);
127
      text.setTexture(bitmap2);
128
129 10b7e588 Leszek Koltunski
      mRoot = new DistortedObjectTree(mLisa,new GridFlat(1,1));
130
      mRoot.attach(text,new GridFlat(20,5));
131 427ab7bf Leszek Koltunski
     
132 7e6110e2 Leszek Koltunski
      int textWidth  = text.getWidth();
133
      int textHeight = text.getHeight();
134 7589635e Leszek Koltunski
135
      float factor = lisaWidth/(1.5f*textWidth);
136
137 7e6110e2 Leszek Koltunski
      text.move( new Static3D(lisaWidth/6,lisaHeight/3,0) );
138
      text.scale(factor);
139 7bf107f7 Leszek Koltunski
140 f988589e Leszek Koltunski
      Dynamic1D sinkDyn = new Dynamic1D(5000,0.0f);
141 7bf107f7 Leszek Koltunski
      sinkDyn.add(new Static1D(1.0f));
142
      sinkDyn.add(new Static1D(0.5f));
143
144 334c13fa Leszek Koltunski
      text.sink(sinkDyn, new Static3D(textWidth/2,textHeight/2, 0));
145 59759251 Leszek Koltunski
146 70d7a31f Leszek Koltunski
      Dynamic1D chromaDyn = new Dynamic1D(10000,0.0f);
147
      chromaDyn.add(new Static1D(0.0f));
148
      chromaDyn.add(new Static1D(0.5f));
149 59759251 Leszek Koltunski
150 70d7a31f Leszek Koltunski
      mLisa.chroma(chromaDyn, new Static3D(0,0,1) );
151 427ab7bf Leszek Koltunski
      
152
      try
153
        {
154 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
155 427ab7bf Leszek Koltunski
        }
156
      catch(Exception ex)
157
        {
158 e8b6aa95 Leszek Koltunski
        android.util.Log.e("FBO", ex.getMessage() );
159 427ab7bf Leszek Koltunski
        }
160
      }
161
}