Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ d04a4886

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.differenteffects;
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.Distorted;
31
import org.distorted.library.DistortedEffects;
32
import org.distorted.library.DistortedFramebuffer;
33
import org.distorted.library.GridFlat;
34
import org.distorted.library.DistortedTexture;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.type.Dynamic1D;
37
import org.distorted.library.type.Dynamic3D;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
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 DifferentEffectsRenderer implements GLSurfaceView.Renderer 
50
{
51
   private static final int NUM = 3;
52
   
53
   private GLSurfaceView mView;
54
   private DistortedEffects[] mEffects;
55
   private DistortedTexture mTexture;
56
   private GridFlat mGrid;
57
   private DistortedFramebuffer mScreen;
58
   private int bmpHeight, bmpWidth;
59
    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
   DifferentEffectsRenderer(GLSurfaceView v)
63
      {     
64
      mView = v;
65
      
66
      // mEffects[0] effects
67
      Static3D pLeft = new Static3D(214, 206, 0);
68
      Static3D pRight= new Static3D(390, 212, 0);
69
      Static4D RegionEye = new Static4D(0,0,60,60);
70
      
71
      // mEffects[1] effects
72
      Dynamic3D dyn = new Dynamic3D(1000,0.0f);
73
      dyn.add(new Static3D( 50,0,0));
74
      dyn.add(new Static3D(-50,0,0));
75
      Static3D pNose1 = new Static3D(305, 340, 0);
76
      
77
      // we don't need to prepare anything for mEffects[2] effects
78

    
79
      mEffects = new DistortedEffects[NUM];
80

    
81
      for(int i=0; i<NUM; i++) mEffects[i] = new DistortedEffects();
82

    
83
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
84
      sink.add(new Static1D( 1));
85
      sink.add(new Static1D(10));
86

    
87
      mEffects[0].sink(sink, pLeft, RegionEye);
88
      mEffects[0].sink(sink, pRight,RegionEye);
89
      mEffects[1].distort(dyn, pNose1);
90

    
91
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
92
      chromaDyn.add(new Static1D(0));
93
      chromaDyn.add(new Static1D(1));
94

    
95
      mEffects[2].chroma(chromaDyn, new Static3D(0,1,0) );
96

    
97
      mScreen = new DistortedFramebuffer(0);
98
      }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
   
102
   public void onDrawFrame(GL10 glUnused)
103
     {
104
     GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
105
      
106
     long time = System.currentTimeMillis();
107
   
108
     for(int i=NUM-1; i>=0; i--) mEffects[i].draw(time, mTexture, mGrid, mScreen);
109
     }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112
    
113
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
114
     {
115
     for(int i=NUM-1; i>=0; i--)
116
       {
117
       mEffects[i].abortEffects(EffectTypes.MATRIX);
118
       }
119
      
120
     if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
121
       {
122
       int w = (height*bmpWidth)/bmpHeight;
123
       float factor = (float)height/bmpHeight;
124

    
125
       for(int i=NUM-1; i>=0; i--)
126
         {
127
         mEffects[i].move( new Static3D((width-NUM*w)/2 +i*w , 0, 0) );
128
         mEffects[i].scale(factor);
129
         }
130
       }
131
     else
132
       {
133
       int w = width/NUM;
134
       int h = (width*bmpHeight)/(bmpWidth*NUM);
135
       float factor = (float)width/(bmpWidth*NUM);
136

    
137
       for(int i=NUM-1; i>=0; i--)
138
         {
139
         mEffects[i].move( new Static3D(i*w, (height-h)/2, 0) );
140
         mEffects[i].scale(factor);
141
         }
142
       }
143
       
144
     mScreen.resize(width, height);
145
     }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
    
149
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
150
     {
151
     GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
152

    
153
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
154
     Bitmap bitmap;
155
        
156
     try
157
       {
158
       bitmap = BitmapFactory.decodeStream(is);
159
       }
160
     finally
161
       {
162
       try
163
         {
164
         is.close();
165
         }
166
       catch(IOException e) { }
167
       }
168
      
169
     bmpHeight = bitmap.getHeight();
170
     bmpWidth  = bitmap.getWidth();
171

    
172
     mGrid     = new GridFlat(30,30*bmpHeight/bmpWidth);
173
     mTexture  = new DistortedTexture(bmpWidth,bmpHeight);
174

    
175
     mTexture.setTexture(bitmap);
176

    
177
     try
178
       {
179
       Distorted.onSurfaceCreated(mView.getContext());
180
       }
181
     catch(Exception ex)
182
       {
183
       android.util.Log.e("DifferentEffects", ex.getMessage() );
184
       }
185
     }
186
}
(2-2/3)