Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ 7451c98a

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.fbo;
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

    
30
import org.distorted.library.DistortedObjectTree;
31
import org.distorted.library.Distorted;
32
import org.distorted.library.GridFlat;
33
import org.distorted.library.DistortedTexture;
34
import org.distorted.library.DistortedEffectQueues;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Static1D;
38
import org.distorted.library.type.Static3D;
39

    
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
   private DistortedEffectQueues mQueues;
51
   private int lisaHeight, lisaWidth;
52
    
53
   private DistortedObjectTree mRoot;
54
    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
   FBORenderer(GLSurfaceView v)
58
      {
59
      mView = v;
60
      mQueues = new DistortedEffectQueues();
61
      }
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
      mQueues.abortEffects(EffectTypes.MATRIX);
76
         
77
      if( (float)lisaHeight/lisaWidth > (float)height/width )
78
        {
79
        int w = (height*lisaWidth)/lisaHeight;
80
        float factor = (float)height/lisaHeight;
81

    
82
        mQueues.move( new Static3D((width-w)/2,0,0) );
83
        mQueues.scale(factor);
84
        }
85
      else
86
        {
87
        int h = (width*lisaHeight)/lisaWidth;
88
        float factor = (float)width/lisaWidth;
89

    
90
        mQueues.move( new Static3D(0,(height-h)/2,0) );
91
        mQueues.scale(factor);
92
        }
93
      
94
      Distorted.onSurfaceChanged(width, height); 
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
    
99
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
100
      {
101
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
102

    
103
      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
      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);
129
      DistortedTexture text = new DistortedTexture(textWidth,textHeight);
130
      lisa.setTexture(bitmap1);
131
      text.setTexture(bitmap2);
132
      DistortedEffectQueues textQueues = new DistortedEffectQueues();
133

    
134
      mRoot = new DistortedObjectTree(lisa,mQueues,new GridFlat(1,1));
135
      mRoot.attach(text,textQueues,new GridFlat(20,5));
136

    
137
      float factor = lisaWidth/(1.5f*textWidth);
138

    
139
      textQueues.move( new Static3D(lisaWidth/6,lisaHeight/3,0) );
140
      textQueues.scale(factor);
141

    
142
      Dynamic1D sinkDyn = new Dynamic1D(5000,0.0f);
143
      sinkDyn.add(new Static1D(1.0f));
144
      sinkDyn.add(new Static1D(0.5f));
145

    
146
      textQueues.sink(sinkDyn, new Static3D(textWidth/2,textHeight/2, 0));
147

    
148
      Dynamic1D chromaDyn = new Dynamic1D(10000,0.0f);
149
      chromaDyn.add(new Static1D(0.0f));
150
      chromaDyn.add(new Static1D(0.5f));
151

    
152
      mQueues.chroma(chromaDyn, new Static3D(0,0,1) );
153
      
154
      try
155
        {
156
        Distorted.onSurfaceCreated(mView.getContext());
157
        }
158
      catch(Exception ex)
159
        {
160
        android.util.Log.e("FBO", ex.getMessage() );
161
        }
162
      }
163
}
(2-2/3)