Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / Effects3DRenderer.java @ e52bd0c8

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.examples.generic;
21

    
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25

    
26
import org.distorted.examples.R;
27
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.EffectType;
29
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectQuaternion;
31
import org.distorted.library.effect.MatrixEffectScale;
32
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
import org.distorted.library.mesh.MeshBase;
38
import org.distorted.library.mesh.MeshFlat;
39
import org.distorted.library.mesh.MeshSphere;
40
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
class Effects3DRenderer implements GLSurfaceView.Renderer
52
{
53
    private static final float FOV = 30.0f;
54
    private static final float NEAR = 0.1f;
55

    
56
    private GLSurfaceView mView;
57
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
58
    private DistortedScreen mScreen;
59
    private DistortedNode mCenterNode, mRegionNode;
60
    private int mObjWidth, mObjHeight;
61
    private Static3D mCenterPoint, mRegionPoint, mRegionScalePoint;
62
    private Static3D mRotateCen, mMoveObject, mScaleObject, mMoveCenter, mScaleCenter, mMoveRegion, mMoveBackground, mScaleBackground;
63
    private boolean mShowingCenter=false;
64
    private boolean mShowingRegion=false;
65
    private float mFactorObj, mFactorReg;
66
    private int mWidth;
67
    private float mFactor;
68

    
69
    Static4D mQuat1, mQuat2;
70
    int mScreenMin;
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
    Effects3DRenderer(GLSurfaceView v)
75
      {
76
      mView = v;
77

    
78
      Effects3DActivity2 act = (Effects3DActivity2)v.getContext();
79

    
80
      mRotateCen      = new Static3D(0,0,0);
81
      mMoveObject     = new Static3D(0,0,0);
82
      mScaleObject    = new Static3D(1,1,1);
83
      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
      mObjectTexture     = act.getTexture();
90
      mBackgroundTexture = new DistortedTexture(100,100);
91
      mCenterTexture     = new DistortedTexture(100,100);
92
      mRegionTexture     = new DistortedTexture(100,100);
93

    
94
      DistortedEffects objectEffects     = act.getEffects();
95
      DistortedEffects backgroundEffects = new DistortedEffects();
96
      DistortedEffects centerEffects     = new DistortedEffects();
97
      DistortedEffects regionEffects     = new DistortedEffects();
98

    
99
      MeshBase mesh    = act.getMesh();
100
      MeshFlat quad    = new MeshFlat(1,1);
101

    
102
      mFactor = mesh instanceof MeshSphere ? 1.0f : 0.7f;
103

    
104
      mObjWidth = mObjectTexture.getWidth();
105
      mObjHeight= mObjectTexture.getHeight();
106

    
107
      mQuat1 = new Static4D(0,0,0,1);  // unity
108
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
109

    
110
      mCenterPoint= new Static3D(0,0,0);
111
      mRegionPoint= new Static3D(0,0,0);
112
      mRegionScalePoint = new Static3D(0,0,0);
113

    
114
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
115
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
116

    
117
      mScreen = new DistortedScreen();
118
      mScreen.setProjection(FOV, NEAR);
119
      mScreen.attach(mBackgroundTexture, backgroundEffects, quad );
120
      mScreen.attach(mObjectTexture    , objectEffects    , mesh );
121

    
122
      int regionSize = mRegionTexture.getWidth();
123
      mRotateCen = new Static3D(0 ,0, 0);
124

    
125
      MatrixEffectQuaternion quat1cen = new MatrixEffectQuaternion(mQuat1, mRotateCen);
126
      MatrixEffectQuaternion quat2cen = new MatrixEffectQuaternion(mQuat2, mRotateCen);
127
      MatrixEffectMove centerMove = new MatrixEffectMove(mCenterPoint);
128

    
129
      centerEffects.apply(quat1cen);
130
      centerEffects.apply(quat2cen);
131
      centerEffects.apply( new MatrixEffectMove(mMoveCenter) );
132
      centerEffects.apply( centerMove );
133
      centerEffects.apply( new MatrixEffectScale(mScaleCenter) );
134

    
135
      regionEffects.apply(quat1cen);
136
      regionEffects.apply(quat2cen);
137
      regionEffects.apply( new MatrixEffectMove(mMoveRegion) );
138
      regionEffects.apply( centerMove );
139
      regionEffects.apply( new MatrixEffectMove(mRegionPoint) );
140
      regionEffects.apply( new MatrixEffectScale(mRegionScalePoint) );
141
      regionEffects.apply( new MatrixEffectMove(new Static3D( -regionSize/2 , -regionSize/2 , 0)) );
142

    
143
      resetMatrixEffects();
144

    
145
      backgroundEffects.apply(new MatrixEffectMove(mMoveBackground) );
146
      backgroundEffects.apply(new MatrixEffectScale(mScaleBackground) );
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
    void resetMatrixEffects()
152
      {
153
      Effects3DActivity2 act = (Effects3DActivity2)mView.getContext();
154
      DistortedEffects objectEffects= act.getEffects();
155
      Static3D rotateObj = new Static3D( 0,0,0 );
156

    
157
      MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1,  rotateObj);
158
      MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2,  rotateObj);
159

    
160
      objectEffects.apply( new MatrixEffectMove(mMoveObject));
161
      objectEffects.apply( new MatrixEffectScale(mScaleObject) );
162
      objectEffects.apply(quat1obj);
163
      objectEffects.apply(quat2obj);
164

    
165
      mQuat1.set(0,0,0,1);
166
      mQuat2.set(0,0,0,1);
167
      }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
172
      {
173
      if( mShowingCenter!=showCenter  )
174
        {
175
        if( showCenter ) mScreen.attach(mCenterNode);
176
        else             mScreen.detach(mCenterNode);
177

    
178
        mShowingCenter = showCenter;
179
        }
180

    
181
      if( mShowingRegion!=showRegion  )
182
        {
183
        if( showRegion ) mScreen.attach(mRegionNode);
184
        else             mScreen.detach(mRegionNode);
185

    
186
        mShowingRegion = showRegion;
187
        }
188
      }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
    public int getWidth()
193
      {
194
      return mWidth;
195
      }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
    void setCenter(float x, float y, float z)
200
      {
201
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
202
      }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
    void setRegion(float x, float y, float z, float r)
207
      {
208
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
209
      mRegionPoint.set(mFactorObj*x,-mFactorObj*y, mFactorObj*z);
210
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
211
      }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

    
215
    void useOIT(boolean use)
216
      {
217
      mScreen.setOrderIndependentTransparency(use);
218
      }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
    public void onDrawFrame(GL10 glUnused)
223
      {
224
      mScreen.render(System.currentTimeMillis());
225
      }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
230
      {
231
      mWidth = width;
232
      mScreenMin = width<height ? width:height;
233

    
234
      float factorCen;
235
      int centerSize = mCenterTexture.getWidth();
236
      int regionSize = mRegionTexture.getWidth();
237

    
238
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
239
        {
240
        mFactorObj = (mFactor*height)/mObjHeight;
241
        factorCen  = (0.08f  *height)/centerSize;
242
        }
243
      else
244
        {
245
        mFactorObj = (mFactor*width)/mObjWidth;
246
        factorCen  = (0.08f  *width)/centerSize;
247
        }
248

    
249
      Effects3DActivity2 act = (Effects3DActivity2)mView.getContext();
250
      mCenterPoint.set(mFactorObj*act.getCenterX(),+mFactorObj*act.getCenterY(),0);
251
      mRegionPoint.set(mFactorObj*act.getRegionX(),-mFactorObj*act.getRegionY(),0);
252
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
253
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
254
      mRotateCen.set(width/2,height/2, 0);
255
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
256
      mMoveCenter.set( 0,0,15);//(width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
257
                    //   (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 15 );
258
      mScaleCenter.set(factorCen,factorCen,factorCen);
259
      mMoveRegion.set( (width -mFactorObj*mObjWidth )/2 ,(height-mFactorObj*mObjHeight)/2 , 12 );
260

    
261
      int backgroundSize = mBackgroundTexture.getWidth();
262
      float factorBackX = ((float)width)/backgroundSize;
263
      float factorBackY = ((float)height)/backgroundSize;
264

    
265
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
266
      mMoveBackground.set( 0, 0, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
267
      mScaleBackground.set( 2*factorBackX, 2*factorBackY, 1.0f );
268

    
269
      mScreen.resize(width, height);
270
      }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
275
      {
276
      Effects3DActivity2 act = (Effects3DActivity2)mView.getContext();
277

    
278
      InputStream isB = act.getResources().openRawResource(R.raw.water);
279
      InputStream isC = act.getResources().openRawResource(R.raw.center);
280
      InputStream isR = act.getResources().openRawResource(R.raw.region);
281

    
282
      Bitmap bitmapB,bitmapC,bitmapR;
283
        
284
      try 
285
        {
286
        bitmapB = BitmapFactory.decodeStream(isB);
287
        bitmapC = BitmapFactory.decodeStream(isC);
288
        bitmapR = BitmapFactory.decodeStream(isR);
289
        }
290
      finally 
291
        {
292
        try 
293
          {
294
          isB.close();
295
          isC.close();
296
          isR.close();
297
          }
298
        catch(IOException e) { }
299
        }  
300
      
301
      mObjectTexture.setTexture( act.getBitmap() );
302
      mBackgroundTexture.setTexture(bitmapB);
303
      mCenterTexture.setTexture(bitmapC);
304
      mRegionTexture.setTexture(bitmapR);
305

    
306
      DistortedEffects.setMax(EffectType.VERTEX  ,10);
307
      DistortedEffects.setMax(EffectType.FRAGMENT,10);
308

    
309
      Effect.enableEffects(EffectType.VERTEX);
310
      Effect.enableEffects(EffectType.FRAGMENT);
311
      Effect.enableEffects(EffectType.POSTPROCESS);
312

    
313
      try
314
        {
315
        Distorted.onCreate(mView.getContext());
316
        }
317
      catch(Exception ex)
318
        {
319
        android.util.Log.e("Effects3D", ex.getMessage() );
320
        }
321
      }
322
}
(4-4/7)