Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 885b9cca

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.effects3d;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.effect.EffectName;
28
import org.distorted.library.effect.EffectType;
29
import org.distorted.library.effect.FragmentEffectAlpha;
30
import org.distorted.library.effect.FragmentEffectBrightness;
31
import org.distorted.library.effect.FragmentEffectChroma;
32
import org.distorted.library.effect.FragmentEffectContrast;
33
import org.distorted.library.effect.FragmentEffectSaturation;
34
import org.distorted.library.effect.MatrixEffectMove;
35
import org.distorted.library.effect.MatrixEffectQuaternion;
36
import org.distorted.library.effect.MatrixEffectScale;
37
import org.distorted.library.effect.VertexEffectDeform;
38
import org.distorted.library.effect.VertexEffectDistort;
39
import org.distorted.library.effect.VertexEffectPinch;
40
import org.distorted.library.effect.VertexEffectSink;
41
import org.distorted.library.effect.VertexEffectSwirl;
42
import org.distorted.library.effect.VertexEffectWave;
43
import org.distorted.library.main.Distorted;
44
import org.distorted.library.main.DistortedEffects;
45
import org.distorted.library.main.DistortedNode;
46
import org.distorted.library.main.DistortedScreen;
47
import org.distorted.library.main.DistortedTexture;
48
import org.distorted.library.main.MeshFlat;
49
import org.distorted.library.main.MeshObject;
50
import org.distorted.library.type.Static3D;
51
import org.distorted.library.type.Static4D;
52

    
53
import java.io.IOException;
54
import java.io.InputStream;
55

    
56
import javax.microedition.khronos.egl.EGLConfig;
57
import javax.microedition.khronos.opengles.GL10;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
class Effects3DRenderer implements GLSurfaceView.Renderer
62
{
63
    private static final float FOV = 70.0f;
64
    private static final float NEAR = 0.1f;
65

    
66
    private GLSurfaceView mView;
67
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
68
    private DistortedScreen mScreen;
69
    private DistortedNode mCenterNode, mRegionNode;
70
    private int mObjWidth, mObjHeight;
71
    private Static3D mCenterPoint, mRegionPoint, mRegionScalePoint;
72
    private Static3D mRotateCen, mMoveObject, mScaleObject, mMoveCenter, mScaleCenter, mMoveRegion, mMoveBackground, mScaleBackground;
73
    private boolean mShowingCenter=false;
74
    private boolean mShowingRegion=false;
75
    private float mFactorObj, mFactorReg;
76

    
77
    Static4D mQuat1, mQuat2;
78
    int mScreenMin;
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
    Effects3DRenderer(GLSurfaceView v)
83
      {
84
      mView = v;
85

    
86
      Effects3DActivity act = (Effects3DActivity)v.getContext();
87

    
88
      mRotateCen      = new Static3D(0,0,0);
89
      mMoveObject     = new Static3D(0,0,0);
90
      mScaleObject    = new Static3D(1,1,1);
91
      mMoveCenter     = new Static3D(0,0,0);
92
      mScaleCenter    = new Static3D(1,1,1);
93
      mMoveRegion     = new Static3D(0,0,0);
94
      mMoveBackground = new Static3D(0,0,0);
95
      mScaleBackground= new Static3D(1,1,1);
96

    
97
      mObjectTexture     = act.getTexture();
98
      mBackgroundTexture = new DistortedTexture(100,100);
99
      mCenterTexture     = new DistortedTexture(100,100);
100
      mRegionTexture     = new DistortedTexture(100,100);
101

    
102
      DistortedEffects objectEffects     = act.getEffects();
103
      DistortedEffects backgroundEffects = new DistortedEffects();
104
      DistortedEffects centerEffects     = new DistortedEffects();
105
      DistortedEffects regionEffects     = new DistortedEffects();
106

    
107
      MeshObject meshO   = act.getMesh();
108
      MeshFlat quad      = new MeshFlat(1,1);
109

    
110
      mObjWidth = mObjectTexture.getWidth();
111
      mObjHeight= mObjectTexture.getHeight();
112
      int objDepth = mObjectTexture.getDepth(meshO);
113

    
114
      mQuat1 = new Static4D(0,0,0,1);  // unity
115
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
116

    
117
      mCenterPoint= new Static3D(0,0,0);
118
      mRegionPoint= new Static3D(0,0,0);
119
      mRegionScalePoint = new Static3D(0,0,0);
120

    
121
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
122
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
123

    
124
      mScreen = new DistortedScreen();
125
      mScreen.setProjection(FOV, NEAR);
126
      mScreen.attach(mBackgroundTexture, backgroundEffects, quad );
127
      mScreen.attach(mObjectTexture    , objectEffects    , meshO);
128

    
129
      int regionSize = mRegionTexture.getWidth();
130
      Static3D rotateObj = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, -(float)objDepth/2 );
131
      mRotateCen = new Static3D(0 ,0, 0);
132

    
133
      MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1,  rotateObj);
134
      MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2,  rotateObj);
135
      MatrixEffectQuaternion quat1cen = new MatrixEffectQuaternion(mQuat1, mRotateCen);
136
      MatrixEffectQuaternion quat2cen = new MatrixEffectQuaternion(mQuat2, mRotateCen);
137
      MatrixEffectMove centerMove = new MatrixEffectMove(mCenterPoint);
138

    
139
      objectEffects.apply( new MatrixEffectMove(mMoveObject));
140
      objectEffects.apply( new MatrixEffectScale(mScaleObject) );
141
      objectEffects.apply(quat1obj);
142
      objectEffects.apply(quat2obj);
143

    
144
      centerEffects.apply(quat1cen);
145
      centerEffects.apply(quat2cen);
146
      centerEffects.apply( new MatrixEffectMove(mMoveCenter) );
147
      centerEffects.apply( centerMove );
148
      centerEffects.apply( new MatrixEffectScale(mScaleCenter) );
149

    
150
      regionEffects.apply(quat1cen);
151
      regionEffects.apply(quat2cen);
152
      regionEffects.apply( new MatrixEffectMove(mMoveRegion) );
153
      regionEffects.apply( centerMove );
154
      regionEffects.apply( new MatrixEffectMove(mRegionPoint) );
155
      regionEffects.apply( new MatrixEffectScale(mRegionScalePoint) );
156
      regionEffects.apply( new MatrixEffectMove(new Static3D( -regionSize/2 , -regionSize/2 , 0)) );
157

    
158
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
159
      backgroundEffects.apply(new MatrixEffectMove(mMoveBackground) );
160
      backgroundEffects.apply(new MatrixEffectScale(mScaleBackground) );
161
      }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
166
      {
167
      if( mShowingCenter!=showCenter  )
168
        {
169
        if( showCenter ) mScreen.attach(mCenterNode);
170
        else             mScreen.detach(mCenterNode);
171

    
172
        mShowingCenter = showCenter;
173
        }
174

    
175
      if( mShowingRegion!=showRegion  )
176
        {
177
        if( showRegion ) mScreen.attach(mRegionNode);
178
        else             mScreen.detach(mRegionNode);
179

    
180
        mShowingRegion = showRegion;
181
        }
182
      }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
    void setCenter(float x, float y, float z)
187
      {
188
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
189
      }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
    void setRegion(float x, float y, float r)
194
      {
195
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
196
      mRegionPoint.set(mFactorObj*x,-mFactorObj*y,0);
197
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
198
      }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
    public void onDrawFrame(GL10 glUnused)
203
      {
204
      mScreen.render(System.currentTimeMillis());
205
      }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208

    
209
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
210
      {
211
      mScreenMin = width<height ? width:height;
212

    
213
      float factorCen;
214
      int centerSize = mCenterTexture.getWidth();
215
      int regionSize = mRegionTexture.getWidth();
216

    
217
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
218
        {
219
        mFactorObj = (0.80f*height)/mObjHeight;
220
        factorCen  = (0.08f*height)/centerSize;
221
        }
222
      else
223
        {
224
        mFactorObj = (0.80f*width)/mObjWidth;
225
        factorCen  = (0.08f*width)/centerSize;
226
        }
227

    
228
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
229
      mCenterPoint.set(mFactorObj*act.getCenterX(),+mFactorObj*act.getCenterY(),0);
230
      mRegionPoint.set(mFactorObj*act.getRegionX(),-mFactorObj*act.getRegionY(),0);
231
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
232
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
233
      mMoveObject.set( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0 );
234
      mRotateCen.set(width/2,height/2, 0);
235
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
236
      mMoveCenter.set( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
237
                       (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 15 );
238
      mScaleCenter.set(factorCen,factorCen,factorCen);
239
      mMoveRegion.set( (width -mFactorObj*mObjWidth )/2 ,(height-mFactorObj*mObjHeight)/2 , 12 );
240

    
241
      int backgroundSize = mBackgroundTexture.getWidth();
242
      float factorBackX = ((float)width)/backgroundSize;
243
      float factorBackY = ((float)height)/backgroundSize;
244

    
245
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
246
      mMoveBackground.set( -width/2, -height/2, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
247
      mScaleBackground.set( 2*factorBackX, 2*factorBackY, 1.0f );
248

    
249
      mScreen.resize(width, height);
250
      }
251

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

    
254
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
255
      {
256
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
257

    
258
      InputStream isB = act.getResources().openRawResource(R.raw.water);
259
      InputStream isC = act.getResources().openRawResource(R.raw.center);
260
      InputStream isR = act.getResources().openRawResource(R.raw.region);
261

    
262
      Bitmap bitmapB,bitmapC,bitmapR;
263
        
264
      try 
265
        {
266
        bitmapB = BitmapFactory.decodeStream(isB);
267
        bitmapC = BitmapFactory.decodeStream(isC);
268
        bitmapR = BitmapFactory.decodeStream(isR);
269
        }
270
      finally 
271
        {
272
        try 
273
          {
274
          isB.close();
275
          isC.close();
276
          isR.close();
277
          }
278
        catch(IOException e) { }
279
        }  
280
      
281
      mObjectTexture.setTexture( act.getBitmap() );
282
      mBackgroundTexture.setTexture(bitmapB);
283
      mCenterTexture.setTexture(bitmapC);
284
      mRegionTexture.setTexture(bitmapR);
285

    
286
      VertexEffectDeform.enable();
287
      VertexEffectDistort.enable();
288
      VertexEffectPinch.enable();
289
      VertexEffectSink.enable();
290
      VertexEffectSwirl.enable();
291
      VertexEffectWave.enable();
292

    
293
      FragmentEffectAlpha.enable();
294
      FragmentEffectBrightness.enable();
295
      FragmentEffectChroma.enable();
296
      FragmentEffectContrast.enable();
297
      FragmentEffectSaturation.enable();
298

    
299
      try
300
        {
301
        Distorted.onCreate(mView.getContext());
302
        }
303
      catch(Exception ex)
304
        {
305
        android.util.Log.e("Effects3D", ex.getMessage() );
306
        }
307
      }
308
}
(3-3/4)