Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 01782e85

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.differenteffects;
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 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
31
import org.distorted.library.main.DistortedEffects;
32
import org.distorted.library.main.DistortedScreen;
33 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
34 01782e85 Leszek Koltunski
import org.distorted.library.main.MeshFlat;
35
import org.distorted.library.main.DistortedTexture;
36 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
37 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
38 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
39 59759251 Leszek Koltunski
import org.distorted.library.type.Static1D;
40 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42 427ab7bf Leszek Koltunski
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 d04a4886 Leszek Koltunski
   private DistortedEffects[] mEffects;
55 b0ebdf5e Leszek Koltunski
   private DistortedTexture mTexture;
56 5f2a53b6 leszek
   private MeshFlat mMesh;
57 d218d64e leszek
   private DistortedScreen mScreen;
58 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
59
    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62 6c097dd3 Leszek Koltunski
   DifferentEffectsRenderer(GLSurfaceView v)
63 427ab7bf Leszek Koltunski
      {     
64
      mView = v;
65
      
66 392e16fd Leszek Koltunski
      // mEffects[0] effects
67 40eef4b9 Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      
71 392e16fd Leszek Koltunski
      // mEffects[1] effects
72 40eef4b9 Leszek Koltunski
      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 427ab7bf Leszek Koltunski
      
77 392e16fd Leszek Koltunski
      // we don't need to prepare anything for mEffects[2] effects
78 40eef4b9 Leszek Koltunski
79 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects[NUM];
80 40eef4b9 Leszek Koltunski
81 d04a4886 Leszek Koltunski
      for(int i=0; i<NUM; i++) mEffects[i] = new DistortedEffects();
82 40eef4b9 Leszek Koltunski
83
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
84
      sink.add(new Static1D( 1));
85
      sink.add(new Static1D(10));
86
87 392e16fd Leszek Koltunski
      mEffects[0].sink(sink, pLeft, RegionEye);
88
      mEffects[0].sink(sink, pRight,RegionEye);
89
      mEffects[1].distort(dyn, pNose1);
90 40eef4b9 Leszek Koltunski
91
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
92
      chromaDyn.add(new Static1D(0));
93
      chromaDyn.add(new Static1D(1));
94
95 392e16fd Leszek Koltunski
      mEffects[2].chroma(chromaDyn, new Static3D(0,1,0) );
96
97 1f9a52f1 leszek
      mScreen = new DistortedScreen(mView);
98 427ab7bf Leszek Koltunski
      }
99
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
   
102 5055b5d4 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
103
     {
104 fe59d375 Leszek Koltunski
     mScreen.render( System.currentTimeMillis() );
105 5055b5d4 Leszek Koltunski
     }
106 427ab7bf Leszek Koltunski
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
    
109 5055b5d4 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
110
     {
111
     for(int i=NUM-1; i>=0; i--)
112
       {
113 392e16fd Leszek Koltunski
       mEffects[i].abortEffects(EffectTypes.MATRIX);
114 5055b5d4 Leszek Koltunski
       }
115 630703d1 leszek
      
116
     if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
117 d79a56d3 Leszek Koltunski
       {
118 630703d1 leszek
       int w = (height*bmpWidth)/bmpHeight;
119
       float factor = (float)height/bmpHeight;
120
121
       for(int i=NUM-1; i>=0; i--)
122 5055b5d4 Leszek Koltunski
         {
123 630703d1 leszek
         mEffects[i].move( new Static3D((width-NUM*w)/2 +i*w , 0, 0) );
124
         mEffects[i].scale(factor);
125 5055b5d4 Leszek Koltunski
         }
126
       }
127
     else
128
       {
129 630703d1 leszek
       int w = width/NUM;
130
       int h = (width*bmpHeight)/(bmpWidth*NUM);
131
       float factor = (float)width/(bmpWidth*NUM);
132
133
       for(int i=NUM-1; i>=0; i--)
134 5055b5d4 Leszek Koltunski
         {
135 630703d1 leszek
         mEffects[i].move( new Static3D(i*w, (height-h)/2, 0) );
136
         mEffects[i].scale(factor);
137 5055b5d4 Leszek Koltunski
         }
138
       }
139 427ab7bf Leszek Koltunski
       
140 392e16fd Leszek Koltunski
     mScreen.resize(width, height);
141 5055b5d4 Leszek Koltunski
     }
142 427ab7bf Leszek Koltunski
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
    
145 5055b5d4 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
146
     {
147
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
148
     Bitmap bitmap;
149 427ab7bf Leszek Koltunski
        
150 5055b5d4 Leszek Koltunski
     try
151
       {
152
       bitmap = BitmapFactory.decodeStream(is);
153
       }
154
     finally
155
       {
156
       try
157
         {
158
         is.close();
159
         }
160
       catch(IOException e) { }
161
       }
162 427ab7bf Leszek Koltunski
      
163 5055b5d4 Leszek Koltunski
     bmpHeight = bitmap.getHeight();
164
     bmpWidth  = bitmap.getWidth();
165 e8b6aa95 Leszek Koltunski
166 5f2a53b6 leszek
     if( mMesh==null ) mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
167 b0ebdf5e Leszek Koltunski
     if( mTexture==null ) mTexture  = new DistortedTexture(bmpWidth,bmpHeight);
168
     mTexture.setTexture(bitmap);
169 e8b6aa95 Leszek Koltunski
170 fe59d375 Leszek Koltunski
     mScreen.detachAll();
171 5f2a53b6 leszek
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture, mEffects[i], mMesh);
172 7bf107f7 Leszek Koltunski
173 6637d0f2 Leszek Koltunski
     DistortedEffects.enableEffect(EffectNames.SINK);
174
     DistortedEffects.enableEffect(EffectNames.DISTORT);
175
     DistortedEffects.enableEffect(EffectNames.CHROMA);
176
177 5055b5d4 Leszek Koltunski
     try
178
       {
179 76f9798b Leszek Koltunski
       Distorted.onCreate(mView.getContext());
180 5055b5d4 Leszek Koltunski
       }
181
     catch(Exception ex)
182
       {
183
       android.util.Log.e("DifferentEffects", ex.getMessage() );
184
       }
185
     }
186 427ab7bf Leszek Koltunski
}