Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericRenderer.java @ f3ca895f

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.DistortedLibrary;
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.MeshRectangles;
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 GenericRenderer 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, mObjDepth;
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;
66
    private int mWidth;
67
    private float mFactor;
68

    
69
    Static4D mQuat1, mQuat2;
70
    int mScreenMin;
71

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

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

    
78
      GenericActivity2 act = (GenericActivity2)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
      mFactorObj = 1.0f;
95

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

    
101
      MeshBase mesh    = act.getMesh();
102
      MeshRectangles quad    = new MeshRectangles(1,1);
103

    
104
      mFactor = mesh instanceof MeshSphere ? 1.0f : 0.7f;
105

    
106
      mObjWidth = mObjectTexture.getWidth();
107
      mObjHeight= mObjectTexture.getHeight();
108
      mObjDepth = mObjectTexture.getDepth(mesh);
109

    
110
      mQuat1 = new Static4D(0,0,0,1);  // unity
111
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
112

    
113
      mCenterPoint= new Static3D(0,0,0);
114
      mRegionPoint= new Static3D(0,0,0);
115
      mRegionScalePoint = new Static3D(0,0,0);
116

    
117
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
118
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
119

    
120
      mScreen = new DistortedScreen();
121
      mScreen.setProjection(FOV, NEAR);
122
      mScreen.attach(mBackgroundTexture, backgroundEffects, quad );
123
      mScreen.attach(mObjectTexture    , objectEffects    , mesh );
124

    
125
      int regionSize = mRegionTexture.getWidth();
126
      mRotateCen = new Static3D(0 ,0, 0);
127

    
128
      MatrixEffectQuaternion quat1cen = new MatrixEffectQuaternion(mQuat1, mRotateCen);
129
      MatrixEffectQuaternion quat2cen = new MatrixEffectQuaternion(mQuat2, mRotateCen);
130
      MatrixEffectMove centerMove = new MatrixEffectMove(mCenterPoint);
131

    
132
      centerEffects.apply( new MatrixEffectScale(mScaleCenter) );
133
      centerEffects.apply( centerMove );
134
      centerEffects.apply( new MatrixEffectMove(mMoveCenter) );
135
      centerEffects.apply(quat2cen);
136
      centerEffects.apply(quat1cen);
137

    
138
      regionEffects.apply( new MatrixEffectMove(new Static3D( -regionSize*0.5f , -regionSize*0.5f , 0)) );
139
      regionEffects.apply( new MatrixEffectScale(mRegionScalePoint) );
140
      regionEffects.apply( new MatrixEffectMove(mRegionPoint) );
141
      regionEffects.apply( centerMove );
142
      regionEffects.apply( new MatrixEffectMove(mMoveRegion) );
143
      regionEffects.apply(quat2cen);
144
      regionEffects.apply(quat1cen);
145

    
146
      resetMatrixEffects();
147

    
148
      // quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
149
      backgroundEffects.apply(new MatrixEffectScale(mScaleBackground) );
150
      backgroundEffects.apply(new MatrixEffectMove(mMoveBackground) );
151
      }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
    void resetMatrixEffects()
156
      {
157
      GenericActivity2 act = (GenericActivity2)mView.getContext();
158
      DistortedEffects objectEffects= act.getEffects();
159
      Static3D rotateObj = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, (float)mObjDepth/2 );
160

    
161
      MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1,  rotateObj);
162
      MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2,  rotateObj);
163

    
164
      objectEffects.apply(quat2obj);
165
      objectEffects.apply(quat1obj);
166
      objectEffects.apply( new MatrixEffectScale(mScaleObject) );
167
      objectEffects.apply( new MatrixEffectMove(mMoveObject));
168

    
169
      mQuat1.set(0,0,0,1);
170
      mQuat2.set(0,0,0,1);
171
      }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
176
      {
177
      if( mShowingCenter!=showCenter  )
178
        {
179
        if( showCenter )
180
          {
181
          if( mShowingRegion ) mScreen.detach(mRegionNode);
182
          mScreen.attach(mCenterNode);
183
          if( mShowingRegion ) mScreen.attach(mRegionNode);
184
          }
185
        else
186
          {
187
          mScreen.detach(mCenterNode);
188
          }
189

    
190
        mShowingCenter = showCenter;
191
        }
192

    
193
      if( mShowingRegion!=showRegion  )
194
        {
195
        if( showRegion ) mScreen.attach(mRegionNode);
196
        else             mScreen.detach(mRegionNode);
197

    
198
        mShowingRegion = showRegion;
199
        }
200
      }
201

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
    public int getWidth()
205
      {
206
      return mWidth;
207
      }
208

    
209
///////////////////////////////////////////////////////////////////////////////////////////////////
210

    
211
    void setCenter(float x, float y, float z)
212
      {
213
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
214
      }
215

    
216
///////////////////////////////////////////////////////////////////////////////////////////////////
217

    
218
    void setRegion(float x, float y, float z, float r)
219
      {
220
      float factorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
221
      mRegionPoint.set(mFactorObj*x,mFactorObj*y, mFactorObj*z);
222
      mRegionScalePoint.set(factorReg,factorReg,factorReg);
223
      }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
    void useOIT(boolean use)
228
      {
229
      mScreen.setOrderIndependentTransparency(use);
230
      }
231

    
232
///////////////////////////////////////////////////////////////////////////////////////////////////
233

    
234
    public void onDrawFrame(GL10 glUnused)
235
      {
236
      mScreen.render(System.currentTimeMillis());
237
      }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

    
241
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
242
      {
243
      mWidth = width;
244
      mScreenMin = width<height ? width:height;
245

    
246
      float factorCen;
247
      int centerSize = mCenterTexture.getWidth();
248

    
249
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
250
        {
251
        mFactorObj = (mFactor*height)/mObjHeight;
252
        factorCen  = (0.08f  *height)/centerSize;
253
        }
254
      else
255
        {
256
        mFactorObj = (mFactor*width)/mObjWidth;
257
        factorCen  = (0.08f  *width)/centerSize;
258
        }
259

    
260
      setCenter(0.5f*mObjWidth,0.5f*mObjHeight,0.5f*mObjDepth);
261
      setRegion(0.0f,0.0f,0.0f,0.25f*(mObjWidth+mObjHeight));
262

    
263
      mMoveObject.set( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , -mFactorObj*mObjDepth );
264
      mRotateCen.set(width*0.5f,height*0.5f, 0);
265
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
266
      mMoveCenter.set( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
267
                       (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 15 );
268
      mScaleCenter.set(factorCen,factorCen,factorCen);
269
      mMoveRegion.set( (width -mFactorObj*mObjWidth )/2 ,(height-mFactorObj*mObjHeight)/2 , 12 );
270

    
271
      int backgroundSize = mBackgroundTexture.getWidth();
272
      float factorBackX = ((float)width)/backgroundSize;
273
      float factorBackY = ((float)height)/backgroundSize;
274

    
275
      // quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
276
      mMoveBackground.set( -width*0.5f, -height*0.5f, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
277
      mScaleBackground.set( 2*factorBackX, 2*factorBackY, 1.0f );
278

    
279
      mScreen.resize(width, height);
280
      }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
285
      {
286
      GenericActivity2 act = (GenericActivity2)mView.getContext();
287

    
288
      InputStream isB = act.getResources().openRawResource(R.raw.water);
289
      InputStream isC = act.getResources().openRawResource(R.raw.center);
290
      InputStream isR = act.getResources().openRawResource(R.raw.region);
291

    
292
      Bitmap bitmapB,bitmapC,bitmapR;
293
        
294
      try 
295
        {
296
        bitmapB = BitmapFactory.decodeStream(isB);
297
        bitmapC = BitmapFactory.decodeStream(isC);
298
        bitmapR = BitmapFactory.decodeStream(isR);
299
        }
300
      finally 
301
        {
302
        try 
303
          {
304
          isB.close();
305
          isC.close();
306
          isR.close();
307
          }
308
        catch(IOException e) { }
309
        }  
310
      
311
      mObjectTexture.setTexture( act.getBitmap() );
312
      mBackgroundTexture.setTexture(bitmapB);
313
      mCenterTexture.setTexture(bitmapC);
314
      mRegionTexture.setTexture(bitmapR);
315

    
316
      DistortedLibrary.setMax(EffectType.VERTEX  ,10);
317
      DistortedLibrary.setMax(EffectType.FRAGMENT,10);
318

    
319
      Effect.enableEffects(EffectType.VERTEX);
320
      Effect.enableEffects(EffectType.FRAGMENT);
321
      Effect.enableEffects(EffectType.POSTPROCESS);
322

    
323
      try
324
        {
325
        DistortedLibrary.onCreate(mView.getContext());
326
        }
327
      catch(Exception ex)
328
        {
329
        android.util.Log.e("Effects3D", ex.getMessage() );
330
        }
331
      }
332
}
(4-4/7)