Project

General

Profile

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

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

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 8dfa45c4 leszek
import org.distorted.library.effect.EffectName;
31
import org.distorted.library.effect.FragmentEffectChroma;
32
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectDistort;
35
import org.distorted.library.effect.VertexEffectSink;
36 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
37
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.MeshFlat;
40
import org.distorted.library.main.DistortedTexture;
41 59759251 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
42 7589635e Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
43 59759251 Leszek Koltunski
import org.distorted.library.type.Static1D;
44 7589635e Leszek Koltunski
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46 427ab7bf Leszek Koltunski
47
import android.graphics.Bitmap;
48
import android.graphics.BitmapFactory;
49
import android.opengl.GLSurfaceView;
50
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53
class DifferentEffectsRenderer implements GLSurfaceView.Renderer 
54
{
55
   private static final int NUM = 3;
56
   
57
   private GLSurfaceView mView;
58 d04a4886 Leszek Koltunski
   private DistortedEffects[] mEffects;
59 b0ebdf5e Leszek Koltunski
   private DistortedTexture mTexture;
60 5f2a53b6 leszek
   private MeshFlat mMesh;
61 d218d64e leszek
   private DistortedScreen mScreen;
62 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
63 8dfa45c4 leszek
   private Static3D mScale;
64
   private Static3D[] mMove;
65
66 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
67
68 6c097dd3 Leszek Koltunski
   DifferentEffectsRenderer(GLSurfaceView v)
69 427ab7bf Leszek Koltunski
      {     
70
      mView = v;
71
      
72 392e16fd Leszek Koltunski
      // mEffects[0] effects
73 40eef4b9 Leszek Koltunski
      Static3D pLeft = new Static3D(214, 206, 0);
74
      Static3D pRight= new Static3D(390, 212, 0);
75
      Static4D RegionEye = new Static4D(0,0,60,60);
76 427ab7bf Leszek Koltunski
      
77 392e16fd Leszek Koltunski
      // mEffects[1] effects
78 40eef4b9 Leszek Koltunski
      Dynamic3D dyn = new Dynamic3D(1000,0.0f);
79
      dyn.add(new Static3D( 50,0,0));
80
      dyn.add(new Static3D(-50,0,0));
81
      Static3D pNose1 = new Static3D(305, 340, 0);
82 427ab7bf Leszek Koltunski
      
83 392e16fd Leszek Koltunski
      // we don't need to prepare anything for mEffects[2] effects
84 40eef4b9 Leszek Koltunski
85 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects[NUM];
86 40eef4b9 Leszek Koltunski
87 d04a4886 Leszek Koltunski
      for(int i=0; i<NUM; i++) mEffects[i] = new DistortedEffects();
88 40eef4b9 Leszek Koltunski
89
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
90
      sink.add(new Static1D( 1));
91
      sink.add(new Static1D(10));
92
93 8dfa45c4 leszek
      VertexEffectSink sinkL = new VertexEffectSink(sink, pLeft , RegionEye);
94
      VertexEffectSink sinkR = new VertexEffectSink(sink, pRight, RegionEye);
95
      VertexEffectDistort distort = new VertexEffectDistort(dyn,pNose1);
96
97
      mEffects[0].apply(sinkL);
98
      mEffects[0].apply(sinkR);
99
      mEffects[1].apply(distort);
100 40eef4b9 Leszek Koltunski
101
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
102
      chromaDyn.add(new Static1D(0));
103
      chromaDyn.add(new Static1D(1));
104
105 8dfa45c4 leszek
      FragmentEffectChroma chroma = new FragmentEffectChroma(chromaDyn, new Static3D(0,1,0));
106
      mEffects[2].apply(chroma);
107
108
      mScale = new Static3D(1,1,1);
109
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
110
      mMove  = new Static3D[NUM];
111
      MatrixEffectMove[] moveEffect = new MatrixEffectMove[NUM];
112
113
      for(int i=0; i<NUM; i++)
114
        {
115
        mMove[i] = new Static3D(0,0,0);
116
        moveEffect[i] = new MatrixEffectMove(mMove[i]);
117
        mEffects[i].apply(moveEffect[i]);
118
        mEffects[i].apply(scaleEffect);
119
        }
120 392e16fd Leszek Koltunski
121 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
122 427ab7bf Leszek Koltunski
      }
123
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
   
126 5055b5d4 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
127
     {
128 fe59d375 Leszek Koltunski
     mScreen.render( System.currentTimeMillis() );
129 5055b5d4 Leszek Koltunski
     }
130 427ab7bf Leszek Koltunski
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
    
133 5055b5d4 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
134
     {
135 630703d1 leszek
     if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
136 d79a56d3 Leszek Koltunski
       {
137 630703d1 leszek
       int w = (height*bmpWidth)/bmpHeight;
138
       float factor = (float)height/bmpHeight;
139 8dfa45c4 leszek
       mScale.set(factor,factor,factor);
140 630703d1 leszek
141
       for(int i=NUM-1; i>=0; i--)
142 5055b5d4 Leszek Koltunski
         {
143 8dfa45c4 leszek
         mMove[i].set((width-NUM*w)/2 +i*w , 0, 0);
144 5055b5d4 Leszek Koltunski
         }
145 8dfa45c4 leszek
146 5055b5d4 Leszek Koltunski
       }
147
     else
148
       {
149 630703d1 leszek
       int w = width/NUM;
150
       int h = (width*bmpHeight)/(bmpWidth*NUM);
151
       float factor = (float)width/(bmpWidth*NUM);
152 8dfa45c4 leszek
       mScale.set(factor,factor,factor);
153 630703d1 leszek
154
       for(int i=NUM-1; i>=0; i--)
155 5055b5d4 Leszek Koltunski
         {
156 8dfa45c4 leszek
         mMove[i].set(i*w, (height-h)/2, 0);
157 5055b5d4 Leszek Koltunski
         }
158
       }
159 427ab7bf Leszek Koltunski
       
160 392e16fd Leszek Koltunski
     mScreen.resize(width, height);
161 5055b5d4 Leszek Koltunski
     }
162 427ab7bf Leszek Koltunski
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
    
165 5055b5d4 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
166
     {
167
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
168
     Bitmap bitmap;
169 427ab7bf Leszek Koltunski
        
170 5055b5d4 Leszek Koltunski
     try
171
       {
172
       bitmap = BitmapFactory.decodeStream(is);
173
       }
174
     finally
175
       {
176
       try
177
         {
178
         is.close();
179
         }
180
       catch(IOException e) { }
181
       }
182 427ab7bf Leszek Koltunski
      
183 5055b5d4 Leszek Koltunski
     bmpHeight = bitmap.getHeight();
184
     bmpWidth  = bitmap.getWidth();
185 e8b6aa95 Leszek Koltunski
186 5f2a53b6 leszek
     if( mMesh==null ) mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
187 b0ebdf5e Leszek Koltunski
     if( mTexture==null ) mTexture  = new DistortedTexture(bmpWidth,bmpHeight);
188
     mTexture.setTexture(bitmap);
189 e8b6aa95 Leszek Koltunski
190 fe59d375 Leszek Koltunski
     mScreen.detachAll();
191 5f2a53b6 leszek
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture, mEffects[i], mMesh);
192 7bf107f7 Leszek Koltunski
193 8dfa45c4 leszek
     DistortedEffects.enableEffect(EffectName.SINK);
194
     DistortedEffects.enableEffect(EffectName.DISTORT);
195
     DistortedEffects.enableEffect(EffectName.CHROMA);
196 6637d0f2 Leszek Koltunski
197 5055b5d4 Leszek Koltunski
     try
198
       {
199 76f9798b Leszek Koltunski
       Distorted.onCreate(mView.getContext());
200 5055b5d4 Leszek Koltunski
       }
201
     catch(Exception ex)
202
       {
203
       android.util.Log.e("DifferentEffects", ex.getMessage() );
204
       }
205
     }
206 427ab7bf Leszek Koltunski
}