Project

General

Profile

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

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

1 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 08f92d82 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 08f92d82 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 08f92d82 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 2ec2dc32 Leszek Koltunski
package org.distorted.examples.generic;
21 08f92d82 Leszek Koltunski
22
import android.graphics.Bitmap;
23
import android.graphics.BitmapFactory;
24
import android.opengl.GLSurfaceView;
25
26
import org.distorted.examples.R;
27 b88ec561 Leszek Koltunski
import org.distorted.library.effect.Effect;
28
import org.distorted.library.effect.EffectType;
29 06366d12 Leszek Koltunski
import org.distorted.library.effect.MatrixEffectMove;
30
import org.distorted.library.effect.MatrixEffectQuaternion;
31
import org.distorted.library.effect.MatrixEffectScale;
32 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
33 01782e85 Leszek Koltunski
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 57d7fdba Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
38 f3ca895f Leszek Koltunski
import org.distorted.library.mesh.MeshRectangles;
39 e83350b4 Leszek Koltunski
import org.distorted.library.mesh.MeshSphere;
40 08f92d82 Leszek Koltunski
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 46b26cdc Leszek Koltunski
class GenericRenderer implements GLSurfaceView.Renderer
52 08f92d82 Leszek Koltunski
{
53 f86c9fb5 Leszek Koltunski
    private static final float FOV = 30.0f;
54 af662543 leszek
    private static final float NEAR = 0.1f;
55
56 08f92d82 Leszek Koltunski
    private GLSurfaceView mView;
57 f6d884d5 Leszek Koltunski
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
58 687263cc Leszek Koltunski
    private DistortedEffects mObjectEffects,mBackgroundEffects,mCenterEffects,mRegionEffects;
59 d218d64e leszek
    private DistortedScreen mScreen;
60 fe59d375 Leszek Koltunski
    private DistortedNode mCenterNode, mRegionNode;
61 b424b062 Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
62 06366d12 Leszek Koltunski
    private Static3D mCenterPoint, mRegionPoint, mRegionScalePoint;
63 fec27f16 Leszek Koltunski
    private Static3D mRotateCen, mMoveObject, mScaleObject, mMoveCenter, mScaleCenter, mMoveRegion, mMoveBackground, mScaleBackground;
64
    private boolean mShowingCenter=false;
65
    private boolean mShowingRegion=false;
66 1e7603bb Leszek Koltunski
    private float mFactorObj;
67 65f622c1 Leszek Koltunski
    private int mWidth;
68 e83350b4 Leszek Koltunski
    private float mFactor;
69 950511ed Leszek Koltunski
70 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
71 d2337a3a Leszek Koltunski
    int mScreenMin;
72 833685d0 Leszek Koltunski
73 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75 46b26cdc Leszek Koltunski
    GenericRenderer(GLSurfaceView v)
76 08f92d82 Leszek Koltunski
      {
77
      mView = v;
78 d40cfeb2 Leszek Koltunski
79 46b26cdc Leszek Koltunski
      GenericActivity2 act = (GenericActivity2)v.getContext();
80 e8b6aa95 Leszek Koltunski
81 06366d12 Leszek Koltunski
      mRotateCen      = new Static3D(0,0,0);
82
      mMoveObject     = new Static3D(0,0,0);
83 fec27f16 Leszek Koltunski
      mScaleObject    = new Static3D(1,1,1);
84 06366d12 Leszek Koltunski
      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 f6d884d5 Leszek Koltunski
      mObjectTexture     = act.getTexture();
91 687263cc Leszek Koltunski
      mBackgroundTexture = new DistortedTexture();
92
      mCenterTexture     = new DistortedTexture();
93
      mRegionTexture     = new DistortedTexture();
94 06366d12 Leszek Koltunski
95 1e7603bb Leszek Koltunski
      mFactorObj = 1.0f;
96
97 687263cc Leszek Koltunski
      mObjectEffects     = act.getEffects();
98
      mBackgroundEffects = new DistortedEffects(1);
99
      mCenterEffects     = new DistortedEffects(1);
100
      mRegionEffects     = new DistortedEffects(1);
101 f6d884d5 Leszek Koltunski
102 687263cc Leszek Koltunski
      MeshBase mesh        = act.getMesh();
103
      MeshRectangles quad  = new MeshRectangles(1,1);
104 aac5c562 leszek
105 e83350b4 Leszek Koltunski
      mFactor = mesh instanceof MeshSphere ? 1.0f : 0.7f;
106
107 687263cc Leszek Koltunski
      mObjWidth = act.getWidth();
108
      mObjHeight= act.getHeight();
109
      mObjDepth = act.getDepth();
110 d40cfeb2 Leszek Koltunski
111 833685d0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
112
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
113
114 24991bc2 Leszek Koltunski
      mCenterPoint= new Static3D(0,0,0);
115
      mRegionPoint= new Static3D(0,0,0);
116 6f779cd4 Leszek Koltunski
      mRegionScalePoint = new Static3D(0,0,0);
117 392e16fd Leszek Koltunski
118 687263cc Leszek Koltunski
      mCenterNode = new DistortedNode(mCenterTexture, mCenterEffects, quad);
119
      mRegionNode = new DistortedNode(mRegionTexture, mRegionEffects, quad);
120 fe59d375 Leszek Koltunski
121 e4330c89 Leszek Koltunski
      mScreen = new DistortedScreen();
122 af662543 leszek
      mScreen.setProjection(FOV, NEAR);
123 687263cc Leszek Koltunski
      mScreen.attach(mBackgroundTexture, mBackgroundEffects, quad );
124
      mScreen.attach(mObjectTexture    , mObjectEffects    , mesh );
125 06366d12 Leszek Koltunski
126 687263cc Leszek Koltunski
      int regionSize = mRegionEffects.getStartchX();
127 06366d12 Leszek Koltunski
      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 687263cc Leszek Koltunski
      mCenterEffects.apply( new MatrixEffectScale(mScaleCenter) );
134
      mCenterEffects.apply( centerMove );
135
      mCenterEffects.apply( new MatrixEffectMove(mMoveCenter) );
136
      mCenterEffects.apply(quat2cen);
137
      mCenterEffects.apply(quat1cen);
138 06366d12 Leszek Koltunski
139 687263cc Leszek Koltunski
      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 06366d12 Leszek Koltunski
147 65f622c1 Leszek Koltunski
      resetMatrixEffects();
148
149 e3900503 Leszek Koltunski
      // quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
150 687263cc Leszek Koltunski
      mBackgroundEffects.apply(new MatrixEffectScale(mScaleBackground) );
151
      mBackgroundEffects.apply(new MatrixEffectMove(mMoveBackground) );
152 f338550a leszek
      }
153
154 65f622c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
155
156
    void resetMatrixEffects()
157
      {
158 b424b062 Leszek Koltunski
      Static3D rotateObj = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, (float)mObjDepth/2 );
159 65f622c1 Leszek Koltunski
160
      MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1,  rotateObj);
161
      MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2,  rotateObj);
162
163 687263cc Leszek Koltunski
      mObjectEffects.apply(quat2obj);
164
      mObjectEffects.apply(quat1obj);
165
      mObjectEffects.apply( new MatrixEffectScale(mScaleObject) );
166
      mObjectEffects.apply( new MatrixEffectMove(mMoveObject));
167 65f622c1 Leszek Koltunski
168
      mQuat1.set(0,0,0,1);
169
      mQuat2.set(0,0,0,1);
170
      }
171
172 f338550a leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
173
174 0ab55f0c Leszek Koltunski
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
175 f338550a leszek
      {
176 0ab55f0c Leszek Koltunski
      if( mShowingCenter!=showCenter  )
177 98c04ab8 leszek
        {
178 1e7603bb Leszek Koltunski
        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 0ab55f0c Leszek Koltunski
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 98c04ab8 leszek
        }
199 950511ed Leszek Koltunski
      }
200
201 65f622c1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
202
203
    public int getWidth()
204
      {
205
      return mWidth;
206
      }
207
208 950511ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
209
210 334c13fa Leszek Koltunski
    void setCenter(float x, float y, float z)
211 950511ed Leszek Koltunski
      {
212 334c13fa Leszek Koltunski
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
213 08f92d82 Leszek Koltunski
      }
214
215 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
216
217 9e7b6dbd Leszek Koltunski
    void setRegion(float x, float y, float z, float r)
218 6f779cd4 Leszek Koltunski
      {
219 687263cc Leszek Koltunski
      float factorReg = 2*mFactorObj*r/mRegionEffects.getStartchX();
220 1e7603bb Leszek Koltunski
      mRegionPoint.set(mFactorObj*x,mFactorObj*y, mFactorObj*z);
221
      mRegionScalePoint.set(factorReg,factorReg,factorReg);
222 6f779cd4 Leszek Koltunski
      }
223
224 bddd4b2d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
225
226
    void useOIT(boolean use)
227
      {
228
      mScreen.setOrderIndependentTransparency(use);
229
      }
230
231 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
232 56cbe1cf Leszek Koltunski
233
    public void onDrawFrame(GL10 glUnused)
234 08f92d82 Leszek Koltunski
      {
235 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
236 08f92d82 Leszek Koltunski
      }
237
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239 56cbe1cf Leszek Koltunski
240
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
241 08f92d82 Leszek Koltunski
      {
242 65f622c1 Leszek Koltunski
      mWidth = width;
243 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
244 752c6b57 Leszek Koltunski
245 950511ed Leszek Koltunski
      float factorCen;
246 687263cc Leszek Koltunski
      int centerSize = mCenterEffects.getStartchX();
247 6f779cd4 Leszek Koltunski
248 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
249 d40cfeb2 Leszek Koltunski
        {
250 e83350b4 Leszek Koltunski
        mFactorObj = (mFactor*height)/mObjHeight;
251
        factorCen  = (0.08f  *height)/centerSize;
252 261fe5bd Leszek Koltunski
        }
253 d40cfeb2 Leszek Koltunski
      else
254
        {
255 e83350b4 Leszek Koltunski
        mFactorObj = (mFactor*width)/mObjWidth;
256
        factorCen  = (0.08f  *width)/centerSize;
257 d40cfeb2 Leszek Koltunski
        }
258
259 7126b262 Leszek Koltunski
      setCenter(0.5f*mObjWidth,0.5f*mObjHeight,0.5f*mObjDepth);
260
      setRegion(0.0f,0.0f,0.0f,0.25f*(mObjWidth+mObjHeight));
261 1e7603bb Leszek Koltunski
262 a4d59c0b Leszek Koltunski
      mMoveObject.set( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , -mFactorObj*mObjDepth );
263 1e7603bb Leszek Koltunski
      mRotateCen.set(width*0.5f,height*0.5f, 0);
264 fec27f16 Leszek Koltunski
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
265 06366d12 Leszek Koltunski
      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 fce25d04 leszek
270 687263cc Leszek Koltunski
      int backgroundSize = mBackgroundEffects.getStartchX();
271 fce25d04 leszek
      float factorBackX = ((float)width)/backgroundSize;
272
      float factorBackY = ((float)height)/backgroundSize;
273
274 e3900503 Leszek Koltunski
      // quite tricky: move the background exactly to the FAR plane! (see InternalOutputSurface.setProjection() )
275 1e7603bb Leszek Koltunski
      mMoveBackground.set( -width*0.5f, -height*0.5f, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
276 06366d12 Leszek Koltunski
      mScaleBackground.set( 2*factorBackX, 2*factorBackY, 1.0f );
277 fce25d04 leszek
278 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
279 08f92d82 Leszek Koltunski
      }
280
281
///////////////////////////////////////////////////////////////////////////////////////////////////
282 56cbe1cf Leszek Koltunski
283
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
284 08f92d82 Leszek Koltunski
      {
285 46b26cdc Leszek Koltunski
      GenericActivity2 act = (GenericActivity2)mView.getContext();
286 bcc8e016 Leszek Koltunski
287 f6d884d5 Leszek Koltunski
      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 71d8ad03 Leszek Koltunski
291 f6d884d5 Leszek Koltunski
      Bitmap bitmapB,bitmapC,bitmapR;
292 08f92d82 Leszek Koltunski
        
293
      try 
294
        {
295 f6d884d5 Leszek Koltunski
        bitmapB = BitmapFactory.decodeStream(isB);
296
        bitmapC = BitmapFactory.decodeStream(isC);
297
        bitmapR = BitmapFactory.decodeStream(isR);
298 71d8ad03 Leszek Koltunski
        }
299 08f92d82 Leszek Koltunski
      finally 
300
        {
301
        try 
302
          {
303 f6d884d5 Leszek Koltunski
          isB.close();
304
          isC.close();
305
          isR.close();
306 9167cfd4 Leszek Koltunski
          }
307 08f92d82 Leszek Koltunski
        catch(IOException e) { }
308
        }  
309
      
310 f6d884d5 Leszek Koltunski
      mObjectTexture.setTexture( act.getBitmap() );
311
      mBackgroundTexture.setTexture(bitmapB);
312
      mCenterTexture.setTexture(bitmapC);
313
      mRegionTexture.setTexture(bitmapR);
314 950511ed Leszek Koltunski
315 e3900503 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX  ,10);
316
      DistortedLibrary.setMax(EffectType.FRAGMENT,10);
317 676c14da Leszek Koltunski
318 b88ec561 Leszek Koltunski
      Effect.enableEffects(EffectType.VERTEX);
319
      Effect.enableEffects(EffectType.FRAGMENT);
320
      Effect.enableEffects(EffectType.POSTPROCESS);
321 cdcbdbe3 Leszek Koltunski
322 08f92d82 Leszek Koltunski
      try
323
        {
324 e3900503 Leszek Koltunski
        DistortedLibrary.onCreate(mView.getContext());
325 08f92d82 Leszek Koltunski
        }
326
      catch(Exception ex)
327
        {
328 76a81b6a Leszek Koltunski
        android.util.Log.e("Effects3D", ex.getMessage() );
329 08f92d82 Leszek Koltunski
        }
330
      }
331 56cbe1cf Leszek Koltunski
}