Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 51554e47

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.Distorted;
28
import org.distorted.library.DistortedEffects;
29
import org.distorted.library.DistortedNode;
30
import org.distorted.library.DistortedScreen;
31
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.EffectNames;
33
import org.distorted.library.MeshFlat;
34
import org.distorted.library.MeshObject;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.DynamicQuat;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.type.Static4D;
40

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

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

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

    
54
    private GLSurfaceView mView;
55
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
56
    private DistortedEffects mObjectEffects, mBackgroundEffects, mCenterEffects, mRegionEffects;
57
    private DistortedScreen mScreen;
58
    private DistortedNode mCenterNode, mRegionNode;
59
    private int mObjWidth, mObjHeight, mObjDepth;
60
    private DynamicQuat mQuatInt1, mQuatInt2;
61

    
62
    private Dynamic3D mCenterInter, mRegionInter;
63
    private Static3D mCenterPoint, mRegionPoint;
64
    private Dynamic3D mRegionScaleInter;
65
    private Static3D mRegionScalePoint;
66

    
67
    private float mFactorObj, mFactorReg;
68

    
69
    Static4D mQuat1, mQuat2;
70
    int mScreenMin;
71
    private boolean mShowing=false;
72

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

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

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

    
81
      mObjectTexture     = act.getTexture();
82
      mObjectEffects     = act.getEffects();
83
      mBackgroundTexture = new DistortedTexture(100,100);
84
      mCenterTexture     = new DistortedTexture(100,100);
85
      mRegionTexture     = new DistortedTexture(100,100);
86
      mBackgroundEffects = new DistortedEffects();
87
      mCenterEffects     = new DistortedEffects();
88
      mRegionEffects     = new DistortedEffects();
89

    
90
      MeshObject meshO   = act.getMesh();
91
      MeshFlat quad      = new MeshFlat(1,1);
92

    
93
      mObjWidth = mObjectTexture.getWidth();
94
      mObjHeight= mObjectTexture.getHeight();
95
      mObjDepth = mObjectTexture.getDepth(meshO);
96

    
97
      mQuat1 = new Static4D(0,0,0,1);  // unity
98
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
99

    
100
      mQuatInt1 = new DynamicQuat(0,0.5f);
101
      mQuatInt2 = new DynamicQuat(0,0.5f);
102

    
103
      mQuatInt1.add(mQuat1);
104
      mQuatInt2.add(mQuat2);
105

    
106
      mCenterInter= new Dynamic3D();
107
      mCenterPoint= new Static3D(0,0,0);
108
      mCenterInter.add(mCenterPoint);
109

    
110
      mRegionInter= new Dynamic3D();
111
      mRegionPoint= new Static3D(0,0,0);
112
      mRegionInter.add(mRegionPoint);
113

    
114
      mRegionScaleInter = new Dynamic3D();
115
      mRegionScalePoint = new Static3D(0,0,0);
116
      mRegionScaleInter.add(mRegionScalePoint);
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    , meshO);
125
      }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
    void showRegionAndCenter(boolean show)
130
      {
131
      if( mShowing!=show )
132
        {
133
        if( show )
134
          {
135
          mScreen.attach(mCenterNode);
136
          mScreen.attach(mRegionNode);
137
          }
138
        else
139
          {
140
          mScreen.detach(mCenterNode);
141
          mScreen.detach(mRegionNode);
142
          }
143

    
144
        mShowing = show;
145
        }
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
    void setCenter(float x, float y, float z)
151
      {
152
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
153
      }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
    void setRegion(float x, float y, float r)
158
      {
159
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
160
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
161
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    public void onDrawFrame(GL10 glUnused)
167
      {
168
      mScreen.render(System.currentTimeMillis());
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

    
173
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
174
      {
175
      mScreenMin = width<height ? width:height;
176

    
177
      mObjectEffects.abortEffects(EffectTypes.MATRIX);
178
      mBackgroundEffects.abortEffects(EffectTypes.MATRIX);
179
      mCenterEffects.abortEffects(EffectTypes.MATRIX);
180
      mRegionEffects.abortEffects(EffectTypes.MATRIX);
181

    
182
      float factorCen;
183
      int centerSize = mCenterTexture.getWidth();
184
      int regionSize = mRegionTexture.getWidth();
185

    
186
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
187
        {
188
        mFactorObj = (0.80f*height)/mObjHeight;
189
        factorCen  = (0.08f*height)/centerSize;
190
        }
191
      else
192
        {
193
        mFactorObj = (0.80f*width)/mObjWidth;
194
        factorCen  = (0.08f*width)/centerSize;
195
        }
196

    
197
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
198
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
199
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
200

    
201
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
202
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
203

    
204
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
205

    
206
      mObjectEffects.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
207
      mObjectEffects.scale(mFactorObj);
208
      mObjectEffects.quaternion(mQuatInt1, rotateObj);
209
      mObjectEffects.quaternion(mQuatInt2, rotateObj);
210

    
211
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
212

    
213
      mCenterEffects.quaternion(mQuatInt1, rotateCen);
214
      mCenterEffects.quaternion(mQuatInt2, rotateCen);
215

    
216
      mCenterEffects.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
217
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+10) );
218
      mCenterEffects.move(mCenterInter);
219
      mCenterEffects.scale(factorCen);
220

    
221
      mRegionEffects.quaternion(mQuatInt1, rotateCen);
222
      mRegionEffects.quaternion(mQuatInt2, rotateCen);
223

    
224
      mRegionEffects.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
225
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
226
      mRegionEffects.move(mCenterInter);
227
      mRegionEffects.move(mRegionInter);
228
      mRegionEffects.scale(mRegionScaleInter);
229
      mRegionEffects.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
230

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

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

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

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

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

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

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

    
276
      EffectTypes type;
277

    
278
      for( EffectNames name : EffectNames.values() )
279
        {
280
        type = name.getType();
281

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

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