Project

General

Profile

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

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

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.GridCubes;
33
import org.distorted.library.GridFlat;
34
import org.distorted.library.DistortedTexture;
35
import org.distorted.library.DistortedEffectQueues;
36
import org.distorted.library.EffectTypes;
37
import org.distorted.library.type.Dynamic;
38
import org.distorted.library.type.Dynamic1D;
39
import org.distorted.library.type.Static1D;
40
import org.distorted.library.type.Static3D;
41

    
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
44
import android.opengl.GLES20;
45
import android.opengl.GLSurfaceView;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
class FBORenderer implements GLSurfaceView.Renderer 
50
{
51
   private GLSurfaceView mView;
52
   private DistortedEffectQueues mQueues;
53
   private int lisaHeight, lisaWidth;
54
    
55
   private DistortedObjectTree mRoot;
56
    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
   FBORenderer(GLSurfaceView v)
60
      {
61
      mView = v;
62
      mQueues = new DistortedEffectQueues();
63
      }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
   
67
    public void onDrawFrame(GL10 glUnused) 
68
      {
69
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
70
      mRoot.draw(System.currentTimeMillis());
71
      }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
    
75
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
76
      { 
77
      mQueues.abortEffects(EffectTypes.MATRIX);
78
         
79
      if( (float)lisaHeight/lisaWidth > (float)height/width )
80
        {
81
        int w = (height*lisaWidth)/lisaHeight;
82
        float factor = (float)height/lisaHeight;
83

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

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

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

    
105
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
106
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
107
      
108
      Bitmap bitmap1, bitmap2;
109
       
110
      try 
111
        {
112
        bitmap1 = BitmapFactory.decodeStream(is1);
113
        bitmap2 = BitmapFactory.decodeStream(is2);
114
        } 
115
      finally 
116
        {
117
        try 
118
          {
119
          is1.close();
120
          is2.close();
121
          } 
122
        catch(IOException e) { }
123
        }  
124
      
125
      lisaWidth     = bitmap1.getWidth();
126
      lisaHeight    = bitmap1.getHeight();
127
      int textWidth = bitmap2.getWidth();
128
      int textHeight= bitmap2.getHeight();
129

    
130
      DistortedTexture lisa = new DistortedTexture(lisaWidth,lisaHeight);
131
      DistortedTexture text = new DistortedTexture(textWidth,textHeight);
132
      lisa.setTexture(bitmap1);
133
      text.setTexture(bitmap2);
134
      DistortedEffectQueues textQueues = new DistortedEffectQueues();
135

    
136
      mRoot = new DistortedObjectTree(lisa,mQueues,new GridFlat(1,1));
137
      mRoot.attach(text,textQueues,new GridCubes(20,5,false));
138

    
139
      float factor = lisaWidth/(2.0f*textWidth);
140

    
141
      textQueues.move( new Static3D( (lisaWidth-factor*textWidth)/2,(lisaHeight-factor*textHeight)/2,0) );
142
      textQueues.scale(factor);
143

    
144
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
145
      rotDyn.add(new Static1D(  0));
146
      rotDyn.add(new Static1D(360));
147
      rotDyn.setMode(Dynamic.MODE_JUMP);
148

    
149
      textQueues.rotate(rotDyn, new Static3D(1,0,0), new Static3D(textWidth/2,textHeight/2,textHeight/10) );
150

    
151
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
152
      sinkDyn.add(new Static1D(1.0f));
153
      sinkDyn.add(new Static1D(0.3f));
154

    
155
      textQueues.sink(sinkDyn, new Static3D(textWidth/2,textHeight/2, 0));
156

    
157
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
158
      chromaDyn.add(new Static1D(0.0f));
159
      chromaDyn.add(new Static1D(1.0f));
160

    
161
      mQueues.chroma(chromaDyn, new Static3D(0,0,1) );
162
      
163
      try
164
        {
165
        Distorted.onSurfaceCreated(mView.getContext());
166
        }
167
      catch(Exception ex)
168
        {
169
        android.util.Log.e("FBO", ex.getMessage() );
170
        }
171
      }
172
}
(2-2/3)