Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 513b2e9c

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 513b2e9c Leszek Koltunski
      Static4D RegionEye = new Static4D(0,0,0, 60);
74
      Static4D RegionCen = new Static4D(0,0,0,300);
75
76 392e16fd Leszek Koltunski
      // mEffects[1] effects
77 40eef4b9 Leszek Koltunski
      Dynamic3D dyn = new Dynamic3D(1000,0.0f);
78
      dyn.add(new Static3D( 50,0,0));
79
      dyn.add(new Static3D(-50,0,0));
80 8eed34d3 Leszek Koltunski
      Static3D pNose1 = new Static3D(305 - 594/2, 260 - 600/2, 0);
81 427ab7bf Leszek Koltunski
      
82 392e16fd Leszek Koltunski
      // we don't need to prepare anything for mEffects[2] effects
83 40eef4b9 Leszek Koltunski
84 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects[NUM];
85 40eef4b9 Leszek Koltunski
86 698ad0a8 Leszek Koltunski
      for(int i=0; i<NUM; i++) mEffects[i] = new DistortedEffects();
87 40eef4b9 Leszek Koltunski
88
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
89
      sink.add(new Static1D( 1));
90
      sink.add(new Static1D(10));
91
92 8dfa45c4 leszek
      VertexEffectSink sinkL = new VertexEffectSink(sink, pLeft , RegionEye);
93
      VertexEffectSink sinkR = new VertexEffectSink(sink, pRight, RegionEye);
94 513b2e9c Leszek Koltunski
      VertexEffectDistort distort = new VertexEffectDistort(dyn,pNose1, RegionCen);
95 8dfa45c4 leszek
96
      mEffects[0].apply(sinkL);
97
      mEffects[0].apply(sinkR);
98
      mEffects[1].apply(distort);
99 40eef4b9 Leszek Koltunski
100
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
101
      chromaDyn.add(new Static1D(0));
102
      chromaDyn.add(new Static1D(1));
103
104 8dfa45c4 leszek
      FragmentEffectChroma chroma = new FragmentEffectChroma(chromaDyn, new Static3D(0,1,0));
105
      mEffects[2].apply(chroma);
106
107
      mScale = new Static3D(1,1,1);
108
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
109
      mMove  = new Static3D[NUM];
110
      MatrixEffectMove[] moveEffect = new MatrixEffectMove[NUM];
111
112
      for(int i=0; i<NUM; i++)
113
        {
114
        mMove[i] = new Static3D(0,0,0);
115
        moveEffect[i] = new MatrixEffectMove(mMove[i]);
116
        mEffects[i].apply(scaleEffect);
117 dae661e9 Leszek Koltunski
        mEffects[i].apply(moveEffect[i]);
118 8dfa45c4 leszek
        }
119 392e16fd Leszek Koltunski
120 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
121 427ab7bf Leszek Koltunski
      }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
   
125 5055b5d4 Leszek Koltunski
   public void onDrawFrame(GL10 glUnused)
126
     {
127 fe59d375 Leszek Koltunski
     mScreen.render( System.currentTimeMillis() );
128 5055b5d4 Leszek Koltunski
     }
129 427ab7bf Leszek Koltunski
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
    
132 5055b5d4 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
133
     {
134 126393c8 Leszek Koltunski
     float horiRatio = (float)width / (NUM*mMesh.getStretchX());
135
     float vertRatio = (float)height/ (    mMesh.getStretchY());
136 16b22aab Leszek Koltunski
     float factor    = horiRatio > vertRatio ? vertRatio : horiRatio;
137
138
     mScale.set( factor,factor,factor );
139
140
     for(int i=NUM-1; i>=0; i--) mMove[i].set((i-(NUM-1)/2.0f)*(width/NUM), 0, 0);
141
142 392e16fd Leszek Koltunski
     mScreen.resize(width, height);
143 5055b5d4 Leszek Koltunski
     }
144 427ab7bf Leszek Koltunski
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
    
147 5055b5d4 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
148
     {
149
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
150
     Bitmap bitmap;
151 427ab7bf Leszek Koltunski
        
152 5055b5d4 Leszek Koltunski
     try
153
       {
154
       bitmap = BitmapFactory.decodeStream(is);
155
       }
156
     finally
157
       {
158
       try
159
         {
160
         is.close();
161
         }
162
       catch(IOException e) { }
163
       }
164 427ab7bf Leszek Koltunski
      
165 16b22aab Leszek Koltunski
     int bmpHeight = bitmap.getHeight();
166
     int bmpWidth  = bitmap.getWidth();
167 e8b6aa95 Leszek Koltunski
168 698ad0a8 Leszek Koltunski
     if( mMesh==null )
169
       {
170
       mMesh = new MeshRectangles(30,30*bmpHeight/bmpWidth);
171
       mMesh.setStretch(bmpWidth, bmpHeight, 0);
172
       }
173 687263cc Leszek Koltunski
174
     if( mTexture==null ) mTexture  = new DistortedTexture();
175 b0ebdf5e Leszek Koltunski
     mTexture.setTexture(bitmap);
176 e8b6aa95 Leszek Koltunski
177 fe59d375 Leszek Koltunski
     mScreen.detachAll();
178 5f2a53b6 leszek
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture, mEffects[i], mMesh);
179 7bf107f7 Leszek Koltunski
180 885b9cca leszek
     VertexEffectSink.enable();
181
     VertexEffectDistort.enable();
182
     FragmentEffectChroma.enable();
183 6637d0f2 Leszek Koltunski
184 5055b5d4 Leszek Koltunski
     try
185
       {
186 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
187 5055b5d4 Leszek Koltunski
       }
188
     catch(Exception ex)
189
       {
190
       android.util.Log.e("DifferentEffects", ex.getMessage() );
191
       }
192
     }
193 427ab7bf Leszek Koltunski
}