Project

General

Profile

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

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

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