Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericRenderer.java @ 061449ed

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.effect.VertexEffectScale;
33
import org.distorted.library.main.DistortedLibrary;
34
import org.distorted.library.main.DistortedEffects;
35
import org.distorted.library.main.DistortedNode;
36
import org.distorted.library.main.DistortedScreen;
37
import org.distorted.library.main.DistortedTexture;
38
import org.distorted.library.mesh.MeshBase;
39
import org.distorted.library.mesh.MeshQuad;
40
import org.distorted.library.mesh.MeshSphere;
41
import org.distorted.library.type.Static3D;
42
import org.distorted.library.type.Static4D;
43

    
44
import java.io.IOException;
45
import java.io.InputStream;
46

    
47
import javax.microedition.khronos.egl.EGLConfig;
48
import javax.microedition.khronos.opengles.GL10;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
class GenericRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
53
{
54
    private static final float FOV = 30.0f;
55
    private static final float NEAR = 0.1f;
56

    
57
    private GLSurfaceView mView;
58
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
59
    private DistortedEffects mObjectEffects;
60
    private DistortedScreen mScreen;
61
    private DistortedNode mCenterNode, mRegionNode;
62
    private float mObjWidth, mObjHeight, mObjDepth;
63
    private Static3D mCenterPoint, mRegionPoint, mScaleRegion;
64
    private Static3D mMoveObject, mScaleObject, mScaleCenter, 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
      mMoveObject     = new Static3D(0,0,0);
83
      mScaleObject    = new Static3D(1,1,1);
84
      mScaleCenter    = new Static3D(1,1,1);
85
      mMoveBackground = new Static3D(0,0,0);
86
      mScaleBackground= new Static3D(1,1,1);
87

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

    
93
      mFactorObj = 1.0f;
94

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

    
100
      MeshBase mesh   = act.getMesh();
101
      MeshQuad quad   = new MeshQuad();
102

    
103
      mFactor = mesh instanceof MeshSphere ? 1.2f : 0.7f;
104

    
105
      mObjWidth = act.getWidth();
106
      mObjHeight= act.getHeight();
107
      mObjDepth = act.getDepth();
108

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

    
112
      mCenterPoint= new Static3D(0,0,0);
113
      mRegionPoint= new Static3D(0,0,0);
114
      mScaleRegion = new Static3D(1,1,1);
115

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

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

    
124
      Static3D rotateCen = new Static3D(0,0,0);
125

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

    
130
      centerEffects.apply( new MatrixEffectScale(mScaleCenter) );
131
      centerEffects.apply( centerMove );
132
      centerEffects.apply( new MatrixEffectMove(new Static3D(0,0,12)) );
133
      centerEffects.apply(quat2cen);
134
      centerEffects.apply(quat1cen);
135

    
136
      regionEffects.apply( new MatrixEffectScale(mScaleRegion) );
137
      regionEffects.apply( centerMove );
138
      regionEffects.apply( new MatrixEffectMove(mRegionPoint) );
139
      regionEffects.apply( new MatrixEffectMove(new Static3D(0,0,10)) );
140
      regionEffects.apply(quat2cen);
141
      regionEffects.apply(quat1cen);
142

    
143
      resetMatrixEffects();
144
      resetVertexEffects();
145

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

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
    void resetMatrixEffects()
154
      {
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
      mObjectEffects.apply(quat2obj);
161
      mObjectEffects.apply(quat1obj);
162
      mObjectEffects.apply( new MatrixEffectScale(mScaleObject) );
163
      mObjectEffects.apply( new MatrixEffectMove(mMoveObject));
164

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

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

    
171
    void resetVertexEffects()
172
      {
173
      Static3D scale = new Static3D(mObjWidth, mObjHeight, mObjDepth);
174
      mObjectEffects.apply( new VertexEffectScale(scale) );
175
      }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

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

    
194
        mShowingCenter = showCenter;
195
        }
196

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

    
202
        mShowingRegion = showRegion;
203
        }
204
      }
205

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

    
208
    public int getWidth()
209
      {
210
      return mWidth;
211
      }
212

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

    
215
    float getScaleFactor()
216
      {
217
      return mFactorObj;
218
      }
219

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

    
222
    void setCenter(float x, float y, float z)
223
      {
224
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
225
      }
226

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

    
229
    void setRegion(float x, float y, float z, float r)
230
      {
231
      float factorReg = 2*mFactorObj*r;
232
      mRegionPoint.set(mFactorObj*x,mFactorObj*y, mFactorObj*z);
233
      mScaleRegion.set(factorReg,factorReg,factorReg);
234
      }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
    void useOIT(boolean use)
239
      {
240
      mScreen.setOrderIndependentTransparency(use);
241
      }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244

    
245
    public void onDrawFrame(GL10 glUnused)
246
      {
247
      mScreen.render(System.currentTimeMillis());
248
      }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
253
      {
254
      mWidth = width;
255
      mScreenMin = Math.min(width, height);
256

    
257
      float factorCen;
258

    
259
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
260
        {
261
        mFactorObj = (mFactor*height)/mObjHeight;
262
        factorCen  = (0.08f  *height);
263
        }
264
      else
265
        {
266
        mFactorObj = (mFactor*width)/mObjWidth;
267
        factorCen  = (0.08f  *width);
268
        }
269

    
270
      setCenter(0,0,0);
271
      setRegion(0.0f,0.0f,0.0f,0.25f*(mObjWidth+mObjHeight));
272

    
273
      mMoveObject.set( 0, 0 , -mFactorObj*mObjDepth );
274
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
275
      mScaleCenter.set(factorCen,factorCen,factorCen);
276

    
277
      // quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
278
      mMoveBackground.set( 0,0, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
279
      mScaleBackground.set( 2*width, 2*height, 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 ignored) { }
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
      DistortedLibrary.onCreate(mView.getContext(), this);
326
      }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

    
330
   public void distortedException(Exception ex)
331
      {
332
      android.util.Log.e("Generic", ex.getMessage() );
333
      }
334
}
(5-5/8)