Project

General

Profile

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

examples / src / main / java / org / distorted / examples / generic / GenericRenderer.java @ 75cc1461

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 DistortedNode mCenterNode, mRegionNode;
61
    private float mObjWidth, mObjHeight, mObjDepth;
62
    private Static3D mCenterPoint, mRegionPoint, mScaleRegion;
63
    private Static3D mMoveObject, mScaleObject, mScaleCenter, 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
      mMoveObject     = new Static3D(0,0,0);
82
      mScaleObject    = new Static3D(1,1,1);
83
      mScaleCenter    = new Static3D(1,1,1);
84
      mMoveBackground = new Static3D(0,0,0);
85
      mScaleBackground= new Static3D(1,1,1);
86

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

    
92
      mFactorObj = 1.0f;
93

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

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

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

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

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

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

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

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

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

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

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

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

    
142
      resetMatrixEffects();
143

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

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

    
151
    void resetMatrixEffects()
152
      {
153
      Static3D rotateObj = new Static3D(0,0,0);
154

    
155
      MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1,  rotateObj);
156
      MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2,  rotateObj);
157

    
158
      mObjectEffects.apply(quat2obj);
159
      mObjectEffects.apply(quat1obj);
160
      mObjectEffects.apply( new MatrixEffectScale(mScaleObject) );
161
      mObjectEffects.apply( new MatrixEffectMove(mMoveObject));
162

    
163
      mQuat1.set(0,0,0,1);
164
      mQuat2.set(0,0,0,1);
165
      }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
170
      {
171
      if( mShowingCenter!=showCenter  )
172
        {
173
        if( showCenter )
174
          {
175
          if( mShowingRegion ) mScreen.detach(mRegionNode);
176
          mScreen.attach(mCenterNode);
177
          if( mShowingRegion ) mScreen.attach(mRegionNode);
178
          }
179
        else
180
          {
181
          mScreen.detach(mCenterNode);
182
          }
183

    
184
        mShowingCenter = showCenter;
185
        }
186

    
187
      if( mShowingRegion!=showRegion  )
188
        {
189
        if( showRegion ) mScreen.attach(mRegionNode);
190
        else             mScreen.detach(mRegionNode);
191

    
192
        mShowingRegion = showRegion;
193
        }
194
      }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
    public int getWidth()
199
      {
200
      return mWidth;
201
      }
202

    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204

    
205
    float getScaleFactor()
206
      {
207
      return mFactorObj;
208
      }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

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

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218

    
219
    void setRegion(float x, float y, float z, float r)
220
      {
221
      float factorReg = 2*mFactorObj*r;
222
      mRegionPoint.set(mFactorObj*x,mFactorObj*y, mFactorObj*z);
223
      mScaleRegion.set(factorReg,factorReg,factorReg);
224
      }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

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

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

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

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
243
      {
244
      mWidth = width;
245
      mScreenMin = Math.min(width, height);
246

    
247
      float factorCen;
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);
253
        }
254
      else
255
        {
256
        mFactorObj = (mFactor*width)/mObjWidth;
257
        factorCen  = (0.08f  *width);
258
        }
259

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

    
263
      mMoveObject.set( 0, 0 , -mFactorObj*mObjDepth );
264
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
265
      mScaleCenter.set(factorCen,factorCen,factorCen);
266

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

    
271
      mScreen.resize(width, height);
272
      }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275

    
276
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
277
      {
278
      GenericActivity2 act = (GenericActivity2)mView.getContext();
279

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

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

    
308
      DistortedLibrary.setMax(EffectType.VERTEX  ,10);
309
      DistortedLibrary.setMax(EffectType.FRAGMENT,10);
310

    
311
      Effect.enableEffects(EffectType.VERTEX);
312
      Effect.enableEffects(EffectType.FRAGMENT);
313
      Effect.enableEffects(EffectType.POSTPROCESS);
314

    
315
      try
316
        {
317
        DistortedLibrary.onCreate(mView.getContext());
318
        }
319
      catch(Exception ex)
320
        {
321
        android.util.Log.e("Effects3D", ex.getMessage() );
322
        }
323
      }
324
}
(5-5/8)