Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ 8744aa41

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

    
44
import android.graphics.Bitmap;
45
import android.graphics.BitmapFactory;
46
import android.opengl.GLES30;
47
import android.opengl.GLSurfaceView;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

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

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
   void setDepth(boolean depth)
71
      {
72
      if( mRoot!=null )
73
        {
74
        DistortedFramebuffer fbo = mRoot.getFramebuffer();
75
        fbo.enableDepthAttachment(depth);
76
        }
77
      }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
   
81
   public void onDrawFrame(GL10 glUnused)
82
      {
83
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
84
      mScreen.renderTo(mRoot,System.currentTimeMillis());
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
    
89
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
90
      { 
91
      mEffects.abortEffects(EffectTypes.MATRIX);
92
         
93
      if( (float)lisaHeight/lisaWidth > (float)height/width )
94
        {
95
        int w = (height*lisaWidth)/lisaHeight;
96
        float factor = (float)height/lisaHeight;
97

    
98
        mEffects.move( new Static3D((width-w)/2,0,0) );
99
        mEffects.scale(factor);
100
        }
101
      else
102
        {
103
        int h = (width*lisaHeight)/lisaWidth;
104
        float factor = (float)width/lisaWidth;
105

    
106
        mEffects.move( new Static3D(0,(height-h)/2,0) );
107
        mEffects.scale(factor);
108
        }
109
      
110
      mScreen.resize(width, height);
111
      }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
    
115
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
116
      {
117
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
118

    
119
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
120
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
121
      
122
      Bitmap bitmap1, bitmap2;
123
       
124
      try 
125
        {
126
        bitmap1 = BitmapFactory.decodeStream(is1);
127
        bitmap2 = BitmapFactory.decodeStream(is2);
128
        } 
129
      finally 
130
        {
131
        try 
132
          {
133
          is1.close();
134
          is2.close();
135
          } 
136
        catch(IOException e) { }
137
        }  
138
      
139
      lisaWidth     = bitmap1.getWidth();
140
      lisaHeight    = bitmap1.getHeight();
141
      int gridWidth = bitmap2.getWidth();
142
      int gridHeight= bitmap2.getHeight();
143

    
144
      DistortedTexture lisa = new DistortedTexture(lisaWidth,lisaHeight);
145
      DistortedTexture grid = new DistortedTexture(gridWidth,gridHeight);
146
      lisa.setTexture(bitmap1);
147
      grid.setTexture(bitmap2);
148
      DistortedEffects gridEffects = new DistortedEffects();
149

    
150
      mEffects.abortAllEffects();
151

    
152
      mRoot = new DistortedNode(lisa, mEffects,new MeshFlat(1,1));
153
      mRoot.attach(grid,gridEffects,new MeshCubes(10,10,false));
154

    
155
      float factor = lisaWidth/(2.0f*gridWidth);
156

    
157
      gridEffects.move( new Static3D( (lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2,0) );
158
      gridEffects.scale(factor);
159

    
160
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
161
      rotDyn.add(new Static1D(  0));
162
      rotDyn.add(new Static1D(360));
163
      rotDyn.setMode(Dynamic.MODE_JUMP);
164

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

    
167
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
168
      sinkDyn.add(new Static1D(1.0f));
169
      sinkDyn.add(new Static1D(0.3f));
170

    
171
      gridEffects.sink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
172

    
173
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
174
      chromaDyn.add(new Static1D(0.0f));
175
      chromaDyn.add(new Static1D(1.0f));
176

    
177
      mEffects.chroma(chromaDyn, new Static3D(0,0,1) );
178
      
179
      try
180
        {
181
        Distorted.onCreate(mView.getContext());
182
        }
183
      catch(Exception ex)
184
        {
185
        android.util.Log.e("FBO", ex.getMessage() );
186
        }
187
      }
188
}
(2-2/3)