Project

General

Profile

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

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

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 885b9cca leszek
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 01782e85 Leszek Koltunski
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 08f92d82 Leszek Koltunski
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 76a81b6a Leszek Koltunski
class Effects3DRenderer implements GLSurfaceView.Renderer
60 08f92d82 Leszek Koltunski
{
61 af662543 leszek
    private static final float FOV = 70.0f;
62
    private static final float NEAR = 0.1f;
63
64 08f92d82 Leszek Koltunski
    private GLSurfaceView mView;
65 f6d884d5 Leszek Koltunski
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
66 d218d64e leszek
    private DistortedScreen mScreen;
67 fe59d375 Leszek Koltunski
    private DistortedNode mCenterNode, mRegionNode;
68 06366d12 Leszek Koltunski
    private int mObjWidth, mObjHeight;
69
    private Static3D mCenterPoint, mRegionPoint, mRegionScalePoint;
70 fec27f16 Leszek Koltunski
    private Static3D mRotateCen, mMoveObject, mScaleObject, mMoveCenter, mScaleCenter, mMoveRegion, mMoveBackground, mScaleBackground;
71
    private boolean mShowingCenter=false;
72
    private boolean mShowingRegion=false;
73 6f779cd4 Leszek Koltunski
    private float mFactorObj, mFactorReg;
74 950511ed Leszek Koltunski
75 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
76 d2337a3a Leszek Koltunski
    int mScreenMin;
77 833685d0 Leszek Koltunski
78 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
79
80 4f8ca8ff Leszek Koltunski
    Effects3DRenderer(GLSurfaceView v)
81 08f92d82 Leszek Koltunski
      {
82
      mView = v;
83 d40cfeb2 Leszek Koltunski
84 e8b6aa95 Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)v.getContext();
85
86 06366d12 Leszek Koltunski
      mRotateCen      = new Static3D(0,0,0);
87
      mMoveObject     = new Static3D(0,0,0);
88 fec27f16 Leszek Koltunski
      mScaleObject    = new Static3D(1,1,1);
89 06366d12 Leszek Koltunski
      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 f6d884d5 Leszek Koltunski
      mObjectTexture     = act.getTexture();
96 7451c98a Leszek Koltunski
      mBackgroundTexture = new DistortedTexture(100,100);
97
      mCenterTexture     = new DistortedTexture(100,100);
98
      mRegionTexture     = new DistortedTexture(100,100);
99 06366d12 Leszek Koltunski
100
      DistortedEffects objectEffects     = act.getEffects();
101
      DistortedEffects backgroundEffects = new DistortedEffects();
102
      DistortedEffects centerEffects     = new DistortedEffects();
103
      DistortedEffects regionEffects     = new DistortedEffects();
104 f6d884d5 Leszek Koltunski
105 aac5c562 leszek
      MeshObject meshO   = act.getMesh();
106
      MeshFlat quad      = new MeshFlat(1,1);
107
108 f6d884d5 Leszek Koltunski
      mObjWidth = mObjectTexture.getWidth();
109
      mObjHeight= mObjectTexture.getHeight();
110 06366d12 Leszek Koltunski
      int objDepth = mObjectTexture.getDepth(meshO);
111 d40cfeb2 Leszek Koltunski
112 833685d0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
113
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
114
115 24991bc2 Leszek Koltunski
      mCenterPoint= new Static3D(0,0,0);
116
      mRegionPoint= new Static3D(0,0,0);
117 6f779cd4 Leszek Koltunski
      mRegionScalePoint = new Static3D(0,0,0);
118 392e16fd Leszek Koltunski
119 06366d12 Leszek Koltunski
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
120
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
121 fe59d375 Leszek Koltunski
122 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
123 af662543 leszek
      mScreen.setProjection(FOV, NEAR);
124 06366d12 Leszek Koltunski
      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 fec27f16 Leszek Koltunski
      objectEffects.apply( new MatrixEffectScale(mScaleObject) );
139 06366d12 Leszek Koltunski
      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 f338550a leszek
      }
160
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162
163 0ab55f0c Leszek Koltunski
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
164 f338550a leszek
      {
165 0ab55f0c Leszek Koltunski
      if( mShowingCenter!=showCenter  )
166 98c04ab8 leszek
        {
167 0ab55f0c Leszek Koltunski
        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 98c04ab8 leszek
        }
180 950511ed Leszek Koltunski
      }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184 334c13fa Leszek Koltunski
    void setCenter(float x, float y, float z)
185 950511ed Leszek Koltunski
      {
186 334c13fa Leszek Koltunski
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
187 08f92d82 Leszek Koltunski
      }
188
189 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
190
191 4f8ca8ff Leszek Koltunski
    void setRegion(float x, float y, float r)
192 6f779cd4 Leszek Koltunski
      {
193 f6d884d5 Leszek Koltunski
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
194 fec27f16 Leszek Koltunski
      mRegionPoint.set(mFactorObj*x,-mFactorObj*y,0);
195 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
196 6f779cd4 Leszek Koltunski
      }
197
198 bddd4b2d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
199
200
    void useOIT(boolean use)
201
      {
202
      mScreen.setOrderIndependentTransparency(use);
203
      }
204
205 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
206 56cbe1cf Leszek Koltunski
207
    public void onDrawFrame(GL10 glUnused)
208 08f92d82 Leszek Koltunski
      {
209 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
210 08f92d82 Leszek Koltunski
      }
211
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213 56cbe1cf Leszek Koltunski
214
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
215 08f92d82 Leszek Koltunski
      {
216 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
217 752c6b57 Leszek Koltunski
218 950511ed Leszek Koltunski
      float factorCen;
219 f6d884d5 Leszek Koltunski
      int centerSize = mCenterTexture.getWidth();
220
      int regionSize = mRegionTexture.getWidth();
221 6f779cd4 Leszek Koltunski
222 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
223 d40cfeb2 Leszek Koltunski
        {
224 950511ed Leszek Koltunski
        mFactorObj = (0.80f*height)/mObjHeight;
225
        factorCen  = (0.08f*height)/centerSize;
226 261fe5bd Leszek Koltunski
        }
227 d40cfeb2 Leszek Koltunski
      else
228
        {
229 950511ed Leszek Koltunski
        mFactorObj = (0.80f*width)/mObjWidth;
230
        factorCen  = (0.08f*width)/centerSize;
231 d40cfeb2 Leszek Koltunski
        }
232
233 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
234 fec27f16 Leszek Koltunski
      mCenterPoint.set(mFactorObj*act.getCenterX(),+mFactorObj*act.getCenterY(),0);
235
      mRegionPoint.set(mFactorObj*act.getRegionX(),-mFactorObj*act.getRegionY(),0);
236 17600407 Leszek Koltunski
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
237 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
238 06366d12 Leszek Koltunski
      mMoveObject.set( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0 );
239
      mRotateCen.set(width/2,height/2, 0);
240 fec27f16 Leszek Koltunski
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
241 06366d12 Leszek Koltunski
      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 fce25d04 leszek
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 06366d12 Leszek Koltunski
      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 fce25d04 leszek
254 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
255 08f92d82 Leszek Koltunski
      }
256
257
///////////////////////////////////////////////////////////////////////////////////////////////////
258 56cbe1cf Leszek Koltunski
259
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
260 08f92d82 Leszek Koltunski
      {
261 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
262 bcc8e016 Leszek Koltunski
263 f6d884d5 Leszek Koltunski
      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 71d8ad03 Leszek Koltunski
267 f6d884d5 Leszek Koltunski
      Bitmap bitmapB,bitmapC,bitmapR;
268 08f92d82 Leszek Koltunski
        
269
      try 
270
        {
271 f6d884d5 Leszek Koltunski
        bitmapB = BitmapFactory.decodeStream(isB);
272
        bitmapC = BitmapFactory.decodeStream(isC);
273
        bitmapR = BitmapFactory.decodeStream(isR);
274 71d8ad03 Leszek Koltunski
        }
275 08f92d82 Leszek Koltunski
      finally 
276
        {
277
        try 
278
          {
279 f6d884d5 Leszek Koltunski
          isB.close();
280
          isC.close();
281
          isR.close();
282 9167cfd4 Leszek Koltunski
          }
283 08f92d82 Leszek Koltunski
        catch(IOException e) { }
284
        }  
285
      
286 f6d884d5 Leszek Koltunski
      mObjectTexture.setTexture( act.getBitmap() );
287
      mBackgroundTexture.setTexture(bitmapB);
288
      mCenterTexture.setTexture(bitmapC);
289
      mRegionTexture.setTexture(bitmapR);
290 950511ed Leszek Koltunski
291 885b9cca leszek
      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 6637d0f2 Leszek Koltunski
304 08f92d82 Leszek Koltunski
      try
305
        {
306 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
307 08f92d82 Leszek Koltunski
        }
308
      catch(Exception ex)
309
        {
310 76a81b6a Leszek Koltunski
        android.util.Log.e("Effects3D", ex.getMessage() );
311 08f92d82 Leszek Koltunski
        }
312
      }
313 56cbe1cf Leszek Koltunski
}