Project

General

Profile

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

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

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 b88ec561 Leszek Koltunski
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.EffectType;
29 06366d12 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectQuaternion;
31
import org.distorted.library.effect.MatrixEffectScale;
32 01782e85 Leszek Koltunski
import org.distorted.library.main.Distorted;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedNode;
35
import org.distorted.library.main.DistortedScreen;
36
import org.distorted.library.main.DistortedTexture;
37 57d7fdba Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
38 107e4b72 Leszek Koltunski
import org.distorted.library.mesh.MeshFlat;
39 e83350b4 Leszek Koltunski
import org.distorted.library.mesh.MeshSphere;
40 08f92d82 Leszek Koltunski
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42
43
import java.io.IOException;
44
import java.io.InputStream;
45
46
import javax.microedition.khronos.egl.EGLConfig;
47
import javax.microedition.khronos.opengles.GL10;
48
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50
51 76a81b6a Leszek Koltunski
class Effects3DRenderer implements GLSurfaceView.Renderer
52 08f92d82 Leszek Koltunski
{
53 f86c9fb5 Leszek Koltunski
    private static final float FOV = 30.0f;
54 af662543 leszek
    private static final float NEAR = 0.1f;
55
56 08f92d82 Leszek Koltunski
    private GLSurfaceView mView;
57 f6d884d5 Leszek Koltunski
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
58 d218d64e leszek
    private DistortedScreen mScreen;
59 fe59d375 Leszek Koltunski
    private DistortedNode mCenterNode, mRegionNode;
60 b424b062 Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
61 06366d12 Leszek Koltunski
    private Static3D mCenterPoint, mRegionPoint, mRegionScalePoint;
62 fec27f16 Leszek Koltunski
    private Static3D mRotateCen, mMoveObject, mScaleObject, mMoveCenter, mScaleCenter, mMoveRegion, mMoveBackground, mScaleBackground;
63
    private boolean mShowingCenter=false;
64
    private boolean mShowingRegion=false;
65 6f779cd4 Leszek Koltunski
    private float mFactorObj, mFactorReg;
66 65f622c1 Leszek Koltunski
    private int mWidth;
67 e83350b4 Leszek Koltunski
    private float mFactor;
68 950511ed Leszek Koltunski
69 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
70 d2337a3a Leszek Koltunski
    int mScreenMin;
71 833685d0 Leszek Koltunski
72 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
73
74 4f8ca8ff Leszek Koltunski
    Effects3DRenderer(GLSurfaceView v)
75 08f92d82 Leszek Koltunski
      {
76
      mView = v;
77 d40cfeb2 Leszek Koltunski
78 348dbeea Leszek Koltunski
      Effects3DActivity2 act = (Effects3DActivity2)v.getContext();
79 e8b6aa95 Leszek Koltunski
80 06366d12 Leszek Koltunski
      mRotateCen      = new Static3D(0,0,0);
81
      mMoveObject     = new Static3D(0,0,0);
82 fec27f16 Leszek Koltunski
      mScaleObject    = new Static3D(1,1,1);
83 06366d12 Leszek Koltunski
      mMoveCenter     = new Static3D(0,0,0);
84
      mScaleCenter    = new Static3D(1,1,1);
85
      mMoveRegion     = new Static3D(0,0,0);
86
      mMoveBackground = new Static3D(0,0,0);
87
      mScaleBackground= new Static3D(1,1,1);
88
89 f6d884d5 Leszek Koltunski
      mObjectTexture     = act.getTexture();
90 7451c98a Leszek Koltunski
      mBackgroundTexture = new DistortedTexture(100,100);
91
      mCenterTexture     = new DistortedTexture(100,100);
92
      mRegionTexture     = new DistortedTexture(100,100);
93 06366d12 Leszek Koltunski
94
      DistortedEffects objectEffects     = act.getEffects();
95
      DistortedEffects backgroundEffects = new DistortedEffects();
96
      DistortedEffects centerEffects     = new DistortedEffects();
97
      DistortedEffects regionEffects     = new DistortedEffects();
98 f6d884d5 Leszek Koltunski
99 57d7fdba Leszek Koltunski
      MeshBase mesh    = act.getMesh();
100 24624a1a Leszek Koltunski
      MeshFlat quad    = new MeshFlat(1,1);
101 aac5c562 leszek
102 e83350b4 Leszek Koltunski
      mFactor = mesh instanceof MeshSphere ? 1.0f : 0.7f;
103
104 f6d884d5 Leszek Koltunski
      mObjWidth = mObjectTexture.getWidth();
105
      mObjHeight= mObjectTexture.getHeight();
106 b424b062 Leszek Koltunski
      mObjDepth = mObjectTexture.getDepth(mesh);
107 d40cfeb2 Leszek Koltunski
108 833685d0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
109
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
110
111 24991bc2 Leszek Koltunski
      mCenterPoint= new Static3D(0,0,0);
112
      mRegionPoint= new Static3D(0,0,0);
113 6f779cd4 Leszek Koltunski
      mRegionScalePoint = new Static3D(0,0,0);
114 392e16fd Leszek Koltunski
115 06366d12 Leszek Koltunski
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
116
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
117 fe59d375 Leszek Koltunski
118 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
119 af662543 leszek
      mScreen.setProjection(FOV, NEAR);
120 06366d12 Leszek Koltunski
      mScreen.attach(mBackgroundTexture, backgroundEffects, quad );
121 65f622c1 Leszek Koltunski
      mScreen.attach(mObjectTexture    , objectEffects    , mesh );
122 06366d12 Leszek Koltunski
123
      int regionSize = mRegionTexture.getWidth();
124
      mRotateCen = new Static3D(0 ,0, 0);
125
126
      MatrixEffectQuaternion quat1cen = new MatrixEffectQuaternion(mQuat1, mRotateCen);
127
      MatrixEffectQuaternion quat2cen = new MatrixEffectQuaternion(mQuat2, mRotateCen);
128
      MatrixEffectMove centerMove = new MatrixEffectMove(mCenterPoint);
129
130
      centerEffects.apply(quat1cen);
131
      centerEffects.apply(quat2cen);
132
      centerEffects.apply( new MatrixEffectMove(mMoveCenter) );
133
      centerEffects.apply( centerMove );
134
      centerEffects.apply( new MatrixEffectScale(mScaleCenter) );
135
136
      regionEffects.apply(quat1cen);
137
      regionEffects.apply(quat2cen);
138
      regionEffects.apply( new MatrixEffectMove(mMoveRegion) );
139
      regionEffects.apply( centerMove );
140
      regionEffects.apply( new MatrixEffectMove(mRegionPoint) );
141
      regionEffects.apply( new MatrixEffectScale(mRegionScalePoint) );
142
      regionEffects.apply( new MatrixEffectMove(new Static3D( -regionSize/2 , -regionSize/2 , 0)) );
143
144 65f622c1 Leszek Koltunski
      resetMatrixEffects();
145
146 06366d12 Leszek Koltunski
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
147
      backgroundEffects.apply(new MatrixEffectMove(mMoveBackground) );
148
      backgroundEffects.apply(new MatrixEffectScale(mScaleBackground) );
149 f338550a leszek
      }
150
151 65f622c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
152
153
    void resetMatrixEffects()
154
      {
155
      Effects3DActivity2 act = (Effects3DActivity2)mView.getContext();
156
      DistortedEffects objectEffects= act.getEffects();
157 b424b062 Leszek Koltunski
      Static3D rotateObj = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, (float)mObjDepth/2 );
158 65f622c1 Leszek Koltunski
159
      MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1,  rotateObj);
160
      MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2,  rotateObj);
161
162
      objectEffects.apply( new MatrixEffectMove(mMoveObject));
163
      objectEffects.apply( new MatrixEffectScale(mScaleObject) );
164
      objectEffects.apply(quat1obj);
165
      objectEffects.apply(quat2obj);
166
167
      mQuat1.set(0,0,0,1);
168
      mQuat2.set(0,0,0,1);
169
      }
170
171 f338550a leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
172
173 0ab55f0c Leszek Koltunski
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
174 f338550a leszek
      {
175 0ab55f0c Leszek Koltunski
      if( mShowingCenter!=showCenter  )
176 98c04ab8 leszek
        {
177 0ab55f0c Leszek Koltunski
        if( showCenter ) mScreen.attach(mCenterNode);
178
        else             mScreen.detach(mCenterNode);
179
180
        mShowingCenter = showCenter;
181
        }
182
183
      if( mShowingRegion!=showRegion  )
184
        {
185
        if( showRegion ) mScreen.attach(mRegionNode);
186
        else             mScreen.detach(mRegionNode);
187
188
        mShowingRegion = showRegion;
189 98c04ab8 leszek
        }
190 950511ed Leszek Koltunski
      }
191
192 65f622c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
193
194
    public int getWidth()
195
      {
196
      return mWidth;
197
      }
198
199 950511ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201 334c13fa Leszek Koltunski
    void setCenter(float x, float y, float z)
202 950511ed Leszek Koltunski
      {
203 334c13fa Leszek Koltunski
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
204 08f92d82 Leszek Koltunski
      }
205
206 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208 9e7b6dbd Leszek Koltunski
    void setRegion(float x, float y, float z, float r)
209 6f779cd4 Leszek Koltunski
      {
210 f6d884d5 Leszek Koltunski
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
211 9e7b6dbd Leszek Koltunski
      mRegionPoint.set(mFactorObj*x,-mFactorObj*y, mFactorObj*z);
212 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
213 6f779cd4 Leszek Koltunski
      }
214
215 bddd4b2d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217
    void useOIT(boolean use)
218
      {
219
      mScreen.setOrderIndependentTransparency(use);
220
      }
221
222 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
223 56cbe1cf Leszek Koltunski
224
    public void onDrawFrame(GL10 glUnused)
225 08f92d82 Leszek Koltunski
      {
226 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
227 08f92d82 Leszek Koltunski
      }
228
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230 56cbe1cf Leszek Koltunski
231
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
232 08f92d82 Leszek Koltunski
      {
233 65f622c1 Leszek Koltunski
      mWidth = width;
234 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
235 752c6b57 Leszek Koltunski
236 950511ed Leszek Koltunski
      float factorCen;
237 f6d884d5 Leszek Koltunski
      int centerSize = mCenterTexture.getWidth();
238
      int regionSize = mRegionTexture.getWidth();
239 6f779cd4 Leszek Koltunski
240 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
241 d40cfeb2 Leszek Koltunski
        {
242 e83350b4 Leszek Koltunski
        mFactorObj = (mFactor*height)/mObjHeight;
243
        factorCen  = (0.08f  *height)/centerSize;
244 261fe5bd Leszek Koltunski
        }
245 d40cfeb2 Leszek Koltunski
      else
246
        {
247 e83350b4 Leszek Koltunski
        mFactorObj = (mFactor*width)/mObjWidth;
248
        factorCen  = (0.08f  *width)/centerSize;
249 d40cfeb2 Leszek Koltunski
        }
250
251 348dbeea Leszek Koltunski
      Effects3DActivity2 act = (Effects3DActivity2)mView.getContext();
252 fec27f16 Leszek Koltunski
      mCenterPoint.set(mFactorObj*act.getCenterX(),+mFactorObj*act.getCenterY(),0);
253
      mRegionPoint.set(mFactorObj*act.getRegionX(),-mFactorObj*act.getRegionY(),0);
254 17600407 Leszek Koltunski
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
255 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
256 06366d12 Leszek Koltunski
      mMoveObject.set( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0 );
257
      mRotateCen.set(width/2,height/2, 0);
258 fec27f16 Leszek Koltunski
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
259 06366d12 Leszek Koltunski
      mMoveCenter.set( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
260
                       (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 15 );
261
      mScaleCenter.set(factorCen,factorCen,factorCen);
262
      mMoveRegion.set( (width -mFactorObj*mObjWidth )/2 ,(height-mFactorObj*mObjHeight)/2 , 12 );
263 fce25d04 leszek
264
      int backgroundSize = mBackgroundTexture.getWidth();
265
      float factorBackX = ((float)width)/backgroundSize;
266
      float factorBackY = ((float)height)/backgroundSize;
267
268
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
269 06366d12 Leszek Koltunski
      mMoveBackground.set( -width/2, -height/2, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
270
      mScaleBackground.set( 2*factorBackX, 2*factorBackY, 1.0f );
271 fce25d04 leszek
272 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
273 08f92d82 Leszek Koltunski
      }
274
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276 56cbe1cf Leszek Koltunski
277
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
278 08f92d82 Leszek Koltunski
      {
279 348dbeea Leszek Koltunski
      Effects3DActivity2 act = (Effects3DActivity2)mView.getContext();
280 bcc8e016 Leszek Koltunski
281 f6d884d5 Leszek Koltunski
      InputStream isB = act.getResources().openRawResource(R.raw.water);
282
      InputStream isC = act.getResources().openRawResource(R.raw.center);
283
      InputStream isR = act.getResources().openRawResource(R.raw.region);
284 71d8ad03 Leszek Koltunski
285 f6d884d5 Leszek Koltunski
      Bitmap bitmapB,bitmapC,bitmapR;
286 08f92d82 Leszek Koltunski
        
287
      try 
288
        {
289 f6d884d5 Leszek Koltunski
        bitmapB = BitmapFactory.decodeStream(isB);
290
        bitmapC = BitmapFactory.decodeStream(isC);
291
        bitmapR = BitmapFactory.decodeStream(isR);
292 71d8ad03 Leszek Koltunski
        }
293 08f92d82 Leszek Koltunski
      finally 
294
        {
295
        try 
296
          {
297 f6d884d5 Leszek Koltunski
          isB.close();
298
          isC.close();
299
          isR.close();
300 9167cfd4 Leszek Koltunski
          }
301 08f92d82 Leszek Koltunski
        catch(IOException e) { }
302
        }  
303
      
304 f6d884d5 Leszek Koltunski
      mObjectTexture.setTexture( act.getBitmap() );
305
      mBackgroundTexture.setTexture(bitmapB);
306
      mCenterTexture.setTexture(bitmapC);
307
      mRegionTexture.setTexture(bitmapR);
308 950511ed Leszek Koltunski
309 b88ec561 Leszek Koltunski
      Effect.enableEffects(EffectType.VERTEX);
310
      Effect.enableEffects(EffectType.FRAGMENT);
311
      Effect.enableEffects(EffectType.POSTPROCESS);
312 cdcbdbe3 Leszek Koltunski
313 08f92d82 Leszek Koltunski
      try
314
        {
315 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
316 08f92d82 Leszek Koltunski
        }
317
      catch(Exception ex)
318
        {
319 76a81b6a Leszek Koltunski
        android.util.Log.e("Effects3D", ex.getMessage() );
320 08f92d82 Leszek Koltunski
        }
321
      }
322 56cbe1cf Leszek Koltunski
}