Project

General

Profile

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

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

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 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
31 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
32 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedTree;
33 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
34 b01acdaf Leszek Koltunski
import org.distorted.library.MeshCubes;
35
import org.distorted.library.MeshFlat;
36 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
37 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
38 c0486401 Leszek Koltunski
import org.distorted.library.type.Dynamic;
39 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
40
import org.distorted.library.type.Static1D;
41 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
42 427ab7bf Leszek Koltunski
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 d04a4886 Leszek Koltunski
   private DistortedEffects mEffects;
54 392e16fd Leszek Koltunski
   private DistortedFramebuffer mScreen;
55 d04a4886 Leszek Koltunski
   private DistortedTree mRoot;
56 392e16fd Leszek Koltunski
   private int lisaHeight, lisaWidth;
57
58 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 7e6110e2 Leszek Koltunski
   FBORenderer(GLSurfaceView v)
61 427ab7bf Leszek Koltunski
      {
62 392e16fd Leszek Koltunski
      mView   = v;
63 d04a4886 Leszek Koltunski
      mEffects= new DistortedEffects();
64 392e16fd Leszek Koltunski
      mScreen = new DistortedFramebuffer(0);
65 427ab7bf Leszek Koltunski
      }
66
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
   
69
    public void onDrawFrame(GL10 glUnused) 
70
      {
71
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
72 bc29e409 Leszek Koltunski
      mScreen.renderTo(mRoot,System.currentTimeMillis());
73 427ab7bf Leszek Koltunski
      }
74
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
    
77
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
78
      { 
79 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
80 427ab7bf Leszek Koltunski
         
81 5055b5d4 Leszek Koltunski
      if( (float)lisaHeight/lisaWidth > (float)height/width )
82 427ab7bf Leszek Koltunski
        {
83
        int w = (height*lisaWidth)/lisaHeight;
84 7589635e Leszek Koltunski
        float factor = (float)height/lisaHeight;
85
86 392e16fd Leszek Koltunski
        mEffects.move( new Static3D((width-w)/2,0,0) );
87
        mEffects.scale(factor);
88 427ab7bf Leszek Koltunski
        }
89
      else
90
        {
91
        int h = (width*lisaHeight)/lisaWidth;
92 7589635e Leszek Koltunski
        float factor = (float)width/lisaWidth;
93
94 392e16fd Leszek Koltunski
        mEffects.move( new Static3D(0,(height-h)/2,0) );
95
        mEffects.scale(factor);
96 427ab7bf Leszek Koltunski
        }
97
      
98 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
99 427ab7bf Leszek Koltunski
      }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
    
103
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
104
      {
105 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
106
107 427ab7bf Leszek Koltunski
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
108 c0486401 Leszek Koltunski
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
109 427ab7bf Leszek Koltunski
      
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 f6d884d5 Leszek Koltunski
      lisaWidth     = bitmap1.getWidth();
128
      lisaHeight    = bitmap1.getHeight();
129 371d99fa Leszek Koltunski
      int gridWidth = bitmap2.getWidth();
130
      int gridHeight= bitmap2.getHeight();
131 f6d884d5 Leszek Koltunski
132 7451c98a Leszek Koltunski
      DistortedTexture lisa = new DistortedTexture(lisaWidth,lisaHeight);
133 371d99fa Leszek Koltunski
      DistortedTexture grid = new DistortedTexture(gridWidth,gridHeight);
134 f6d884d5 Leszek Koltunski
      lisa.setTexture(bitmap1);
135 371d99fa Leszek Koltunski
      grid.setTexture(bitmap2);
136
      DistortedEffects gridEffects = new DistortedEffects();
137 e8b6aa95 Leszek Koltunski
138 392e16fd Leszek Koltunski
      mEffects.abortAllEffects();
139 fd89ecb6 Leszek Koltunski
140 b01acdaf Leszek Koltunski
      mRoot = new DistortedTree(lisa, mEffects,new MeshFlat(1,1));
141 371d99fa Leszek Koltunski
      mRoot.attach(grid,gridEffects,new MeshCubes(10,10,false));
142 7589635e Leszek Koltunski
143 843646bd Leszek Koltunski
      DistortedFramebuffer tmp = mRoot.getFramebuffer();
144
      tmp.setDepthAttachment(true);
145
146 371d99fa Leszek Koltunski
      float factor = lisaWidth/(2.0f*gridWidth);
147 7589635e Leszek Koltunski
148 371d99fa Leszek Koltunski
      gridEffects.move( new Static3D( (lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2,0) );
149
      gridEffects.scale(factor);
150 7bf107f7 Leszek Koltunski
151 c0486401 Leszek Koltunski
      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 371d99fa Leszek Koltunski
      gridEffects.rotate(rotDyn, new Static3D(1,0,0), new Static3D(gridWidth/2,gridHeight/2,gridHeight/10) );
157 c0486401 Leszek Koltunski
158
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
159 7bf107f7 Leszek Koltunski
      sinkDyn.add(new Static1D(1.0f));
160 c0486401 Leszek Koltunski
      sinkDyn.add(new Static1D(0.3f));
161 7bf107f7 Leszek Koltunski
162 371d99fa Leszek Koltunski
      gridEffects.sink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
163 59759251 Leszek Koltunski
164 c0486401 Leszek Koltunski
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
165 70d7a31f Leszek Koltunski
      chromaDyn.add(new Static1D(0.0f));
166 c0486401 Leszek Koltunski
      chromaDyn.add(new Static1D(1.0f));
167 59759251 Leszek Koltunski
168 392e16fd Leszek Koltunski
      mEffects.chroma(chromaDyn, new Static3D(0,0,1) );
169 427ab7bf Leszek Koltunski
      
170
      try
171
        {
172 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
173 427ab7bf Leszek Koltunski
        }
174
      catch(Exception ex)
175
        {
176 e8b6aa95 Leszek Koltunski
        android.util.Log.e("FBO", ex.getMessage() );
177 427ab7bf Leszek Koltunski
        }
178
      }
179
}