Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ f6d884d5

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