Project

General

Profile

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

examples / src / main / java / org / distorted / examples / differenteffects / DifferentEffectsRenderer.java @ 061449ed

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.VertexEffectScale;
35
import org.distorted.library.effect.VertexEffectSink;
36
import org.distorted.library.main.DistortedLibrary;
37
import org.distorted.library.main.DistortedEffects;
38
import org.distorted.library.main.DistortedScreen;
39
import org.distorted.library.mesh.MeshRectangles;
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, DistortedLibrary.ExceptionListener
54
{
55
   private static final int NUM = 3;
56
   
57
   private GLSurfaceView mView;
58
   private DistortedEffects[] mEffects;
59
   private DistortedTexture mTexture;
60
   private MeshRectangles mMesh;
61
   private DistortedScreen mScreen;
62
   private Static3D mScale, mScaleBitmap;
63
   private Static3D[] mMove;
64
   private int mBmpWidth, mBmpHeight;
65

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

    
68
   DifferentEffectsRenderer(GLSurfaceView v)
69
      {     
70
      mView = v;
71
      
72
      // mEffects[0] effects
73
      Static3D pLeft = new Static3D(214 - 594/2, 394 - 600/2, 0);
74
      Static3D pRight= new Static3D(390 - 594/2, 388 - 600/2, 0);
75
      Static4D RegionEye = new Static4D(0,0,0, 60);
76
      Static4D RegionCen = new Static4D(0,0,0,300);
77

    
78
      // mEffects[1] effects
79
      Dynamic3D dyn = new Dynamic3D(1000,0.0f);
80
      dyn.add(new Static3D( 50,0,0));
81
      dyn.add(new Static3D(-50,0,0));
82
      Static3D pNose1 = new Static3D(305 - 594/2, 260 - 600/2, 0);
83
      
84
      // we don't need to prepare anything for mEffects[2] effects
85

    
86
      mEffects = new DistortedEffects[NUM];
87

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

    
90
      mScaleBitmap = new Static3D(1,1,1);
91
      VertexEffectScale scale = new VertexEffectScale(mScaleBitmap);
92

    
93
      mEffects[0].apply(scale);
94
      mEffects[1].apply(scale);
95
      mEffects[2].apply(scale);
96

    
97
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
98
      sink.add(new Static1D( 1));
99
      sink.add(new Static1D(10));
100

    
101
      VertexEffectSink sinkL = new VertexEffectSink(sink, pLeft , RegionEye);
102
      VertexEffectSink sinkR = new VertexEffectSink(sink, pRight, RegionEye);
103
      VertexEffectDistort distort = new VertexEffectDistort(dyn,pNose1, RegionCen);
104

    
105
      mEffects[0].apply(sinkL);
106
      mEffects[0].apply(sinkR);
107
      mEffects[1].apply(distort);
108

    
109
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
110
      chromaDyn.add(new Static1D(0));
111
      chromaDyn.add(new Static1D(1));
112

    
113
      FragmentEffectChroma chroma = new FragmentEffectChroma(chromaDyn, new Static3D(0,1,0));
114
      mEffects[2].apply(chroma);
115

    
116
      mScale = new Static3D(1,1,1);
117
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
118
      mMove  = new Static3D[NUM];
119
      MatrixEffectMove[] moveEffect = new MatrixEffectMove[NUM];
120

    
121
      for(int i=0; i<NUM; i++)
122
        {
123
        mMove[i] = new Static3D(0,0,0);
124
        moveEffect[i] = new MatrixEffectMove(mMove[i]);
125
        mEffects[i].apply(scaleEffect);
126
        mEffects[i].apply(moveEffect[i]);
127
        }
128

    
129
      mScreen = new DistortedScreen();
130
      }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
   
134
   public void onDrawFrame(GL10 glUnused)
135
     {
136
     mScreen.render( System.currentTimeMillis() );
137
     }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
    
141
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
142
     {
143
     float horiRatio = (float)width / (NUM*mBmpWidth);
144
     float vertRatio = (float)height/ (    mBmpHeight);
145
     float factor    = Math.min(horiRatio, vertRatio);
146

    
147
     mScale.set( factor,factor,factor );
148

    
149
     for(int i=NUM-1; i>=0; i--) mMove[i].set((i-(NUM-1)/2.0f)*(width/NUM), 0, 0);
150

    
151
     mScreen.resize(width, height);
152
     }
153

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

    
177
     mScaleBitmap.set(mBmpWidth,mBmpHeight,0);
178

    
179
     if( mMesh   ==null ) mMesh = new MeshRectangles(30,30*mBmpHeight/mBmpWidth);
180
     if( mTexture==null ) mTexture  = new DistortedTexture();
181
     mTexture.setTexture(bitmap);
182

    
183
     mScreen.detachAll();
184
     for(int i=NUM-1; i>=0; i--) mScreen.attach(mTexture, mEffects[i], mMesh);
185

    
186
     VertexEffectScale.enable();
187
     VertexEffectSink.enable();
188
     VertexEffectDistort.enable();
189
     FragmentEffectChroma.enable();
190

    
191
     DistortedLibrary.onCreate(mView.getContext(), this);
192
     }
193

    
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195

    
196
   public void distortedException(Exception ex)
197
     {
198
     android.util.Log.e("DifferentEffects", ex.getMessage() );
199
     }
200
}
(2-2/3)