Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ e4330c89

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.effects3d;
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.EffectName;
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.Distorted;
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.main.MeshFlat;
38
import org.distorted.library.main.MeshObject;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.type.Static4D;
41

    
42
import java.io.IOException;
43
import java.io.InputStream;
44

    
45
import javax.microedition.khronos.egl.EGLConfig;
46
import javax.microedition.khronos.opengles.GL10;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
class Effects3DRenderer implements GLSurfaceView.Renderer
51
{
52
    private static final float FOV = 70.0f;
53
    private static final float NEAR = 0.1f;
54

    
55
    private GLSurfaceView mView;
56
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
57
    private DistortedScreen mScreen;
58
    private DistortedNode mCenterNode, mRegionNode;
59
    private int mObjWidth, mObjHeight;
60
    private Static3D mCenterPoint, mRegionPoint, mRegionScalePoint;
61
    private Static3D mRotateCen, mMoveObject, mScaleObject, mMoveCenter, mScaleCenter, mMoveRegion, mMoveBackground, mScaleBackground;
62
    private boolean mShowingCenter=false;
63
    private boolean mShowingRegion=false;
64
    private float mFactorObj, mFactorReg;
65

    
66
    Static4D mQuat1, mQuat2;
67
    int mScreenMin;
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
    Effects3DRenderer(GLSurfaceView v)
72
      {
73
      mView = v;
74

    
75
      Effects3DActivity act = (Effects3DActivity)v.getContext();
76

    
77
      mRotateCen      = new Static3D(0,0,0);
78
      mMoveObject     = new Static3D(0,0,0);
79
      mScaleObject    = new Static3D(1,1,1);
80
      mMoveCenter     = new Static3D(0,0,0);
81
      mScaleCenter    = new Static3D(1,1,1);
82
      mMoveRegion     = new Static3D(0,0,0);
83
      mMoveBackground = new Static3D(0,0,0);
84
      mScaleBackground= new Static3D(1,1,1);
85

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

    
91
      DistortedEffects objectEffects     = act.getEffects();
92
      DistortedEffects backgroundEffects = new DistortedEffects();
93
      DistortedEffects centerEffects     = new DistortedEffects();
94
      DistortedEffects regionEffects     = new DistortedEffects();
95

    
96
      MeshObject meshO   = act.getMesh();
97
      MeshFlat quad      = new MeshFlat(1,1);
98

    
99
      mObjWidth = mObjectTexture.getWidth();
100
      mObjHeight= mObjectTexture.getHeight();
101
      int objDepth = mObjectTexture.getDepth(meshO);
102

    
103
      mQuat1 = new Static4D(0,0,0,1);  // unity
104
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
105

    
106
      mCenterPoint= new Static3D(0,0,0);
107
      mRegionPoint= new Static3D(0,0,0);
108
      mRegionScalePoint = new Static3D(0,0,0);
109

    
110
      mCenterNode = new DistortedNode(mCenterTexture, centerEffects, quad);
111
      mRegionNode = new DistortedNode(mRegionTexture, regionEffects, quad);
112

    
113
      mScreen = new DistortedScreen();
114
      mScreen.setProjection(FOV, NEAR);
115
      mScreen.attach(mBackgroundTexture, backgroundEffects, quad );
116
      mScreen.attach(mObjectTexture    , objectEffects    , meshO);
117

    
118
      int regionSize = mRegionTexture.getWidth();
119
      Static3D rotateObj = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, -(float)objDepth/2 );
120
      mRotateCen = new Static3D(0 ,0, 0);
121

    
122
      MatrixEffectQuaternion quat1obj = new MatrixEffectQuaternion(mQuat1,  rotateObj);
123
      MatrixEffectQuaternion quat2obj = new MatrixEffectQuaternion(mQuat2,  rotateObj);
124
      MatrixEffectQuaternion quat1cen = new MatrixEffectQuaternion(mQuat1, mRotateCen);
125
      MatrixEffectQuaternion quat2cen = new MatrixEffectQuaternion(mQuat2, mRotateCen);
126
      MatrixEffectMove centerMove = new MatrixEffectMove(mCenterPoint);
127

    
128
      objectEffects.apply( new MatrixEffectMove(mMoveObject));
129
      objectEffects.apply( new MatrixEffectScale(mScaleObject) );
130
      objectEffects.apply(quat1obj);
131
      objectEffects.apply(quat2obj);
132

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

    
139
      regionEffects.apply(quat1cen);
140
      regionEffects.apply(quat2cen);
141
      regionEffects.apply( new MatrixEffectMove(mMoveRegion) );
142
      regionEffects.apply( centerMove );
143
      regionEffects.apply( new MatrixEffectMove(mRegionPoint) );
144
      regionEffects.apply( new MatrixEffectScale(mRegionScalePoint) );
145
      regionEffects.apply( new MatrixEffectMove(new Static3D( -regionSize/2 , -regionSize/2 , 0)) );
146

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

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
    void showRegionAndCenter(boolean showRegion, boolean showCenter)
155
      {
156
      if( mShowingCenter!=showCenter  )
157
        {
158
        if( showCenter ) mScreen.attach(mCenterNode);
159
        else             mScreen.detach(mCenterNode);
160

    
161
        mShowingCenter = showCenter;
162
        }
163

    
164
      if( mShowingRegion!=showRegion  )
165
        {
166
        if( showRegion ) mScreen.attach(mRegionNode);
167
        else             mScreen.detach(mRegionNode);
168

    
169
        mShowingRegion = showRegion;
170
        }
171
      }
172

    
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174

    
175
    void setCenter(float x, float y, float z)
176
      {
177
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
    void setRegion(float x, float y, float r)
183
      {
184
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
185
      mRegionPoint.set(mFactorObj*x,-mFactorObj*y,0);
186
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
187
      }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

    
191
    public void onDrawFrame(GL10 glUnused)
192
      {
193
      mScreen.render(System.currentTimeMillis());
194
      }
195

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

    
198
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
199
      {
200
      mScreenMin = width<height ? width:height;
201

    
202
      float factorCen;
203
      int centerSize = mCenterTexture.getWidth();
204
      int regionSize = mRegionTexture.getWidth();
205

    
206
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
207
        {
208
        mFactorObj = (0.80f*height)/mObjHeight;
209
        factorCen  = (0.08f*height)/centerSize;
210
        }
211
      else
212
        {
213
        mFactorObj = (0.80f*width)/mObjWidth;
214
        factorCen  = (0.08f*width)/centerSize;
215
        }
216

    
217
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
218
      mCenterPoint.set(mFactorObj*act.getCenterX(),+mFactorObj*act.getCenterY(),0);
219
      mRegionPoint.set(mFactorObj*act.getRegionX(),-mFactorObj*act.getRegionY(),0);
220
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
221
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
222
      mMoveObject.set( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0 );
223
      mRotateCen.set(width/2,height/2, 0);
224
      mScaleObject.set(mFactorObj,mFactorObj,mFactorObj);
225
      mMoveCenter.set( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
226
                       (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , 15 );
227
      mScaleCenter.set(factorCen,factorCen,factorCen);
228
      mMoveRegion.set( (width -mFactorObj*mObjWidth )/2 ,(height-mFactorObj*mObjHeight)/2 , 12 );
229

    
230
      int backgroundSize = mBackgroundTexture.getWidth();
231
      float factorBackX = ((float)width)/backgroundSize;
232
      float factorBackY = ((float)height)/backgroundSize;
233

    
234
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
235
      mMoveBackground.set( -width/2, -height/2, -0.9f*height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360)) );
236
      mScaleBackground.set( 2*factorBackX, 2*factorBackY, 1.0f );
237

    
238
      mScreen.resize(width, height);
239
      }
240

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

    
243
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
244
      {
245
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
246

    
247
      InputStream isB = act.getResources().openRawResource(R.raw.water);
248
      InputStream isC = act.getResources().openRawResource(R.raw.center);
249
      InputStream isR = act.getResources().openRawResource(R.raw.region);
250

    
251
      Bitmap bitmapB,bitmapC,bitmapR;
252
        
253
      try 
254
        {
255
        bitmapB = BitmapFactory.decodeStream(isB);
256
        bitmapC = BitmapFactory.decodeStream(isC);
257
        bitmapR = BitmapFactory.decodeStream(isR);
258
        }
259
      finally 
260
        {
261
        try 
262
          {
263
          isB.close();
264
          isC.close();
265
          isR.close();
266
          }
267
        catch(IOException e) { }
268
        }  
269
      
270
      mObjectTexture.setTexture( act.getBitmap() );
271
      mBackgroundTexture.setTexture(bitmapB);
272
      mCenterTexture.setTexture(bitmapC);
273
      mRegionTexture.setTexture(bitmapR);
274

    
275
      EffectType type;
276

    
277
      for( EffectName name : EffectName.values() )
278
        {
279
        type = name.getType();
280

    
281
        if( type== EffectType.VERTEX || type==EffectType.FRAGMENT )
282
          {
283
          DistortedEffects.enableEffect(name);
284
          }
285
        }
286

    
287
      try
288
        {
289
        Distorted.onCreate(mView.getContext());
290
        }
291
      catch(Exception ex)
292
        {
293
        android.util.Log.e("Effects3D", ex.getMessage() );
294
        }
295
      }
296
}
(3-3/4)