Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 6637d0f2

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 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
25 08f92d82 Leszek Koltunski
import android.opengl.GLSurfaceView;
26
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
30 fe59d375 Leszek Koltunski
import org.distorted.library.DistortedNode;
31 d218d64e leszek
import org.distorted.library.DistortedScreen;
32 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
33 6637d0f2 Leszek Koltunski
import org.distorted.library.EffectNames;
34 b01acdaf Leszek Koltunski
import org.distorted.library.MeshFlat;
35
import org.distorted.library.MeshObject;
36 08f92d82 Leszek Koltunski
import org.distorted.library.EffectTypes;
37 950511ed Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
38 833685d0 Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
39 08f92d82 Leszek Koltunski
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 76a81b6a Leszek Koltunski
class Effects3DRenderer implements GLSurfaceView.Renderer
51 08f92d82 Leszek Koltunski
{
52 af662543 leszek
    private static final float FOV = 70.0f;
53
    private static final float NEAR = 0.1f;
54
55 08f92d82 Leszek Koltunski
    private GLSurfaceView mView;
56 f6d884d5 Leszek Koltunski
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
57 d04a4886 Leszek Koltunski
    private DistortedEffects mObjectEffects, mBackgroundEffects, mCenterEffects, mRegionEffects;
58 d218d64e leszek
    private DistortedScreen mScreen;
59 fe59d375 Leszek Koltunski
    private DistortedNode mCenterNode, mRegionNode;
60 8ff32d4d Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
61 833685d0 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
62 950511ed Leszek Koltunski
63 24991bc2 Leszek Koltunski
    private Dynamic3D mCenterInter, mRegionInter;
64
    private Static3D mCenterPoint, mRegionPoint;
65 6f779cd4 Leszek Koltunski
    private Dynamic3D mRegionScaleInter;
66
    private Static3D mRegionScalePoint;
67
68
    private float mFactorObj, mFactorReg;
69 950511ed Leszek Koltunski
70 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
71 d2337a3a Leszek Koltunski
    int mScreenMin;
72 aac5c562 leszek
    private boolean mShowing=false;
73 833685d0 Leszek Koltunski
74 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
75
76 4f8ca8ff Leszek Koltunski
    Effects3DRenderer(GLSurfaceView v)
77 08f92d82 Leszek Koltunski
      {
78
      mView = v;
79 d40cfeb2 Leszek Koltunski
80 e8b6aa95 Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)v.getContext();
81
82 f6d884d5 Leszek Koltunski
      mObjectTexture     = act.getTexture();
83 d04a4886 Leszek Koltunski
      mObjectEffects     = act.getEffects();
84 7451c98a Leszek Koltunski
      mBackgroundTexture = new DistortedTexture(100,100);
85
      mCenterTexture     = new DistortedTexture(100,100);
86
      mRegionTexture     = new DistortedTexture(100,100);
87 d04a4886 Leszek Koltunski
      mBackgroundEffects = new DistortedEffects();
88
      mCenterEffects     = new DistortedEffects();
89
      mRegionEffects     = new DistortedEffects();
90 f6d884d5 Leszek Koltunski
91 aac5c562 leszek
      MeshObject meshO   = act.getMesh();
92
      MeshFlat quad      = new MeshFlat(1,1);
93
94 f6d884d5 Leszek Koltunski
      mObjWidth = mObjectTexture.getWidth();
95
      mObjHeight= mObjectTexture.getHeight();
96 aac5c562 leszek
      mObjDepth = mObjectTexture.getDepth(meshO);
97 d40cfeb2 Leszek Koltunski
98 833685d0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
99
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
100
101
      mQuatInt1 = new DynamicQuat(0,0.5f);
102
      mQuatInt2 = new DynamicQuat(0,0.5f);
103
104
      mQuatInt1.add(mQuat1);
105
      mQuatInt2.add(mQuat2);
106 950511ed Leszek Koltunski
107 24991bc2 Leszek Koltunski
      mCenterInter= new Dynamic3D();
108
      mCenterPoint= new Static3D(0,0,0);
109
      mCenterInter.add(mCenterPoint);
110
111
      mRegionInter= new Dynamic3D();
112
      mRegionPoint= new Static3D(0,0,0);
113
      mRegionInter.add(mRegionPoint);
114 6f779cd4 Leszek Koltunski
115
      mRegionScaleInter = new Dynamic3D();
116
      mRegionScalePoint = new Static3D(0,0,0);
117
      mRegionScaleInter.add(mRegionScalePoint);
118 392e16fd Leszek Koltunski
119 aac5c562 leszek
      mCenterNode = new DistortedNode(mCenterTexture, mCenterEffects, quad);
120
      mRegionNode = new DistortedNode(mRegionTexture, mRegionEffects, quad);
121 fe59d375 Leszek Koltunski
122 d218d64e leszek
      mScreen = new DistortedScreen();
123 af662543 leszek
      mScreen.setProjection(FOV, NEAR);
124 aac5c562 leszek
      mScreen.attach(mBackgroundTexture, mBackgroundEffects, quad );
125
      mScreen.attach(mObjectTexture    , mObjectEffects    , meshO);
126 f338550a leszek
      }
127
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
130 98c04ab8 leszek
    void showRegionAndCenter(boolean show)
131 f338550a leszek
      {
132 98c04ab8 leszek
      if( mShowing!=show )
133
        {
134
        if( show )
135
          {
136
          mScreen.attach(mCenterNode);
137
          mScreen.attach(mRegionNode);
138
          }
139
        else
140
          {
141
          mScreen.detach(mCenterNode);
142
          mScreen.detach(mRegionNode);
143
          }
144
145
        mShowing = show;
146
        }
147 950511ed Leszek Koltunski
      }
148
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
151 334c13fa Leszek Koltunski
    void setCenter(float x, float y, float z)
152 950511ed Leszek Koltunski
      {
153 334c13fa Leszek Koltunski
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
154 08f92d82 Leszek Koltunski
      }
155
156 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
157
158 4f8ca8ff Leszek Koltunski
    void setRegion(float x, float y, float r)
159 6f779cd4 Leszek Koltunski
      {
160 f6d884d5 Leszek Koltunski
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
161 24991bc2 Leszek Koltunski
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
162 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
163 6f779cd4 Leszek Koltunski
      }
164
165 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
166 56cbe1cf Leszek Koltunski
167
    public void onDrawFrame(GL10 glUnused)
168 08f92d82 Leszek Koltunski
      {
169 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
170 fe59d375 Leszek Koltunski
      mScreen.render(System.currentTimeMillis());
171 08f92d82 Leszek Koltunski
      }
172
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174 56cbe1cf Leszek Koltunski
175
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
176 08f92d82 Leszek Koltunski
      {
177 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
178
179 392e16fd Leszek Koltunski
      mObjectEffects.abortEffects(EffectTypes.MATRIX);
180
      mBackgroundEffects.abortEffects(EffectTypes.MATRIX);
181
      mCenterEffects.abortEffects(EffectTypes.MATRIX);
182
      mRegionEffects.abortEffects(EffectTypes.MATRIX);
183 950511ed Leszek Koltunski
184
      float factorCen;
185 f6d884d5 Leszek Koltunski
      int centerSize = mCenterTexture.getWidth();
186
      int regionSize = mRegionTexture.getWidth();
187 6f779cd4 Leszek Koltunski
188 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
189 d40cfeb2 Leszek Koltunski
        {
190 950511ed Leszek Koltunski
        mFactorObj = (0.80f*height)/mObjHeight;
191
        factorCen  = (0.08f*height)/centerSize;
192 261fe5bd Leszek Koltunski
        }
193 d40cfeb2 Leszek Koltunski
      else
194
        {
195 950511ed Leszek Koltunski
        mFactorObj = (0.80f*width)/mObjWidth;
196
        factorCen  = (0.08f*width)/centerSize;
197 d40cfeb2 Leszek Koltunski
        }
198
199 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
200 24991bc2 Leszek Koltunski
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
201
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
202 9ae05f6c Leszek Koltunski
203 17600407 Leszek Koltunski
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
204 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
205 950511ed Leszek Koltunski
206 2bf60c29 Leszek Koltunski
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
207
208 392e16fd Leszek Koltunski
      mObjectEffects.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
209
      mObjectEffects.scale(mFactorObj);
210
      mObjectEffects.quaternion(mQuatInt1, rotateObj);
211
      mObjectEffects.quaternion(mQuatInt2, rotateObj);
212 833685d0 Leszek Koltunski
213 950511ed Leszek Koltunski
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
214
215 392e16fd Leszek Koltunski
      mCenterEffects.quaternion(mQuatInt1, rotateCen);
216
      mCenterEffects.quaternion(mQuatInt2, rotateCen);
217 950511ed Leszek Koltunski
218 392e16fd Leszek Koltunski
      mCenterEffects.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
219 8ff32d4d Leszek Koltunski
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+10) );
220 392e16fd Leszek Koltunski
      mCenterEffects.move(mCenterInter);
221
      mCenterEffects.scale(factorCen);
222 950511ed Leszek Koltunski
223 392e16fd Leszek Koltunski
      mRegionEffects.quaternion(mQuatInt1, rotateCen);
224
      mRegionEffects.quaternion(mQuatInt2, rotateCen);
225 6f779cd4 Leszek Koltunski
226 392e16fd Leszek Koltunski
      mRegionEffects.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
227 8ff32d4d Leszek Koltunski
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
228 392e16fd Leszek Koltunski
      mRegionEffects.move(mCenterInter);
229
      mRegionEffects.move(mRegionInter);
230
      mRegionEffects.scale(mRegionScaleInter);
231
      mRegionEffects.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
232 6f779cd4 Leszek Koltunski
233 f6d884d5 Leszek Koltunski
      int backgroundSize = mBackgroundTexture.getWidth();
234 56cbe1cf Leszek Koltunski
      float factorBackX = ((float)width)/backgroundSize;
235
      float factorBackY = ((float)height)/backgroundSize;
236 833685d0 Leszek Koltunski
237 af662543 leszek
      // quite tricky: move the background exactly to the FAR plane! (see DistortedOutputSurface.setProjection() )
238
      mBackgroundEffects.move(new Static3D( -width/2, -height/2, -height*(1.0f-NEAR)/(2.0f*(float)Math.tan(FOV*Math.PI/360))) );
239 392e16fd Leszek Koltunski
      mBackgroundEffects.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
240 71d8ad03 Leszek Koltunski
241 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
242 08f92d82 Leszek Koltunski
      }
243
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245 56cbe1cf Leszek Koltunski
246
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
247 08f92d82 Leszek Koltunski
      {
248 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
249 e7a4ef16 Leszek Koltunski
250 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
251 bcc8e016 Leszek Koltunski
252 f6d884d5 Leszek Koltunski
      InputStream isB = act.getResources().openRawResource(R.raw.water);
253
      InputStream isC = act.getResources().openRawResource(R.raw.center);
254
      InputStream isR = act.getResources().openRawResource(R.raw.region);
255 71d8ad03 Leszek Koltunski
256 f6d884d5 Leszek Koltunski
      Bitmap bitmapB,bitmapC,bitmapR;
257 08f92d82 Leszek Koltunski
        
258
      try 
259
        {
260 f6d884d5 Leszek Koltunski
        bitmapB = BitmapFactory.decodeStream(isB);
261
        bitmapC = BitmapFactory.decodeStream(isC);
262
        bitmapR = BitmapFactory.decodeStream(isR);
263 71d8ad03 Leszek Koltunski
        }
264 08f92d82 Leszek Koltunski
      finally 
265
        {
266
        try 
267
          {
268 f6d884d5 Leszek Koltunski
          isB.close();
269
          isC.close();
270
          isR.close();
271 9167cfd4 Leszek Koltunski
          }
272 08f92d82 Leszek Koltunski
        catch(IOException e) { }
273
        }  
274
      
275 f6d884d5 Leszek Koltunski
      mObjectTexture.setTexture( act.getBitmap() );
276
      mBackgroundTexture.setTexture(bitmapB);
277
      mCenterTexture.setTexture(bitmapC);
278
      mRegionTexture.setTexture(bitmapR);
279 950511ed Leszek Koltunski
280 6637d0f2 Leszek Koltunski
      EffectTypes type;
281
282
      for( EffectNames name : EffectNames.values() )
283
        {
284
        type = name.getType();
285
286
        if( type== EffectTypes.VERTEX || type==EffectTypes.FRAGMENT )
287
          {
288
          DistortedEffects.enableEffect(name);
289
          }
290
        }
291
292 08f92d82 Leszek Koltunski
      try
293
        {
294 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
295 08f92d82 Leszek Koltunski
        }
296
      catch(Exception ex)
297
        {
298 76a81b6a Leszek Koltunski
        android.util.Log.e("Effects3D", ex.getMessage() );
299 08f92d82 Leszek Koltunski
        }
300
      }
301 56cbe1cf Leszek Koltunski
}