Project

General

Profile

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

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

1 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 76a81b6a Leszek Koltunski
package org.distorted.examples.effects3d;
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
import org.distorted.library.Distorted;
28 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
29 fe59d375 Leszek Koltunski
import org.distorted.library.DistortedNode;
30 d218d64e leszek
import org.distorted.library.DistortedScreen;
31 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
32 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
33 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
34
import org.distorted.library.MeshObject;
35 08f92d82 Leszek Koltunski
import org.distorted.library.EffectTypes;
36 950511ed Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
37 833685d0 Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
38 08f92d82 Leszek Koltunski
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 76a81b6a Leszek Koltunski
class Effects3DRenderer implements GLSurfaceView.Renderer
50 08f92d82 Leszek Koltunski
{
51 af662543 leszek
    private static final float FOV = 70.0f;
52
    private static final float NEAR = 0.1f;
53
54 08f92d82 Leszek Koltunski
    private GLSurfaceView mView;
55 f6d884d5 Leszek Koltunski
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
56 d04a4886 Leszek Koltunski
    private DistortedEffects mObjectEffects, mBackgroundEffects, mCenterEffects, mRegionEffects;
57 d218d64e leszek
    private DistortedScreen mScreen;
58 fe59d375 Leszek Koltunski
    private DistortedNode mCenterNode, mRegionNode;
59 8ff32d4d Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
60 833685d0 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
61 950511ed Leszek Koltunski
62 24991bc2 Leszek Koltunski
    private Dynamic3D mCenterInter, mRegionInter;
63
    private Static3D mCenterPoint, mRegionPoint;
64 6f779cd4 Leszek Koltunski
    private Dynamic3D mRegionScaleInter;
65
    private Static3D mRegionScalePoint;
66
67
    private float mFactorObj, mFactorReg;
68 950511ed Leszek Koltunski
69 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
70 d2337a3a Leszek Koltunski
    int mScreenMin;
71 aac5c562 leszek
    private boolean mShowing=false;
72 833685d0 Leszek Koltunski
73 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75 4f8ca8ff Leszek Koltunski
    Effects3DRenderer(GLSurfaceView v)
76 08f92d82 Leszek Koltunski
      {
77
      mView = v;
78 d40cfeb2 Leszek Koltunski
79 e8b6aa95 Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)v.getContext();
80
81 f6d884d5 Leszek Koltunski
      mObjectTexture     = act.getTexture();
82 d04a4886 Leszek Koltunski
      mObjectEffects     = act.getEffects();
83 7451c98a Leszek Koltunski
      mBackgroundTexture = new DistortedTexture(100,100);
84
      mCenterTexture     = new DistortedTexture(100,100);
85
      mRegionTexture     = new DistortedTexture(100,100);
86 d04a4886 Leszek Koltunski
      mBackgroundEffects = new DistortedEffects();
87
      mCenterEffects     = new DistortedEffects();
88
      mRegionEffects     = new DistortedEffects();
89 f6d884d5 Leszek Koltunski
90 aac5c562 leszek
      MeshObject meshO   = act.getMesh();
91
      MeshFlat quad      = new MeshFlat(1,1);
92
93 f6d884d5 Leszek Koltunski
      mObjWidth = mObjectTexture.getWidth();
94
      mObjHeight= mObjectTexture.getHeight();
95 aac5c562 leszek
      mObjDepth = mObjectTexture.getDepth(meshO);
96 d40cfeb2 Leszek Koltunski
97 833685d0 Leszek Koltunski
      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 950511ed Leszek Koltunski
106 24991bc2 Leszek Koltunski
      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 6f779cd4 Leszek Koltunski
114
      mRegionScaleInter = new Dynamic3D();
115
      mRegionScalePoint = new Static3D(0,0,0);
116
      mRegionScaleInter.add(mRegionScalePoint);
117 392e16fd Leszek Koltunski
118 aac5c562 leszek
      mCenterNode = new DistortedNode(mCenterTexture, mCenterEffects, quad);
119
      mRegionNode = new DistortedNode(mRegionTexture, mRegionEffects, quad);
120 fe59d375 Leszek Koltunski
121 d218d64e leszek
      mScreen = new DistortedScreen();
122 af662543 leszek
      mScreen.setProjection(FOV, NEAR);
123 aac5c562 leszek
      mScreen.attach(mBackgroundTexture, mBackgroundEffects, quad );
124
      mScreen.attach(mObjectTexture    , mObjectEffects    , meshO);
125 f338550a leszek
      }
126
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
129 98c04ab8 leszek
    void showRegionAndCenter(boolean show)
130 f338550a leszek
      {
131 98c04ab8 leszek
      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 950511ed Leszek Koltunski
      }
147
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
150 334c13fa Leszek Koltunski
    void setCenter(float x, float y, float z)
151 950511ed Leszek Koltunski
      {
152 334c13fa Leszek Koltunski
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
153 08f92d82 Leszek Koltunski
      }
154
155 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
156
157 4f8ca8ff Leszek Koltunski
    void setRegion(float x, float y, float r)
158 6f779cd4 Leszek Koltunski
      {
159 f6d884d5 Leszek Koltunski
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
160 24991bc2 Leszek Koltunski
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
161 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
162 6f779cd4 Leszek Koltunski
      }
163
164 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
165 56cbe1cf Leszek Koltunski
166
    public void onDrawFrame(GL10 glUnused)
167 08f92d82 Leszek Koltunski
      {
168 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
169 08f92d82 Leszek Koltunski
      }
170
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172 56cbe1cf Leszek Koltunski
173
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
174 08f92d82 Leszek Koltunski
      {
175 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
176 752c6b57 Leszek Koltunski
      Static3D centerRot = new Static3D(0,0,0);
177 833685d0 Leszek Koltunski
178 752c6b57 Leszek Koltunski
      //// 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 392e16fd Leszek Koltunski
      mBackgroundEffects.abortEffects(EffectTypes.MATRIX);
184 752c6b57 Leszek Koltunski
      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 950511ed Leszek Koltunski
198 752c6b57 Leszek Koltunski
199
      /*
200 950511ed Leszek Koltunski
      float factorCen;
201 f6d884d5 Leszek Koltunski
      int centerSize = mCenterTexture.getWidth();
202
      int regionSize = mRegionTexture.getWidth();
203 6f779cd4 Leszek Koltunski
204 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
205 d40cfeb2 Leszek Koltunski
        {
206 950511ed Leszek Koltunski
        mFactorObj = (0.80f*height)/mObjHeight;
207
        factorCen  = (0.08f*height)/centerSize;
208 261fe5bd Leszek Koltunski
        }
209 d40cfeb2 Leszek Koltunski
      else
210
        {
211 950511ed Leszek Koltunski
        mFactorObj = (0.80f*width)/mObjWidth;
212
        factorCen  = (0.08f*width)/centerSize;
213 d40cfeb2 Leszek Koltunski
        }
214
215 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
216 24991bc2 Leszek Koltunski
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
217
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
218 9ae05f6c Leszek Koltunski
219 17600407 Leszek Koltunski
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
220 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
221 752c6b57 Leszek Koltunski
      */
222 950511ed Leszek Koltunski
223
224 752c6b57 Leszek Koltunski
      //// CENTER //////////////
225
      float qCen= ((float)width/height);
226
      float sCen= 0.1f;
227 950511ed Leszek Koltunski
228 752c6b57 Leszek Koltunski
      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 392e16fd Leszek Koltunski
      mCenterEffects.move(mCenterInter);
234 752c6b57 Leszek Koltunski
      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 950511ed Leszek Koltunski
240 392e16fd Leszek Koltunski
      mRegionEffects.quaternion(mQuatInt1, rotateCen);
241
      mRegionEffects.quaternion(mQuatInt2, rotateCen);
242 6f779cd4 Leszek Koltunski
243 392e16fd Leszek Koltunski
      mRegionEffects.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
244 8ff32d4d Leszek Koltunski
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
245 392e16fd Leszek Koltunski
      mRegionEffects.move(mCenterInter);
246
      mRegionEffects.move(mRegionInter);
247
      mRegionEffects.scale(mRegionScaleInter);
248
      mRegionEffects.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
249 752c6b57 Leszek Koltunski
      */
250 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
251 08f92d82 Leszek Koltunski
      }
252
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254 56cbe1cf Leszek Koltunski
255
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
256 08f92d82 Leszek Koltunski
      {
257 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
258 bcc8e016 Leszek Koltunski
259 f6d884d5 Leszek Koltunski
      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 71d8ad03 Leszek Koltunski
263 f6d884d5 Leszek Koltunski
      Bitmap bitmapB,bitmapC,bitmapR;
264 08f92d82 Leszek Koltunski
        
265
      try 
266
        {
267 f6d884d5 Leszek Koltunski
        bitmapB = BitmapFactory.decodeStream(isB);
268
        bitmapC = BitmapFactory.decodeStream(isC);
269
        bitmapR = BitmapFactory.decodeStream(isR);
270 71d8ad03 Leszek Koltunski
        }
271 08f92d82 Leszek Koltunski
      finally 
272
        {
273
        try 
274
          {
275 f6d884d5 Leszek Koltunski
          isB.close();
276
          isC.close();
277
          isR.close();
278 9167cfd4 Leszek Koltunski
          }
279 08f92d82 Leszek Koltunski
        catch(IOException e) { }
280
        }  
281
      
282 f6d884d5 Leszek Koltunski
      mObjectTexture.setTexture( act.getBitmap() );
283
      mBackgroundTexture.setTexture(bitmapB);
284
      mCenterTexture.setTexture(bitmapC);
285
      mRegionTexture.setTexture(bitmapR);
286 950511ed Leszek Koltunski
287 6637d0f2 Leszek Koltunski
      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 08f92d82 Leszek Koltunski
      try
300
        {
301 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
302 08f92d82 Leszek Koltunski
        }
303
      catch(Exception ex)
304
        {
305 76a81b6a Leszek Koltunski
        android.util.Log.e("Effects3D", ex.getMessage() );
306 08f92d82 Leszek Koltunski
        }
307
      }
308 56cbe1cf Leszek Koltunski
}