Project

General

Profile

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

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

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