Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericRenderer.java @ 698ad0a8

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.MeshQuad;
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 DistortedEffects mObjectEffects;
59
    private DistortedScreen mScreen;
60
    private MeshQuad mRegionQuad, mCenterQuad, mBackgroundQuad;
61
    private DistortedNode mCenterNode, mRegionNode;
62
    private float mObjWidth, mObjHeight, mObjDepth;
63
    private Static3D mCenterPoint, mRegionPoint, mRegionScalePoint;
64
    private Static3D mRotateCen, mMoveObject, mScaleObject, mMoveCenter, mScaleCenter, mMoveRegion, mMoveBackground, mScaleBackground;
65
    private boolean mShowingCenter=false;
66
    private boolean mShowingRegion=false;
67
    private float mFactorObj;
68
    private int mWidth;
69
    private float mFactor;
70

    
71
    Static4D mQuat1, mQuat2;
72
    int mScreenMin;
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
    GenericRenderer(GLSurfaceView v)
77
      {
78
      mView = v;
79

    
80
      GenericActivity2 act = (GenericActivity2)v.getContext();
81

    
82
      mRotateCen      = new Static3D(0,0,0);
83
      mMoveObject     = new Static3D(0,0,0);
84
      mScaleObject    = new Static3D(1,1,1);
85
      mMoveCenter     = new Static3D(0,0,0);
86
      mScaleCenter    = new Static3D(1,1,1);
87
      mMoveRegion     = new Static3D(0,0,0);
88
      mMoveBackground = new Static3D(0,0,0);
89
      mScaleBackground= new Static3D(1,1,1);
90

    
91
      mObjectTexture     = act.getTexture();
92
      mBackgroundTexture = new DistortedTexture();
93
      mCenterTexture     = new DistortedTexture();
94
      mRegionTexture     = new DistortedTexture();
95

    
96
      mFactorObj = 1.0f;
97

    
98
      mObjectEffects   = act.getEffects();
99
      DistortedEffects backgroundEffects = new DistortedEffects();
100
      DistortedEffects centerEffects     = new DistortedEffects();
101
      DistortedEffects regionEffects     = new DistortedEffects();
102

    
103
      MeshBase mesh   = act.getMesh();
104
      mRegionQuad     = new MeshQuad();
105
      mCenterQuad     = new MeshQuad();
106
      mBackgroundQuad = new MeshQuad();
107

    
108
      mFactor = mesh instanceof MeshSphere ? 1.0f : 0.7f;
109

    
110
      mObjWidth = act.getWidth();
111
      mObjHeight= act.getHeight();
112
      mObjDepth = act.getDepth();
113

    
114
      mQuat1 = new Static4D(0,0,0,1);  // unity
115
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
116

    
117
      mCenterPoint= new Static3D(0,0,0);
118
      mRegionPoint= new Static3D(0,0,0);
119
      mRegionScalePoint = new Static3D(0,0,0);
120

    
121
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, mCenterQuad);
122
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, mRegionQuad);
123

    
124
      mScreen = new DistortedScreen();
125
      mScreen.setProjection(FOV, NEAR);
126
      mScreen.attach(mBackgroundTexture, backgroundEffects, mBackgroundQuad );
127
      mScreen.attach(mObjectTexture    , mObjectEffects   , mesh );
128

    
129
      float regionSize = mRegionQuad.getStretchX();
130
      mRotateCen = new Static3D(0 ,0, 0);
131

    
132
      MatrixEffectQuaternion quat1cen = new MatrixEffectQuaternion(mQuat1, mRotateCen);
133
      MatrixEffectQuaternion quat2cen = new MatrixEffectQuaternion(mQuat2, mRotateCen);
134
      MatrixEffectMove centerMove = new MatrixEffectMove(mCenterPoint);
135

    
136
      centerEffects.apply( new MatrixEffectScale(mScaleCenter) );
137
      centerEffects.apply( centerMove );
138
      centerEffects.apply( new MatrixEffectMove(mMoveCenter) );
139
      centerEffects.apply(quat2cen);
140
      centerEffects.apply(quat1cen);
141

    
142
      regionEffects.apply( new MatrixEffectMove(new Static3D( -regionSize*0.5f , -regionSize*0.5f , 0)) );
143
      regionEffects.apply( new MatrixEffectScale(mRegionScalePoint) );
144
      regionEffects.apply( new MatrixEffectMove(mRegionPoint) );
145
      regionEffects.apply( centerMove );
146
      regionEffects.apply( new MatrixEffectMove(mMoveRegion) );
147
      regionEffects.apply(quat2cen);
148
      regionEffects.apply(quat1cen);
149

    
150
      resetMatrixEffects();
151

    
152
      // quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
153
      backgroundEffects.apply(new MatrixEffectScale(mScaleBackground) );
154
      backgroundEffects.apply(new MatrixEffectMove(mMoveBackground) );
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
    void resetMatrixEffects()
160
      {
161
      Static3D rotateObj = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, (float)mObjDepth/2 );
162

    
163
      MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1,  rotateObj);
164
      MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2,  rotateObj);
165

    
166
      mObjectEffects.apply(quat2obj);
167
      mObjectEffects.apply(quat1obj);
168
      mObjectEffects.apply( new MatrixEffectScale(mScaleObject) );
169
      mObjectEffects.apply( new MatrixEffectMove(mMoveObject));
170

    
171
      mQuat1.set(0,0,0,1);
172
      mQuat2.set(0,0,0,1);
173
      }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

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

    
192
        mShowingCenter = showCenter;
193
        }
194

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

    
200
        mShowingRegion = showRegion;
201
        }
202
      }
203

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

    
206
    public int getWidth()
207
      {
208
      return mWidth;
209
      }
210

    
211
///////////////////////////////////////////////////////////////////////////////////////////////////
212

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

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
    void setRegion(float x, float y, float z, float r)
221
      {
222
      float factorReg = 2*mFactorObj*r/mRegionQuad.getStretchX();
223
      mRegionPoint.set(mFactorObj*x,mFactorObj*y, mFactorObj*z);
224
      mRegionScalePoint.set(factorReg,factorReg,factorReg);
225
      }
226

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

    
229
    void useOIT(boolean use)
230
      {
231
      mScreen.setOrderIndependentTransparency(use);
232
      }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235

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

    
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242

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

    
248
      float factorCen;
249
      float centerSize = mCenterQuad.getStretchX();
250

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

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

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

    
273
      float backgroundSize = mBackgroundQuad.getStretchX();
274
      float factorBackX = ((float)width)/backgroundSize;
275
      float factorBackY = ((float)height)/backgroundSize;
276

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

    
281
      mScreen.resize(width, height);
282
      }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285

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

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

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

    
318
      DistortedLibrary.setMax(EffectType.VERTEX  ,10);
319
      DistortedLibrary.setMax(EffectType.FRAGMENT,10);
320

    
321
      Effect.enableEffects(EffectType.VERTEX);
322
      Effect.enableEffects(EffectType.FRAGMENT);
323
      Effect.enableEffects(EffectType.POSTPROCESS);
324

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