Project

General

Profile

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

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

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 427ab7bf Leszek Koltunski
   private int bmpHeight, bmpWidth;
62 8dfa45c4 leszek
   private Static3D mScale;
63
   private Static3D[] mMove;
64
65 427ab7bf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67 6c097dd3 Leszek Koltunski
   DifferentEffectsRenderer(GLSurfaceView v)
68 427ab7bf Leszek Koltunski
      {     
69
      mView = v;
70
      
71 392e16fd Leszek Koltunski
      // mEffects[0] effects
72 a4d59c0b Leszek Koltunski
      Static3D pLeft = new Static3D(214, 394, 0);
73
      Static3D pRight= new Static3D(390, 388, 0);
74 9e7b6dbd Leszek Koltunski
      Static4D RegionEye = new Static4D(0,0,0,60);
75 427ab7bf Leszek Koltunski
      
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 a4d59c0b Leszek Koltunski
      Static3D pNose1 = new Static3D(305, 260, 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 d04a4886 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
      VertexEffectDistort distort = new VertexEffectDistort(dyn,pNose1);
95
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 630703d1 leszek
     if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
135 d79a56d3 Leszek Koltunski
       {
136 630703d1 leszek
       int w = (height*bmpWidth)/bmpHeight;
137
       float factor = (float)height/bmpHeight;
138 8dfa45c4 leszek
       mScale.set(factor,factor,factor);
139 babda153 Leszek Koltunski
       for(int i=NUM-1; i>=0; i--) mMove[i].set((width-NUM*w)/2 +i*w , 0, 0);
140 5055b5d4 Leszek Koltunski
       }
141
     else
142
       {
143 630703d1 leszek
       int w = width/NUM;
144
       int h = (width*bmpHeight)/(bmpWidth*NUM);
145
       float factor = (float)width/(bmpWidth*NUM);
146 8dfa45c4 leszek
       mScale.set(factor,factor,factor);
147 babda153 Leszek Koltunski
       for(int i=NUM-1; i>=0; i--) mMove[i].set(i*w, (height-h)/2, 0);
148 5055b5d4 Leszek Koltunski
       }
149 427ab7bf Leszek Koltunski
       
150 392e16fd Leszek Koltunski
     mScreen.resize(width, height);
151 5055b5d4 Leszek Koltunski
     }
152 427ab7bf Leszek Koltunski
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
    
155 5055b5d4 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
156
     {
157
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
158
     Bitmap bitmap;
159 427ab7bf Leszek Koltunski
        
160 5055b5d4 Leszek Koltunski
     try
161
       {
162
       bitmap = BitmapFactory.decodeStream(is);
163
       }
164
     finally
165
       {
166
       try
167
         {
168
         is.close();
169
         }
170
       catch(IOException e) { }
171
       }
172 427ab7bf Leszek Koltunski
      
173 5055b5d4 Leszek Koltunski
     bmpHeight = bitmap.getHeight();
174
     bmpWidth  = bitmap.getWidth();
175 e8b6aa95 Leszek Koltunski
176 f3ca895f Leszek Koltunski
     if( mMesh==null ) mMesh = new MeshRectangles(30,30*bmpHeight/bmpWidth);
177 b0ebdf5e Leszek Koltunski
     if( mTexture==null ) mTexture  = new DistortedTexture(bmpWidth,bmpHeight);
178
     mTexture.setTexture(bitmap);
179 e8b6aa95 Leszek Koltunski
180 fe59d375 Leszek Koltunski
     mScreen.detachAll();
181 5f2a53b6 leszek
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture, mEffects[i], mMesh);
182 7bf107f7 Leszek Koltunski
183 885b9cca leszek
     VertexEffectSink.enable();
184
     VertexEffectDistort.enable();
185
     FragmentEffectChroma.enable();
186 6637d0f2 Leszek Koltunski
187 5055b5d4 Leszek Koltunski
     try
188
       {
189 e3900503 Leszek Koltunski
       DistortedLibrary.onCreate(mView.getContext());
190 5055b5d4 Leszek Koltunski
       }
191
     catch(Exception ex)
192
       {
193
       android.util.Log.e("DifferentEffects", ex.getMessage() );
194
       }
195
     }
196 427ab7bf Leszek Koltunski
}