Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 107e4b72

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
20
package org.distorted.examples.differenteffects;
21

    
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
import org.distorted.examples.R;
29

    
30
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
import org.distorted.library.main.Distorted;
36
import org.distorted.library.main.DistortedEffects;
37
import org.distorted.library.main.DistortedScreen;
38
import org.distorted.library.mesh.MeshFlat;
39
import org.distorted.library.main.DistortedTexture;
40
import org.distorted.library.type.Dynamic1D;
41
import org.distorted.library.type.Dynamic3D;
42
import org.distorted.library.type.Static1D;
43
import org.distorted.library.type.Static3D;
44
import org.distorted.library.type.Static4D;
45

    
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
   private DistortedEffects[] mEffects;
58
   private DistortedTexture mTexture;
59
   private MeshFlat mMesh;
60
   private DistortedScreen mScreen;
61
   private int bmpHeight, bmpWidth;
62
   private Static3D mScale;
63
   private Static3D[] mMove;
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

    
67
   DifferentEffectsRenderer(GLSurfaceView v)
68
      {     
69
      mView = v;
70
      
71
      // mEffects[0] effects
72
      Static3D pLeft = new Static3D(214, 206, 0);
73
      Static3D pRight= new Static3D(390, 212, 0);
74
      Static4D RegionEye = new Static4D(0,0,60,60);
75
      
76
      // mEffects[1] effects
77
      Dynamic3D dyn = new Dynamic3D(1000,0.0f);
78
      dyn.add(new Static3D( 50,0,0));
79
      dyn.add(new Static3D(-50,0,0));
80
      Static3D pNose1 = new Static3D(305, 340, 0);
81
      
82
      // we don't need to prepare anything for mEffects[2] effects
83

    
84
      mEffects = new DistortedEffects[NUM];
85

    
86
      for(int i=0; i<NUM; i++) mEffects[i] = new DistortedEffects();
87

    
88
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
89
      sink.add(new Static1D( 1));
90
      sink.add(new Static1D(10));
91

    
92
      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

    
100
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
101
      chromaDyn.add(new Static1D(0));
102
      chromaDyn.add(new Static1D(1));
103

    
104
      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(moveEffect[i]);
117
        mEffects[i].apply(scaleEffect);
118
        }
119

    
120
      mScreen = new DistortedScreen();
121
      }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
   
125
   public void onDrawFrame(GL10 glUnused)
126
     {
127
     mScreen.render( System.currentTimeMillis() );
128
     }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
    
132
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
133
     {
134
     if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
135
       {
136
       int w = (height*bmpWidth)/bmpHeight;
137
       float factor = (float)height/bmpHeight;
138
       mScale.set(factor,factor,factor);
139
       for(int i=NUM-1; i>=0; i--) mMove[i].set((width-NUM*w)/2 +i*w , 0, 0);
140
       }
141
     else
142
       {
143
       int w = width/NUM;
144
       int h = (width*bmpHeight)/(bmpWidth*NUM);
145
       float factor = (float)width/(bmpWidth*NUM);
146
       mScale.set(factor,factor,factor);
147
       for(int i=NUM-1; i>=0; i--) mMove[i].set(i*w, (height-h)/2, 0);
148
       }
149
       
150
     mScreen.resize(width, height);
151
     }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
    
155
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
156
     {
157
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
158
     Bitmap bitmap;
159
        
160
     try
161
       {
162
       bitmap = BitmapFactory.decodeStream(is);
163
       }
164
     finally
165
       {
166
       try
167
         {
168
         is.close();
169
         }
170
       catch(IOException e) { }
171
       }
172
      
173
     bmpHeight = bitmap.getHeight();
174
     bmpWidth  = bitmap.getWidth();
175

    
176
     if( mMesh==null ) mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
177
     if( mTexture==null ) mTexture  = new DistortedTexture(bmpWidth,bmpHeight);
178
     mTexture.setTexture(bitmap);
179

    
180
     mScreen.detachAll();
181
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture, mEffects[i], mMesh);
182

    
183
     VertexEffectSink.enable();
184
     VertexEffectDistort.enable();
185
     FragmentEffectChroma.enable();
186

    
187
     try
188
       {
189
       Distorted.onCreate(mView.getContext());
190
       }
191
     catch(Exception ex)
192
       {
193
       android.util.Log.e("DifferentEffects", ex.getMessage() );
194
       }
195
     }
196
}
(2-2/3)