Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fbo / FBORenderer.java @ 596714ec

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 1746198f Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69
   void setDepth(boolean depth)
70
      {
71
      if( mRoot!=null )
72
        {
73 596714ec Leszek Koltunski
        DistortedFramebuffer fbo = mRoot.getFramebuffer();
74
        fbo.setDepthAttachment(depth);
75 1746198f Leszek Koltunski
        }
76
      }
77
78 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
79
   
80 1746198f Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
81 427ab7bf Leszek Koltunski
      {
82
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
83 bc29e409 Leszek Koltunski
      mScreen.renderTo(mRoot,System.currentTimeMillis());
84 427ab7bf Leszek Koltunski
      }
85
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
    
88 1746198f Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
89 427ab7bf Leszek Koltunski
      { 
90 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
91 427ab7bf Leszek Koltunski
         
92 5055b5d4 Leszek Koltunski
      if( (float)lisaHeight/lisaWidth > (float)height/width )
93 427ab7bf Leszek Koltunski
        {
94
        int w = (height*lisaWidth)/lisaHeight;
95 7589635e Leszek Koltunski
        float factor = (float)height/lisaHeight;
96
97 392e16fd Leszek Koltunski
        mEffects.move( new Static3D((width-w)/2,0,0) );
98
        mEffects.scale(factor);
99 427ab7bf Leszek Koltunski
        }
100
      else
101
        {
102
        int h = (width*lisaHeight)/lisaWidth;
103 7589635e Leszek Koltunski
        float factor = (float)width/lisaWidth;
104
105 392e16fd Leszek Koltunski
        mEffects.move( new Static3D(0,(height-h)/2,0) );
106
        mEffects.scale(factor);
107 427ab7bf Leszek Koltunski
        }
108
      
109 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
110 427ab7bf Leszek Koltunski
      }
111
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
    
114 1746198f Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
115 427ab7bf Leszek Koltunski
      {
116 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
117
118 427ab7bf Leszek Koltunski
      InputStream is1 = mView.getContext().getResources().openRawResource(R.raw.monalisa);
119 c0486401 Leszek Koltunski
      InputStream is2 = mView.getContext().getResources().openRawResource(R.raw.grid);
120 427ab7bf Leszek Koltunski
      
121
      Bitmap bitmap1, bitmap2;
122
       
123
      try 
124
        {
125
        bitmap1 = BitmapFactory.decodeStream(is1);
126
        bitmap2 = BitmapFactory.decodeStream(is2);
127
        } 
128
      finally 
129
        {
130
        try 
131
          {
132
          is1.close();
133
          is2.close();
134
          } 
135
        catch(IOException e) { }
136
        }  
137
      
138 f6d884d5 Leszek Koltunski
      lisaWidth     = bitmap1.getWidth();
139
      lisaHeight    = bitmap1.getHeight();
140 371d99fa Leszek Koltunski
      int gridWidth = bitmap2.getWidth();
141
      int gridHeight= bitmap2.getHeight();
142 f6d884d5 Leszek Koltunski
143 7451c98a Leszek Koltunski
      DistortedTexture lisa = new DistortedTexture(lisaWidth,lisaHeight);
144 371d99fa Leszek Koltunski
      DistortedTexture grid = new DistortedTexture(gridWidth,gridHeight);
145 f6d884d5 Leszek Koltunski
      lisa.setTexture(bitmap1);
146 371d99fa Leszek Koltunski
      grid.setTexture(bitmap2);
147
      DistortedEffects gridEffects = new DistortedEffects();
148 e8b6aa95 Leszek Koltunski
149 392e16fd Leszek Koltunski
      mEffects.abortAllEffects();
150 fd89ecb6 Leszek Koltunski
151 b01acdaf Leszek Koltunski
      mRoot = new DistortedTree(lisa, mEffects,new MeshFlat(1,1));
152 371d99fa Leszek Koltunski
      mRoot.attach(grid,gridEffects,new MeshCubes(10,10,false));
153 7589635e Leszek Koltunski
154 371d99fa Leszek Koltunski
      float factor = lisaWidth/(2.0f*gridWidth);
155 7589635e Leszek Koltunski
156 371d99fa Leszek Koltunski
      gridEffects.move( new Static3D( (lisaWidth-factor*gridWidth)/2,(lisaHeight-factor*gridHeight)/2,0) );
157
      gridEffects.scale(factor);
158 7bf107f7 Leszek Koltunski
159 c0486401 Leszek Koltunski
      Dynamic1D rotDyn = new Dynamic1D(12000,0.0f);
160
      rotDyn.add(new Static1D(  0));
161
      rotDyn.add(new Static1D(360));
162
      rotDyn.setMode(Dynamic.MODE_JUMP);
163
164 371d99fa Leszek Koltunski
      gridEffects.rotate(rotDyn, new Static3D(1,0,0), new Static3D(gridWidth/2,gridHeight/2,gridHeight/10) );
165 c0486401 Leszek Koltunski
166
      Dynamic1D sinkDyn = new Dynamic1D(3000,0.0f);
167 7bf107f7 Leszek Koltunski
      sinkDyn.add(new Static1D(1.0f));
168 c0486401 Leszek Koltunski
      sinkDyn.add(new Static1D(0.3f));
169 7bf107f7 Leszek Koltunski
170 371d99fa Leszek Koltunski
      gridEffects.sink(sinkDyn, new Static3D(gridWidth/2,gridHeight/2, 0));
171 59759251 Leszek Koltunski
172 c0486401 Leszek Koltunski
      Dynamic1D chromaDyn = new Dynamic1D(5000,0.0f);
173 70d7a31f Leszek Koltunski
      chromaDyn.add(new Static1D(0.0f));
174 c0486401 Leszek Koltunski
      chromaDyn.add(new Static1D(1.0f));
175 59759251 Leszek Koltunski
176 392e16fd Leszek Koltunski
      mEffects.chroma(chromaDyn, new Static3D(0,0,1) );
177 427ab7bf Leszek Koltunski
      
178
      try
179
        {
180 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
181 427ab7bf Leszek Koltunski
        }
182
      catch(Exception ex)
183
        {
184 e8b6aa95 Leszek Koltunski
        android.util.Log.e("FBO", ex.getMessage() );
185 427ab7bf Leszek Koltunski
        }
186
      }
187
}