Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 061449ed

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 bc0a685b Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 bc0a685b Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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.FragmentEffectChroma;
31
import org.distorted.library.effect.MatrixEffectMove;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.effect.VertexEffectDistort;
34 40122d03 Leszek Koltunski
import org.distorted.library.effect.VertexEffectScale;
35 8dfa45c4 leszek
import org.distorted.library.effect.VertexEffectSink;
36 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
37 01782e85 Leszek Koltunski
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedScreen;
39 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
40 01782e85 Leszek Koltunski
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 061449ed Leszek Koltunski
class DifferentEffectsRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
54 427ab7bf Leszek Koltunski
{
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 f3ca895f Leszek Koltunski
   private MeshRectangles mMesh;
61 d218d64e leszek
   private DistortedScreen mScreen;
62 40122d03 Leszek Koltunski
   private Static3D mScale, mScaleBitmap;
63 8dfa45c4 leszek
   private Static3D[] mMove;
64 40122d03 Leszek Koltunski
   private int mBmpWidth, mBmpHeight;
65 8dfa45c4 leszek
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 8eed34d3 Leszek Koltunski
      Static3D pLeft = new Static3D(214 - 594/2, 394 - 600/2, 0);
74
      Static3D pRight= new Static3D(390 - 594/2, 388 - 600/2, 0);
75 513b2e9c Leszek Koltunski
      Static4D RegionEye = new Static4D(0,0,0, 60);
76
      Static4D RegionCen = new Static4D(0,0,0,300);
77
78 392e16fd Leszek Koltunski
      // mEffects[1] effects
79 40eef4b9 Leszek Koltunski
      Dynamic3D dyn = new Dynamic3D(1000,0.0f);
80
      dyn.add(new Static3D( 50,0,0));
81
      dyn.add(new Static3D(-50,0,0));
82 8eed34d3 Leszek Koltunski
      Static3D pNose1 = new Static3D(305 - 594/2, 260 - 600/2, 0);
83 427ab7bf Leszek Koltunski
      
84 392e16fd Leszek Koltunski
      // we don't need to prepare anything for mEffects[2] effects
85 40eef4b9 Leszek Koltunski
86 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects[NUM];
87 40eef4b9 Leszek Koltunski
88 698ad0a8 Leszek Koltunski
      for(int i=0; i<NUM; i++) mEffects[i] = new DistortedEffects();
89 40eef4b9 Leszek Koltunski
90 40122d03 Leszek Koltunski
      mScaleBitmap = new Static3D(1,1,1);
91
      VertexEffectScale scale = new VertexEffectScale(mScaleBitmap);
92
93
      mEffects[0].apply(scale);
94
      mEffects[1].apply(scale);
95
      mEffects[2].apply(scale);
96
97 40eef4b9 Leszek Koltunski
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
98
      sink.add(new Static1D( 1));
99
      sink.add(new Static1D(10));
100
101 8dfa45c4 leszek
      VertexEffectSink sinkL = new VertexEffectSink(sink, pLeft , RegionEye);
102
      VertexEffectSink sinkR = new VertexEffectSink(sink, pRight, RegionEye);
103 513b2e9c Leszek Koltunski
      VertexEffectDistort distort = new VertexEffectDistort(dyn,pNose1, RegionCen);
104 8dfa45c4 leszek
105
      mEffects[0].apply(sinkL);
106
      mEffects[0].apply(sinkR);
107
      mEffects[1].apply(distort);
108 40eef4b9 Leszek Koltunski
109
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
110
      chromaDyn.add(new Static1D(0));
111
      chromaDyn.add(new Static1D(1));
112
113 8dfa45c4 leszek
      FragmentEffectChroma chroma = new FragmentEffectChroma(chromaDyn, new Static3D(0,1,0));
114
      mEffects[2].apply(chroma);
115
116
      mScale = new Static3D(1,1,1);
117
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
118
      mMove  = new Static3D[NUM];
119
      MatrixEffectMove[] moveEffect = new MatrixEffectMove[NUM];
120
121
      for(int i=0; i<NUM; i++)
122
        {
123
        mMove[i] = new Static3D(0,0,0);
124
        moveEffect[i] = new MatrixEffectMove(mMove[i]);
125
        mEffects[i].apply(scaleEffect);
126 dae661e9 Leszek Koltunski
        mEffects[i].apply(moveEffect[i]);
127 8dfa45c4 leszek
        }
128 392e16fd Leszek Koltunski
129 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
130 427ab7bf Leszek Koltunski
      }
131
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
   
134 5055b5d4 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
135
     {
136 fe59d375 Leszek Koltunski
     mScreen.render( System.currentTimeMillis() );
137 5055b5d4 Leszek Koltunski
     }
138 427ab7bf Leszek Koltunski
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
    
141 5055b5d4 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
142
     {
143 40122d03 Leszek Koltunski
     float horiRatio = (float)width / (NUM*mBmpWidth);
144
     float vertRatio = (float)height/ (    mBmpHeight);
145
     float factor    = Math.min(horiRatio, vertRatio);
146 16b22aab Leszek Koltunski
147
     mScale.set( factor,factor,factor );
148
149
     for(int i=NUM-1; i>=0; i--) mMove[i].set((i-(NUM-1)/2.0f)*(width/NUM), 0, 0);
150
151 392e16fd Leszek Koltunski
     mScreen.resize(width, height);
152 5055b5d4 Leszek Koltunski
     }
153 427ab7bf Leszek Koltunski
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
    
156 5055b5d4 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
157
     {
158
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
159
     Bitmap bitmap;
160 427ab7bf Leszek Koltunski
        
161 5055b5d4 Leszek Koltunski
     try
162
       {
163
       bitmap = BitmapFactory.decodeStream(is);
164
       }
165
     finally
166
       {
167
       try
168
         {
169
         is.close();
170
         }
171
       catch(IOException e) { }
172
       }
173 427ab7bf Leszek Koltunski
      
174 40122d03 Leszek Koltunski
     mBmpHeight = bitmap.getHeight();
175
     mBmpWidth  = bitmap.getWidth();
176 e8b6aa95 Leszek Koltunski
177 40122d03 Leszek Koltunski
     mScaleBitmap.set(mBmpWidth,mBmpHeight,0);
178 687263cc Leszek Koltunski
179 40122d03 Leszek Koltunski
     if( mMesh   ==null ) mMesh = new MeshRectangles(30,30*mBmpHeight/mBmpWidth);
180 687263cc Leszek Koltunski
     if( mTexture==null ) mTexture  = new DistortedTexture();
181 b0ebdf5e Leszek Koltunski
     mTexture.setTexture(bitmap);
182 e8b6aa95 Leszek Koltunski
183 fe59d375 Leszek Koltunski
     mScreen.detachAll();
184 5f2a53b6 leszek
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture, mEffects[i], mMesh);
185 7bf107f7 Leszek Koltunski
186 40122d03 Leszek Koltunski
     VertexEffectScale.enable();
187 885b9cca leszek
     VertexEffectSink.enable();
188
     VertexEffectDistort.enable();
189
     FragmentEffectChroma.enable();
190 6637d0f2 Leszek Koltunski
191 061449ed Leszek Koltunski
     DistortedLibrary.onCreate(mView.getContext(), this);
192
     }
193
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
196
   public void distortedException(Exception ex)
197
     {
198
     android.util.Log.e("DifferentEffects", ex.getMessage() );
199 5055b5d4 Leszek Koltunski
     }
200 427ab7bf Leszek Koltunski
}