Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 752c6b57

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
      Static3D centerRot = new Static3D(0,0,0);
177

    
178
      //// BACKGROUND ////////////
179
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
180
      float distToScreen = height/(2.0f*(float)Math.tan(FOV*Math.PI/360));
181
      float distToFar    = distToScreen*(2-NEAR);
182
      float factor       = distToFar / distToScreen;
183
      mBackgroundEffects.abortEffects(EffectTypes.MATRIX);
184
      mBackgroundEffects.move(new Static3D( 0, 0, -(distToFar-distToScreen)/width ) );
185
      mBackgroundEffects.scale(new Static3D(factor,factor,1.0f) );
186

    
187
      //// OBJECT //////////////
188
      float qObj= ((float)width/height)*((float)mObjHeight/mObjWidth);
189
      float sObj= 0.8f;
190

    
191
      mObjectEffects.abortEffects(EffectTypes.MATRIX);
192
      mObjectEffects.quaternion(mQuatInt1, centerRot);
193
      mObjectEffects.quaternion(mQuatInt2, centerRot);
194
      mObjectEffects.scale(  qObj<1 ? (new Static3D(sObj,sObj*qObj,sObj)) : (new Static3D(sObj/qObj,sObj,sObj/qObj)) );
195

    
196

    
197

    
198

    
199
      /*
200
      float factorCen;
201
      int centerSize = mCenterTexture.getWidth();
202
      int regionSize = mRegionTexture.getWidth();
203

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

    
215
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
216
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
217
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
218

    
219
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
220
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
221
      */
222

    
223

    
224
      //// CENTER //////////////
225
      float qCen= ((float)width/height);
226
      float sCen= 0.1f;
227

    
228
      mCenterEffects.abortEffects(EffectTypes.MATRIX);
229
      /*
230
      mCenterEffects.quaternion(mQuatInt1, centerRot);
231
      mCenterEffects.quaternion(mQuatInt2, centerRot);
232
      mCenterEffects.move( new Static3D(0,0,0.5f) );
233
      mCenterEffects.move(mCenterInter);
234
      mCenterEffects.scale(qObj<1 ? (new Static3D(sCen,sCen*qCen,sCen)) : (new Static3D(sCen/qCen,sCen,sCen/qCen)));
235
      */
236
/*
237
      //// REGION //////////////
238
      mRegionEffects.abortEffects(EffectTypes.MATRIX);
239

    
240
      mRegionEffects.quaternion(mQuatInt1, rotateCen);
241
      mRegionEffects.quaternion(mQuatInt2, rotateCen);
242

    
243
      mRegionEffects.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
244
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
245
      mRegionEffects.move(mCenterInter);
246
      mRegionEffects.move(mRegionInter);
247
      mRegionEffects.scale(mRegionScaleInter);
248
      mRegionEffects.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
249
      */
250
      mScreen.resize(width, height);
251
      }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254

    
255
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
256
      {
257
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
258

    
259
      InputStream isB = act.getResources().openRawResource(R.raw.water);
260
      InputStream isC = act.getResources().openRawResource(R.raw.center);
261
      InputStream isR = act.getResources().openRawResource(R.raw.region);
262

    
263
      Bitmap bitmapB,bitmapC,bitmapR;
264
        
265
      try 
266
        {
267
        bitmapB = BitmapFactory.decodeStream(isB);
268
        bitmapC = BitmapFactory.decodeStream(isC);
269
        bitmapR = BitmapFactory.decodeStream(isR);
270
        }
271
      finally 
272
        {
273
        try 
274
          {
275
          isB.close();
276
          isC.close();
277
          isR.close();
278
          }
279
        catch(IOException e) { }
280
        }  
281
      
282
      mObjectTexture.setTexture( act.getBitmap() );
283
      mBackgroundTexture.setTexture(bitmapB);
284
      mCenterTexture.setTexture(bitmapC);
285
      mRegionTexture.setTexture(bitmapR);
286

    
287
      EffectTypes type;
288

    
289
      for( EffectNames name : EffectNames.values() )
290
        {
291
        type = name.getType();
292

    
293
        if( type== EffectTypes.VERTEX || type==EffectTypes.FRAGMENT )
294
          {
295
          DistortedEffects.enableEffect(name);
296
          }
297
        }
298

    
299
      try
300
        {
301
        Distorted.onCreate(mView.getContext());
302
        }
303
      catch(Exception ex)
304
        {
305
        android.util.Log.e("Effects3D", ex.getMessage() );
306
        }
307
      }
308
}
(3-3/4)