Project

General

Profile

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

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

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.MeshSquare;
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.app.ActivityManager;
48
import android.content.Context;
49
import android.content.pm.ConfigurationInfo;
50
import android.content.res.Resources;
51
import android.graphics.Bitmap;
52
import android.graphics.BitmapFactory;
53
import android.opengl.GLSurfaceView;
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
class DifferentEffectsRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
58
{
59
   private static final int NUM = 3;
60
   
61
   private final GLSurfaceView mView;
62
   private final Resources mResources;
63
   private final DistortedEffects[] mEffects;
64
   private final DistortedScreen mScreen;
65
   private final Static3D mScale, mScaleBitmap;
66
   private final Static3D[] mMove;
67

    
68
   private DistortedTexture mTexture;
69
   private MeshSquare mMesh;
70
   private int mBmpWidth, mBmpHeight;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
   DifferentEffectsRenderer(GLSurfaceView v)
75
      {     
76
      mView = v;
77
      mResources = v.getResources();
78

    
79
      // mEffects[0] effects
80
      Static3D pLeft = new Static3D(214 - 594/2, 394 - 600/2, 0);
81
      Static3D pRight= new Static3D(390 - 594/2, 388 - 600/2, 0);
82
      Static4D RegionEye = new Static4D(0,0,0, 60);
83
      Static4D RegionCen = new Static4D(0,0,0,300);
84

    
85
      // mEffects[1] effects
86
      Dynamic3D dyn = new Dynamic3D(1000,0.0f);
87
      dyn.add(new Static3D( 50,0,0));
88
      dyn.add(new Static3D(-50,0,0));
89
      Static3D pNose1 = new Static3D(305 - 594/2, 260 - 600/2, 0);
90
      
91
      // we don't need to prepare anything for mEffects[2] effects
92

    
93
      mEffects = new DistortedEffects[NUM];
94

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

    
97
      mScaleBitmap = new Static3D(1,1,1);
98
      VertexEffectScale scale = new VertexEffectScale(mScaleBitmap);
99

    
100
      mEffects[0].apply(scale);
101
      mEffects[1].apply(scale);
102
      mEffects[2].apply(scale);
103

    
104
      Dynamic1D sink = new Dynamic1D(2000,0.0f);
105
      sink.add(new Static1D( 1));
106
      sink.add(new Static1D(10));
107

    
108
      VertexEffectSink sinkL = new VertexEffectSink(sink, pLeft , RegionEye);
109
      VertexEffectSink sinkR = new VertexEffectSink(sink, pRight, RegionEye);
110
      VertexEffectDistort distort = new VertexEffectDistort(dyn,pNose1, RegionCen);
111

    
112
      mEffects[0].apply(sinkL);
113
      mEffects[0].apply(sinkR);
114
      mEffects[1].apply(distort);
115

    
116
      Dynamic1D chromaDyn = new Dynamic1D(3000,0.0f);
117
      chromaDyn.add(new Static1D(0));
118
      chromaDyn.add(new Static1D(1));
119

    
120
      FragmentEffectChroma chroma = new FragmentEffectChroma(chromaDyn, new Static3D(0,1,0));
121
      mEffects[2].apply(chroma);
122

    
123
      mScale = new Static3D(1,1,1);
124
      MatrixEffectScale scaleEffect = new MatrixEffectScale(mScale);
125
      mMove  = new Static3D[NUM];
126
      MatrixEffectMove[] moveEffect = new MatrixEffectMove[NUM];
127

    
128
      for(int i=0; i<NUM; i++)
129
        {
130
        mMove[i] = new Static3D(0,0,0);
131
        moveEffect[i] = new MatrixEffectMove(mMove[i]);
132
        mEffects[i].apply(scaleEffect);
133
        mEffects[i].apply(moveEffect[i]);
134
        }
135

    
136
      mScreen = new DistortedScreen();
137
      }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
   
141
   public void onDrawFrame(GL10 glUnused)
142
     {
143
     mScreen.render( System.currentTimeMillis() );
144
     }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
    
148
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
149
     {
150
     float horiRatio = (float)width / (NUM*mBmpWidth);
151
     float vertRatio = (float)height/ (    mBmpHeight);
152
     float factor    = Math.min(horiRatio, vertRatio);
153

    
154
     mScale.set( factor,factor,factor );
155

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

    
158
     mScreen.resize(width, height);
159
     }
160

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

    
184
     mScaleBitmap.set(mBmpWidth,mBmpHeight,0);
185

    
186
     if( mMesh   ==null ) mMesh = new MeshSquare(30,30*mBmpHeight/mBmpWidth);
187
     if( mTexture==null ) mTexture  = new DistortedTexture();
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
     VertexEffectScale.enable();
194
     VertexEffectSink.enable();
195
     VertexEffectDistort.enable();
196
     FragmentEffectChroma.enable();
197

    
198
     DistortedLibrary.onSurfaceCreated(this);
199
     }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
   public void distortedException(Exception ex)
204
     {
205
     android.util.Log.e("DifferentEffects", ex.getMessage() );
206
     }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

    
210
   public int openGlVersion()
211
      {
212
      Context context = mView.getContext();
213
      final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
214
      final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
215
      int glESversion = configurationInfo.reqGlEsVersion;
216
      int major = glESversion >> 16;
217
      int minor = glESversion & 0xff;
218

    
219
      return 100*major + 10*minor;
220
      }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
   public InputStream localFile(int fileID)
225
      {
226
      return mResources.openRawResource(fileID);
227
      }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
   public void logMessage(String message)
232
      {
233
      android.util.Log.e("DifferentEffects", message );
234
      }
235
}
(2-2/3)