Project

General

Profile

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

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

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.EffectName;
31
import org.distorted.library.effect.FragmentEffectChroma;
32
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectScale;
34
import org.distorted.library.effect.VertexEffectDistort;
35
import org.distorted.library.effect.VertexEffectSink;
36
import org.distorted.library.main.Distorted;
37
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.main.MeshFlat;
40
import org.distorted.library.main.DistortedTexture;
41
import org.distorted.library.type.Dynamic1D;
42
import org.distorted.library.type.Dynamic3D;
43
import org.distorted.library.type.Static1D;
44
import org.distorted.library.type.Static3D;
45
import org.distorted.library.type.Static4D;
46

    
47
import android.graphics.Bitmap;
48
import android.graphics.BitmapFactory;
49
import android.opengl.GLSurfaceView;
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
class DifferentEffectsRenderer implements GLSurfaceView.Renderer 
54
{
55
   private static final int NUM = 3;
56
   
57
   private GLSurfaceView mView;
58
   private DistortedEffects[] mEffects;
59
   private DistortedTexture mTexture;
60
   private MeshFlat mMesh;
61
   private DistortedScreen mScreen;
62
   private int bmpHeight, bmpWidth;
63
   private Static3D mScale;
64
   private Static3D[] mMove;
65

    
66
///////////////////////////////////////////////////////////////////////////////////////////////////
67

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

    
85
      mEffects = new DistortedEffects[NUM];
86

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

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

    
93
      VertexEffectSink sinkL = new VertexEffectSink(sink, pLeft , RegionEye);
94
      VertexEffectSink sinkR = new VertexEffectSink(sink, pRight, RegionEye);
95
      VertexEffectDistort distort = new VertexEffectDistort(dyn,pNose1);
96

    
97
      mEffects[0].apply(sinkL);
98
      mEffects[0].apply(sinkR);
99
      mEffects[1].apply(distort);
100

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

    
105
      FragmentEffectChroma chroma = new FragmentEffectChroma(chromaDyn, new Static3D(0,1,0));
106
      mEffects[2].apply(chroma);
107

    
108
      mScale = new Static3D(1,1,1);
109
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
110
      mMove  = new Static3D[NUM];
111
      MatrixEffectMove[] moveEffect = new MatrixEffectMove[NUM];
112

    
113
      for(int i=0; i<NUM; i++)
114
        {
115
        mMove[i] = new Static3D(0,0,0);
116
        moveEffect[i] = new MatrixEffectMove(mMove[i]);
117
        mEffects[i].apply(moveEffect[i]);
118
        mEffects[i].apply(scaleEffect);
119
        }
120

    
121
      mScreen = new DistortedScreen();
122
      }
123

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

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
    
133
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
134
     {
135
     if( (float)bmpHeight/(NUM*bmpWidth) > (float)height/width )
136
       {
137
       int w = (height*bmpWidth)/bmpHeight;
138
       float factor = (float)height/bmpHeight;
139
       mScale.set(factor,factor,factor);
140

    
141
       for(int i=NUM-1; i>=0; i--)
142
         {
143
         mMove[i].set((width-NUM*w)/2 +i*w , 0, 0);
144
         }
145

    
146
       }
147
     else
148
       {
149
       int w = width/NUM;
150
       int h = (width*bmpHeight)/(bmpWidth*NUM);
151
       float factor = (float)width/(bmpWidth*NUM);
152
       mScale.set(factor,factor,factor);
153

    
154
       for(int i=NUM-1; i>=0; i--)
155
         {
156
         mMove[i].set(i*w, (height-h)/2, 0);
157
         }
158
       }
159
       
160
     mScreen.resize(width, height);
161
     }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
    
165
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
166
     {
167
     InputStream is = mView.getContext().getResources().openRawResource(R.raw.dog);
168
     Bitmap bitmap;
169
        
170
     try
171
       {
172
       bitmap = BitmapFactory.decodeStream(is);
173
       }
174
     finally
175
       {
176
       try
177
         {
178
         is.close();
179
         }
180
       catch(IOException e) { }
181
       }
182
      
183
     bmpHeight = bitmap.getHeight();
184
     bmpWidth  = bitmap.getWidth();
185

    
186
     if( mMesh==null ) mMesh = new MeshFlat(30,30*bmpHeight/bmpWidth);
187
     if( mTexture==null ) mTexture  = new DistortedTexture(bmpWidth,bmpHeight);
188
     mTexture.setTexture(bitmap);
189

    
190
     mScreen.detachAll();
191
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture, mEffects[i], mMesh);
192

    
193
     DistortedEffects.enableEffect(EffectName.SINK);
194
     DistortedEffects.enableEffect(EffectName.DISTORT);
195
     DistortedEffects.enableEffect(EffectName.CHROMA);
196

    
197
     try
198
       {
199
       Distorted.onCreate(mView.getContext());
200
       }
201
     catch(Exception ex)
202
       {
203
       android.util.Log.e("DifferentEffects", ex.getMessage() );
204
       }
205
     }
206
}
(2-2/3)