Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericRenderer.java @ 687263cc

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

    
70
    Static4D mQuat1, mQuat2;
71
    int mScreenMin;
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

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

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

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

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

    
95
      mFactorObj = 1.0f;
96

    
97
      mObjectEffects     = act.getEffects();
98
      mBackgroundEffects = new DistortedEffects(1);
99
      mCenterEffects     = new DistortedEffects(1);
100
      mRegionEffects     = new DistortedEffects(1);
101

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

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

    
107
      mObjWidth = act.getWidth();
108
      mObjHeight= act.getHeight();
109
      mObjDepth = act.getDepth();
110

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

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

    
118
      mCenterNode = new DistortedNode(mCenterTexture, mCenterEffects, quad);
119
      mRegionNode = new DistortedNode(mRegionTexture, mRegionEffects, quad);
120

    
121
      mScreen = new DistortedScreen();
122
      mScreen.setProjection(FOV, NEAR);
123
      mScreen.attach(mBackgroundTexture, mBackgroundEffects, quad );
124
      mScreen.attach(mObjectTexture    , mObjectEffects    , mesh );
125

    
126
      int regionSize = mRegionEffects.getStartchX();
127
      mRotateCen = new Static3D(0 ,0, 0);
128

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

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

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

    
147
      resetMatrixEffects();
148

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

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
    void resetMatrixEffects()
157
      {
158
      Static3D rotateObj = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, (float)mObjDepth/2 );
159

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

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

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

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

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

    
189
        mShowingCenter = showCenter;
190
        }
191

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

    
197
        mShowingRegion = showRegion;
198
        }
199
      }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

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

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

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

    
215
///////////////////////////////////////////////////////////////////////////////////////////////////
216

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

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

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

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

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

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239

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

    
245
      float factorCen;
246
      int centerSize = mCenterEffects.getStartchX();
247

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

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

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

    
270
      int backgroundSize = mBackgroundEffects.getStartchX();
271
      float factorBackX = ((float)width)/backgroundSize;
272
      float factorBackY = ((float)height)/backgroundSize;
273

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

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

    
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282

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

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

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

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

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

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