Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 4dd7ca16

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 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
199 56cbe1cf Leszek Koltunski
200
    public void onDrawFrame(GL10 glUnused)
201 08f92d82 Leszek Koltunski
      {
202 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
203 08f92d82 Leszek Koltunski
      }
204
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206 56cbe1cf Leszek Koltunski
207
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
208 08f92d82 Leszek Koltunski
      {
209 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
210 752c6b57 Leszek Koltunski
211 950511ed Leszek Koltunski
      float factorCen;
212 f6d884d5 Leszek Koltunski
      int centerSize = mCenterTexture.getWidth();
213
      int regionSize = mRegionTexture.getWidth();
214 6f779cd4 Leszek Koltunski
215 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
216 d40cfeb2 Leszek Koltunski
        {
217 950511ed Leszek Koltunski
        mFactorObj = (0.80f*height)/mObjHeight;
218
        factorCen  = (0.08f*height)/centerSize;
219 261fe5bd Leszek Koltunski
        }
220 d40cfeb2 Leszek Koltunski
      else
221
        {
222 950511ed Leszek Koltunski
        mFactorObj = (0.80f*width)/mObjWidth;
223
        factorCen  = (0.08f*width)/centerSize;
224 d40cfeb2 Leszek Koltunski
        }
225
226 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
227 fec27f16 Leszek Koltunski
      mCenterPoint.set(mFactorObj*act.getCenterX(),+mFactorObj*act.getCenterY(),0);
228
      mRegionPoint.set(mFactorObj*act.getRegionX(),-mFactorObj*act.getRegionY(),0);
229 17600407 Leszek Koltunski
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
230 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
231 06366d12 Leszek Koltunski
      mMoveObject.set( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0 );
232
      mRotateCen.set(width/2,height/2, 0);
233 fec27f16 Leszek Koltunski
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
234 06366d12 Leszek Koltunski
      mMoveCenter.set( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
235
                       (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 15 );
236
      mScaleCenter.set(factorCen,factorCen,factorCen);
237
      mMoveRegion.set( (width -mFactorObj*mObjWidth )/2 ,(height-mFactorObj*mObjHeight)/2 , 12 );
238 fce25d04 leszek
239
      int backgroundSize = mBackgroundTexture.getWidth();
240
      float factorBackX = ((float)width)/backgroundSize;
241
      float factorBackY = ((float)height)/backgroundSize;
242
243
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
244 06366d12 Leszek Koltunski
      mMoveBackground.set( -width/2, -height/2, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
245
      mScaleBackground.set( 2*factorBackX, 2*factorBackY, 1.0f );
246 fce25d04 leszek
247 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
248 08f92d82 Leszek Koltunski
      }
249
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251 56cbe1cf Leszek Koltunski
252
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
253 08f92d82 Leszek Koltunski
      {
254 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
255 bcc8e016 Leszek Koltunski
256 f6d884d5 Leszek Koltunski
      InputStream isB = act.getResources().openRawResource(R.raw.water);
257
      InputStream isC = act.getResources().openRawResource(R.raw.center);
258
      InputStream isR = act.getResources().openRawResource(R.raw.region);
259 71d8ad03 Leszek Koltunski
260 f6d884d5 Leszek Koltunski
      Bitmap bitmapB,bitmapC,bitmapR;
261 08f92d82 Leszek Koltunski
        
262
      try 
263
        {
264 f6d884d5 Leszek Koltunski
        bitmapB = BitmapFactory.decodeStream(isB);
265
        bitmapC = BitmapFactory.decodeStream(isC);
266
        bitmapR = BitmapFactory.decodeStream(isR);
267 71d8ad03 Leszek Koltunski
        }
268 08f92d82 Leszek Koltunski
      finally 
269
        {
270
        try 
271
          {
272 f6d884d5 Leszek Koltunski
          isB.close();
273
          isC.close();
274
          isR.close();
275 9167cfd4 Leszek Koltunski
          }
276 08f92d82 Leszek Koltunski
        catch(IOException e) { }
277
        }  
278
      
279 f6d884d5 Leszek Koltunski
      mObjectTexture.setTexture( act.getBitmap() );
280
      mBackgroundTexture.setTexture(bitmapB);
281
      mCenterTexture.setTexture(bitmapC);
282
      mRegionTexture.setTexture(bitmapR);
283 950511ed Leszek Koltunski
284 885b9cca leszek
      VertexEffectDeform.enable();
285
      VertexEffectDistort.enable();
286
      VertexEffectPinch.enable();
287
      VertexEffectSink.enable();
288
      VertexEffectSwirl.enable();
289
      VertexEffectWave.enable();
290
291
      FragmentEffectAlpha.enable();
292
      FragmentEffectBrightness.enable();
293
      FragmentEffectChroma.enable();
294
      FragmentEffectContrast.enable();
295
      FragmentEffectSaturation.enable();
296 6637d0f2 Leszek Koltunski
297 08f92d82 Leszek Koltunski
      try
298
        {
299 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
300 08f92d82 Leszek Koltunski
        }
301
      catch(Exception ex)
302
        {
303 76a81b6a Leszek Koltunski
        android.util.Log.e("Effects3D", ex.getMessage() );
304 08f92d82 Leszek Koltunski
        }
305
      }
306 56cbe1cf Leszek Koltunski
}