Project

General

Profile

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

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

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

    
43
import android.graphics.Bitmap;
44
import android.graphics.BitmapFactory;
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 DistortedScreen mScreen;
57
   private int bmpHeight, bmpWidth;
58
    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

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

    
78
      mEffects = new DistortedEffects[NUM];
79

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

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

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

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

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

    
96
      mScreen = new DistortedScreen();
97
      }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
   
101
   public void onDrawFrame(GL10 glUnused)
102
     {
103
     mScreen.render( System.currentTimeMillis() );
104
     }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
    
108
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
109
     {
110
     for(int i=NUM-1; i>=0; i--)
111
       {
112
       mEffects[i].abortEffects(EffectTypes.MATRIX);
113
       }
114

    
115
     float qx = (float)width /bmpWidth;
116
     float qy = (float)height/bmpHeight;
117

    
118
     if( qx > NUM*qy )  // screen more 'horizontal' than NUM bitmaps next to each other
119
       {
120
       for(int i=0; i<NUM; i++)
121
         {
122
         mEffects[i].move( new Static3D( qy*((0.5f+i)/NUM-0.5f), 0, 0) );
123
         mEffects[i].scale( new Static3D( qy/qx, 1, 1) );
124
         }
125
       }
126
     else
127
       {
128
       for(int i=0; i<NUM; i++)
129
         {
130
         mEffects[i].move( new Static3D( (0.5f+i)/NUM-0.5f, 0, 0) );
131
         mEffects[i].scale( new Static3D( 1.0f/NUM, (qx/qy)/NUM, 1) );
132
         }
133
       }
134
       
135
     mScreen.resize(width, height);
136
     }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139
    
140
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
141
     {
142
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
143
     Bitmap bitmap;
144
        
145
     try
146
       {
147
       bitmap = BitmapFactory.decodeStream(is);
148
       }
149
     finally
150
       {
151
       try
152
         {
153
         is.close();
154
         }
155
       catch(IOException e) { }
156
       }
157
      
158
     bmpHeight = bitmap.getHeight();
159
     bmpWidth  = bitmap.getWidth();
160

    
161
     MeshFlat mesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
162
     if( mTexture==null ) mTexture  = new DistortedTexture(bmpWidth,bmpHeight);
163
     mTexture.setTexture(bitmap);
164

    
165
     mScreen.detachAll();
166
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture, mEffects[i], mesh);
167

    
168
     DistortedEffects.enableEffect(EffectNames.SINK);
169
     DistortedEffects.enableEffect(EffectNames.DISTORT);
170
     DistortedEffects.enableEffect(EffectNames.CHROMA);
171

    
172
     try
173
       {
174
       Distorted.onCreate(mView.getContext());
175
       }
176
     catch(Exception ex)
177
       {
178
       android.util.Log.e("DifferentEffects", ex.getMessage() );
179
       }
180
     }
181
}
(2-2/3)