Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ 843646bd

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.DistortedEffects;
31
import org.distorted.library.DistortedFramebuffer;
32
import org.distorted.library.DistortedTree;
33
import org.distorted.library.Distorted;
34
import org.distorted.library.MeshCubes;
35
import org.distorted.library.MeshFlat;
36
import org.distorted.library.DistortedTexture;
37
import org.distorted.library.EffectTypes;
38
import org.distorted.library.type.Dynamic;
39
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.Static1D;
41
import org.distorted.library.type.Static3D;
42

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class FBORenderer implements GLSurfaceView.Renderer 
51
{
52
   private GLSurfaceView mView;
53
   private DistortedEffects mEffects;
54
   private DistortedFramebuffer mScreen;
55
   private DistortedTree mRoot;
56
   private int lisaHeight, lisaWidth;
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
   FBORenderer(GLSurfaceView v)
61
      {
62
      mView   = v;
63
      mEffects= new DistortedEffects();
64
      mScreen = new DistortedFramebuffer(0);
65
      }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
   
69
    public void onDrawFrame(GL10 glUnused) 
70
      {
71
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
72
      mScreen.renderTo(mRoot,System.currentTimeMillis());
73
      }
74

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

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

    
94
        mEffects.move( new Static3D(0,(height-h)/2,0) );
95
        mEffects.scale(factor);
96
        }
97
      
98
      mScreen.resize(width, height);
99
      }
100

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

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

    
132
      DistortedTexture lisa = new DistortedTexture(lisaWidth,lisaHeight);
133
      DistortedTexture grid = new DistortedTexture(gridWidth,gridHeight);
134
      lisa.setTexture(bitmap1);
135
      grid.setTexture(bitmap2);
136
      DistortedEffects gridEffects = new DistortedEffects();
137

    
138
      mEffects.abortAllEffects();
139

    
140
      mRoot = new DistortedTree(lisa, mEffects,new MeshFlat(1,1));
141
      mRoot.attach(grid,gridEffects,new MeshCubes(10,10,false));
142

    
143
      DistortedFramebuffer tmp = mRoot.getFramebuffer();
144
      tmp.setDepthAttachment(true);
145

    
146
      float factor = lisaWidth/(2.0f*gridWidth);
147

    
148
      gridEffects.move( new Static3D( (lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2,0) );
149
      gridEffects.scale(factor);
150

    
151
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
152
      rotDyn.add(new Static1D(  0));
153
      rotDyn.add(new Static1D(360));
154
      rotDyn.setMode(Dynamic.MODE_JUMP);
155

    
156
      gridEffects.rotate(rotDyn, new Static3D(1,0,0), new Static3D(gridWidth/2,gridHeight/2,gridHeight/10) );
157

    
158
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
159
      sinkDyn.add(new Static1D(1.0f));
160
      sinkDyn.add(new Static1D(0.3f));
161

    
162
      gridEffects.sink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
163

    
164
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
165
      chromaDyn.add(new Static1D(0.0f));
166
      chromaDyn.add(new Static1D(1.0f));
167

    
168
      mEffects.chroma(chromaDyn, new Static3D(0,0,1) );
169
      
170
      try
171
        {
172
        Distorted.onCreate(mView.getContext());
173
        }
174
      catch(Exception ex)
175
        {
176
        android.util.Log.e("FBO", ex.getMessage() );
177
        }
178
      }
179
}
(2-2/3)