Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 8eed34d3

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