Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ bddd4b2d

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.FragmentEffectAlpha;
28
import org.distorted.library.effect.FragmentEffectBrightness;
29
import org.distorted.library.effect.FragmentEffectChroma;
30
import org.distorted.library.effect.FragmentEffectContrast;
31
import org.distorted.library.effect.FragmentEffectSaturation;
32
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectQuaternion;
34
import org.distorted.library.effect.MatrixEffectScale;
35
import org.distorted.library.effect.VertexEffectDeform;
36
import org.distorted.library.effect.VertexEffectDistort;
37
import org.distorted.library.effect.VertexEffectPinch;
38
import org.distorted.library.effect.VertexEffectSink;
39
import org.distorted.library.effect.VertexEffectSwirl;
40
import org.distorted.library.effect.VertexEffectWave;
41
import org.distorted.library.main.Distorted;
42
import org.distorted.library.main.DistortedEffects;
43
import org.distorted.library.main.DistortedNode;
44
import org.distorted.library.main.DistortedScreen;
45
import org.distorted.library.main.DistortedTexture;
46
import org.distorted.library.main.MeshFlat;
47
import org.distorted.library.main.MeshObject;
48
import org.distorted.library.type.Static3D;
49
import org.distorted.library.type.Static4D;
50

    
51
import java.io.IOException;
52
import java.io.InputStream;
53

    
54
import javax.microedition.khronos.egl.EGLConfig;
55
import javax.microedition.khronos.opengles.GL10;
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

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

    
75
    Static4D mQuat1, mQuat2;
76
    int mScreenMin;
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
    Effects3DRenderer(GLSurfaceView v)
81
      {
82
      mView = v;
83

    
84
      Effects3DActivity act = (Effects3DActivity)v.getContext();
85

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

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

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

    
105
      MeshObject meshO   = act.getMesh();
106
      MeshFlat quad      = new MeshFlat(1,1);
107

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

    
112
      mQuat1 = new Static4D(0,0,0,1);  // unity
113
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
114

    
115
      mCenterPoint= new Static3D(0,0,0);
116
      mRegionPoint= new Static3D(0,0,0);
117
      mRegionScalePoint = new Static3D(0,0,0);
118

    
119
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
120
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
121

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

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

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

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

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

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

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

    
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

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

    
170
        mShowingCenter = showCenter;
171
        }
172

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

    
178
        mShowingRegion = showRegion;
179
        }
180
      }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

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

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
    void useOIT(boolean use)
201
      {
202
      mScreen.setOrderIndependentTransparency(use);
203
      }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
    public void onDrawFrame(GL10 glUnused)
208
      {
209
      mScreen.render(System.currentTimeMillis());
210
      }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
215
      {
216
      mScreenMin = width<height ? width:height;
217

    
218
      float factorCen;
219
      int centerSize = mCenterTexture.getWidth();
220
      int regionSize = mRegionTexture.getWidth();
221

    
222
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
223
        {
224
        mFactorObj = (0.80f*height)/mObjHeight;
225
        factorCen  = (0.08f*height)/centerSize;
226
        }
227
      else
228
        {
229
        mFactorObj = (0.80f*width)/mObjWidth;
230
        factorCen  = (0.08f*width)/centerSize;
231
        }
232

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

    
246
      int backgroundSize = mBackgroundTexture.getWidth();
247
      float factorBackX = ((float)width)/backgroundSize;
248
      float factorBackY = ((float)height)/backgroundSize;
249

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

    
254
      mScreen.resize(width, height);
255
      }
256

    
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258

    
259
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
260
      {
261
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
262

    
263
      InputStream isB = act.getResources().openRawResource(R.raw.water);
264
      InputStream isC = act.getResources().openRawResource(R.raw.center);
265
      InputStream isR = act.getResources().openRawResource(R.raw.region);
266

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

    
291
      VertexEffectDeform.enable();
292
      VertexEffectDistort.enable();
293
      VertexEffectPinch.enable();
294
      VertexEffectSink.enable();
295
      VertexEffectSwirl.enable();
296
      VertexEffectWave.enable();
297

    
298
      FragmentEffectAlpha.enable();
299
      FragmentEffectBrightness.enable();
300
      FragmentEffectChroma.enable();
301
      FragmentEffectContrast.enable();
302
      FragmentEffectSaturation.enable();
303

    
304
      try
305
        {
306
        Distorted.onCreate(mView.getContext());
307
        }
308
      catch(Exception ex)
309
        {
310
        android.util.Log.e("Effects3D", ex.getMessage() );
311
        }
312
      }
313
}
(3-3/4)