Project

General

Profile

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

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

1 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 76a81b6a Leszek Koltunski
package org.distorted.examples.effects3d;
21 08f92d82 Leszek Koltunski
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25
26
import org.distorted.examples.R;
27 885b9cca leszek
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 06366d12 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
33
import org.distorted.library.effect.MatrixEffectQuaternion;
34
import org.distorted.library.effect.MatrixEffectScale;
35 cdcbdbe3 Leszek Koltunski
import org.distorted.library.effect.PostprocessEffectBlur;
36
import org.distorted.library.effect.PostprocessEffectGlow;
37 885b9cca leszek
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 01782e85 Leszek Koltunski
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 08f92d82 Leszek Koltunski
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 76a81b6a Leszek Koltunski
class Effects3DRenderer implements GLSurfaceView.Renderer
62 08f92d82 Leszek Koltunski
{
63 af662543 leszek
    private static final float FOV = 70.0f;
64
    private static final float NEAR = 0.1f;
65
66 08f92d82 Leszek Koltunski
    private GLSurfaceView mView;
67 f6d884d5 Leszek Koltunski
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
68 d218d64e leszek
    private DistortedScreen mScreen;
69 fe59d375 Leszek Koltunski
    private DistortedNode mCenterNode, mRegionNode;
70 06366d12 Leszek Koltunski
    private int mObjWidth, mObjHeight;
71
    private Static3D mCenterPoint, mRegionPoint, mRegionScalePoint;
72 fec27f16 Leszek Koltunski
    private Static3D mRotateCen, mMoveObject, mScaleObject, mMoveCenter, mScaleCenter, mMoveRegion, mMoveBackground, mScaleBackground;
73
    private boolean mShowingCenter=false;
74
    private boolean mShowingRegion=false;
75 6f779cd4 Leszek Koltunski
    private float mFactorObj, mFactorReg;
76 950511ed Leszek Koltunski
77 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
78 d2337a3a Leszek Koltunski
    int mScreenMin;
79 833685d0 Leszek Koltunski
80 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
81
82 4f8ca8ff Leszek Koltunski
    Effects3DRenderer(GLSurfaceView v)
83 08f92d82 Leszek Koltunski
      {
84
      mView = v;
85 d40cfeb2 Leszek Koltunski
86 348dbeea Leszek Koltunski
      Effects3DActivity2 act = (Effects3DActivity2)v.getContext();
87 e8b6aa95 Leszek Koltunski
88 06366d12 Leszek Koltunski
      mRotateCen      = new Static3D(0,0,0);
89
      mMoveObject     = new Static3D(0,0,0);
90 fec27f16 Leszek Koltunski
      mScaleObject    = new Static3D(1,1,1);
91 06366d12 Leszek Koltunski
      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 f6d884d5 Leszek Koltunski
      mObjectTexture     = act.getTexture();
98 7451c98a Leszek Koltunski
      mBackgroundTexture = new DistortedTexture(100,100);
99
      mCenterTexture     = new DistortedTexture(100,100);
100
      mRegionTexture     = new DistortedTexture(100,100);
101 06366d12 Leszek Koltunski
102
      DistortedEffects objectEffects     = act.getEffects();
103
      DistortedEffects backgroundEffects = new DistortedEffects();
104
      DistortedEffects centerEffects     = new DistortedEffects();
105
      DistortedEffects regionEffects     = new DistortedEffects();
106 f6d884d5 Leszek Koltunski
107 aac5c562 leszek
      MeshObject meshO   = act.getMesh();
108
      MeshFlat quad      = new MeshFlat(1,1);
109
110 f6d884d5 Leszek Koltunski
      mObjWidth = mObjectTexture.getWidth();
111
      mObjHeight= mObjectTexture.getHeight();
112 06366d12 Leszek Koltunski
      int objDepth = mObjectTexture.getDepth(meshO);
113 d40cfeb2 Leszek Koltunski
114 833685d0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
115
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
116
117 24991bc2 Leszek Koltunski
      mCenterPoint= new Static3D(0,0,0);
118
      mRegionPoint= new Static3D(0,0,0);
119 6f779cd4 Leszek Koltunski
      mRegionScalePoint = new Static3D(0,0,0);
120 392e16fd Leszek Koltunski
121 06366d12 Leszek Koltunski
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
122
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
123 fe59d375 Leszek Koltunski
124 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
125 af662543 leszek
      mScreen.setProjection(FOV, NEAR);
126 06366d12 Leszek Koltunski
      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 fec27f16 Leszek Koltunski
      objectEffects.apply( new MatrixEffectScale(mScaleObject) );
141 06366d12 Leszek Koltunski
      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 f338550a leszek
      }
162
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
165 0ab55f0c Leszek Koltunski
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
166 f338550a leszek
      {
167 0ab55f0c Leszek Koltunski
      if( mShowingCenter!=showCenter  )
168 98c04ab8 leszek
        {
169 0ab55f0c Leszek Koltunski
        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 98c04ab8 leszek
        }
182 950511ed Leszek Koltunski
      }
183
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
186 334c13fa Leszek Koltunski
    void setCenter(float x, float y, float z)
187 950511ed Leszek Koltunski
      {
188 334c13fa Leszek Koltunski
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
189 08f92d82 Leszek Koltunski
      }
190
191 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
192
193 4f8ca8ff Leszek Koltunski
    void setRegion(float x, float y, float r)
194 6f779cd4 Leszek Koltunski
      {
195 f6d884d5 Leszek Koltunski
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
196 fec27f16 Leszek Koltunski
      mRegionPoint.set(mFactorObj*x,-mFactorObj*y,0);
197 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
198 6f779cd4 Leszek Koltunski
      }
199
200 bddd4b2d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
201
202
    void useOIT(boolean use)
203
      {
204
      mScreen.setOrderIndependentTransparency(use);
205
      }
206
207 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
208 56cbe1cf Leszek Koltunski
209
    public void onDrawFrame(GL10 glUnused)
210 08f92d82 Leszek Koltunski
      {
211 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
212 08f92d82 Leszek Koltunski
      }
213
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215 56cbe1cf Leszek Koltunski
216
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
217 08f92d82 Leszek Koltunski
      {
218 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
219 752c6b57 Leszek Koltunski
220 950511ed Leszek Koltunski
      float factorCen;
221 f6d884d5 Leszek Koltunski
      int centerSize = mCenterTexture.getWidth();
222
      int regionSize = mRegionTexture.getWidth();
223 6f779cd4 Leszek Koltunski
224 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
225 d40cfeb2 Leszek Koltunski
        {
226 950511ed Leszek Koltunski
        mFactorObj = (0.80f*height)/mObjHeight;
227
        factorCen  = (0.08f*height)/centerSize;
228 261fe5bd Leszek Koltunski
        }
229 d40cfeb2 Leszek Koltunski
      else
230
        {
231 950511ed Leszek Koltunski
        mFactorObj = (0.80f*width)/mObjWidth;
232
        factorCen  = (0.08f*width)/centerSize;
233 d40cfeb2 Leszek Koltunski
        }
234
235 348dbeea Leszek Koltunski
      Effects3DActivity2 act = (Effects3DActivity2)mView.getContext();
236 fec27f16 Leszek Koltunski
      mCenterPoint.set(mFactorObj*act.getCenterX(),+mFactorObj*act.getCenterY(),0);
237
      mRegionPoint.set(mFactorObj*act.getRegionX(),-mFactorObj*act.getRegionY(),0);
238 17600407 Leszek Koltunski
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
239 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
240 06366d12 Leszek Koltunski
      mMoveObject.set( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0 );
241
      mRotateCen.set(width/2,height/2, 0);
242 fec27f16 Leszek Koltunski
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
243 06366d12 Leszek Koltunski
      mMoveCenter.set( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
244
                       (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 15 );
245
      mScaleCenter.set(factorCen,factorCen,factorCen);
246
      mMoveRegion.set( (width -mFactorObj*mObjWidth )/2 ,(height-mFactorObj*mObjHeight)/2 , 12 );
247 fce25d04 leszek
248
      int backgroundSize = mBackgroundTexture.getWidth();
249
      float factorBackX = ((float)width)/backgroundSize;
250
      float factorBackY = ((float)height)/backgroundSize;
251
252
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
253 06366d12 Leszek Koltunski
      mMoveBackground.set( -width/2, -height/2, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
254
      mScaleBackground.set( 2*factorBackX, 2*factorBackY, 1.0f );
255 fce25d04 leszek
256 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
257 08f92d82 Leszek Koltunski
      }
258
259
///////////////////////////////////////////////////////////////////////////////////////////////////
260 56cbe1cf Leszek Koltunski
261
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
262 08f92d82 Leszek Koltunski
      {
263 348dbeea Leszek Koltunski
      Effects3DActivity2 act = (Effects3DActivity2)mView.getContext();
264 bcc8e016 Leszek Koltunski
265 f6d884d5 Leszek Koltunski
      InputStream isB = act.getResources().openRawResource(R.raw.water);
266
      InputStream isC = act.getResources().openRawResource(R.raw.center);
267
      InputStream isR = act.getResources().openRawResource(R.raw.region);
268 71d8ad03 Leszek Koltunski
269 f6d884d5 Leszek Koltunski
      Bitmap bitmapB,bitmapC,bitmapR;
270 08f92d82 Leszek Koltunski
        
271
      try 
272
        {
273 f6d884d5 Leszek Koltunski
        bitmapB = BitmapFactory.decodeStream(isB);
274
        bitmapC = BitmapFactory.decodeStream(isC);
275
        bitmapR = BitmapFactory.decodeStream(isR);
276 71d8ad03 Leszek Koltunski
        }
277 08f92d82 Leszek Koltunski
      finally 
278
        {
279
        try 
280
          {
281 f6d884d5 Leszek Koltunski
          isB.close();
282
          isC.close();
283
          isR.close();
284 9167cfd4 Leszek Koltunski
          }
285 08f92d82 Leszek Koltunski
        catch(IOException e) { }
286
        }  
287
      
288 f6d884d5 Leszek Koltunski
      mObjectTexture.setTexture( act.getBitmap() );
289
      mBackgroundTexture.setTexture(bitmapB);
290
      mCenterTexture.setTexture(bitmapC);
291
      mRegionTexture.setTexture(bitmapR);
292 950511ed Leszek Koltunski
293 885b9cca leszek
      VertexEffectDeform.enable();
294
      VertexEffectDistort.enable();
295
      VertexEffectPinch.enable();
296
      VertexEffectSink.enable();
297
      VertexEffectSwirl.enable();
298
      VertexEffectWave.enable();
299
300
      FragmentEffectAlpha.enable();
301
      FragmentEffectBrightness.enable();
302
      FragmentEffectChroma.enable();
303
      FragmentEffectContrast.enable();
304
      FragmentEffectSaturation.enable();
305 6637d0f2 Leszek Koltunski
306 cdcbdbe3 Leszek Koltunski
      PostprocessEffectBlur.enable();
307
      PostprocessEffectGlow.enable();
308
309 08f92d82 Leszek Koltunski
      try
310
        {
311 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
312 08f92d82 Leszek Koltunski
        }
313
      catch(Exception ex)
314
        {
315 76a81b6a Leszek Koltunski
        android.util.Log.e("Effects3D", ex.getMessage() );
316 08f92d82 Leszek Koltunski
        }
317
      }
318 56cbe1cf Leszek Koltunski
}