Project

General

Profile

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

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

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 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
31 d218d64e leszek
import org.distorted.library.DistortedScreen;
32 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
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
    private GLSurfaceView mView;
52 f6d884d5 Leszek Koltunski
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
53 d04a4886 Leszek Koltunski
    private DistortedEffects mObjectEffects, mBackgroundEffects, mCenterEffects, mRegionEffects;
54 d218d64e leszek
    private DistortedScreen mScreen;
55 b01acdaf Leszek Koltunski
    private MeshFlat mQuad;
56
    private MeshObject mObjectMesh;
57 8ff32d4d Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
58 833685d0 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
59 950511ed Leszek Koltunski
60 24991bc2 Leszek Koltunski
    private Dynamic3D mCenterInter, mRegionInter;
61
    private Static3D mCenterPoint, mRegionPoint;
62 6f779cd4 Leszek Koltunski
    private Dynamic3D mRegionScaleInter;
63
    private Static3D mRegionScalePoint;
64
65
    private float mFactorObj, mFactorReg;
66 950511ed Leszek Koltunski
67 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
68 d2337a3a Leszek Koltunski
    int mScreenMin;
69 833685d0 Leszek Koltunski
70 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72 4f8ca8ff Leszek Koltunski
    Effects3DRenderer(GLSurfaceView v)
73 08f92d82 Leszek Koltunski
      {
74
      mView = v;
75 d40cfeb2 Leszek Koltunski
76 e8b6aa95 Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)v.getContext();
77
78 f6d884d5 Leszek Koltunski
      mObjectTexture     = act.getTexture();
79 bfcf419a Leszek Koltunski
      mObjectMesh        = act.getMesh();
80 d04a4886 Leszek Koltunski
      mObjectEffects     = act.getEffects();
81 7451c98a Leszek Koltunski
      mBackgroundTexture = new DistortedTexture(100,100);
82
      mCenterTexture     = new DistortedTexture(100,100);
83
      mRegionTexture     = new DistortedTexture(100,100);
84 b01acdaf Leszek Koltunski
      mQuad              = new MeshFlat(1,1);
85 d04a4886 Leszek Koltunski
      mBackgroundEffects = new DistortedEffects();
86
      mCenterEffects     = new DistortedEffects();
87
      mRegionEffects     = new DistortedEffects();
88 f6d884d5 Leszek Koltunski
89
      mObjWidth = mObjectTexture.getWidth();
90
      mObjHeight= mObjectTexture.getHeight();
91 b01acdaf Leszek Koltunski
      mObjDepth = mObjectTexture.getDepth(mObjectMesh);
92 d40cfeb2 Leszek Koltunski
93 833685d0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
94
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
95
96
      mQuatInt1 = new DynamicQuat(0,0.5f);
97
      mQuatInt2 = new DynamicQuat(0,0.5f);
98
99
      mQuatInt1.add(mQuat1);
100
      mQuatInt2.add(mQuat2);
101 950511ed Leszek Koltunski
102 24991bc2 Leszek Koltunski
      mCenterInter= new Dynamic3D();
103
      mCenterPoint= new Static3D(0,0,0);
104
      mCenterInter.add(mCenterPoint);
105
106
      mRegionInter= new Dynamic3D();
107
      mRegionPoint= new Static3D(0,0,0);
108
      mRegionInter.add(mRegionPoint);
109 6f779cd4 Leszek Koltunski
110
      mRegionScaleInter = new Dynamic3D();
111
      mRegionScalePoint = new Static3D(0,0,0);
112
      mRegionScaleInter.add(mRegionScalePoint);
113 392e16fd Leszek Koltunski
114 d218d64e leszek
      mScreen = new DistortedScreen();
115 950511ed Leszek Koltunski
      }
116
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119 334c13fa Leszek Koltunski
    void setCenter(float x, float y, float z)
120 950511ed Leszek Koltunski
      {
121 334c13fa Leszek Koltunski
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
122 08f92d82 Leszek Koltunski
      }
123
124 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126 4f8ca8ff Leszek Koltunski
    void setRegion(float x, float y, float r)
127 6f779cd4 Leszek Koltunski
      {
128 f6d884d5 Leszek Koltunski
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
129 24991bc2 Leszek Koltunski
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
130 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
131 6f779cd4 Leszek Koltunski
      }
132
133 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
134 56cbe1cf Leszek Koltunski
135
    public void onDrawFrame(GL10 glUnused)
136 08f92d82 Leszek Koltunski
      {
137 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
138 71d8ad03 Leszek Koltunski
139
      long time = System.currentTimeMillis();
140
141 bc29e409 Leszek Koltunski
      mScreen.renderTo(mBackgroundTexture,mQuad      ,mBackgroundEffects,time);
142 b01acdaf Leszek Koltunski
      mScreen.renderTo(mObjectTexture    , mObjectMesh,mObjectEffects    ,time);
143 24991bc2 Leszek Koltunski
144 4d5b37fe Leszek Koltunski
      if( Effects3DActivity.supportsCenter() )
145 76a81b6a Leszek Koltunski
        {
146 bc29e409 Leszek Koltunski
        mScreen.renderTo(mCenterTexture,mQuad,mCenterEffects, time);
147
        if( Effects3DActivity.supportsRegion() ) mScreen.renderTo(mRegionTexture,mQuad,mRegionEffects,time);
148 76a81b6a Leszek Koltunski
        }
149 08f92d82 Leszek Koltunski
      }
150
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152 56cbe1cf Leszek Koltunski
153
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
154 08f92d82 Leszek Koltunski
      {
155 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
156
157 392e16fd Leszek Koltunski
      mObjectEffects.abortEffects(EffectTypes.MATRIX);
158
      mBackgroundEffects.abortEffects(EffectTypes.MATRIX);
159
      mCenterEffects.abortEffects(EffectTypes.MATRIX);
160
      mRegionEffects.abortEffects(EffectTypes.MATRIX);
161 950511ed Leszek Koltunski
162
      float factorCen;
163 f6d884d5 Leszek Koltunski
      int centerSize = mCenterTexture.getWidth();
164
      int regionSize = mRegionTexture.getWidth();
165 6f779cd4 Leszek Koltunski
166 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
167 d40cfeb2 Leszek Koltunski
        {
168 950511ed Leszek Koltunski
        mFactorObj = (0.80f*height)/mObjHeight;
169
        factorCen  = (0.08f*height)/centerSize;
170 261fe5bd Leszek Koltunski
        }
171 d40cfeb2 Leszek Koltunski
      else
172
        {
173 950511ed Leszek Koltunski
        mFactorObj = (0.80f*width)/mObjWidth;
174
        factorCen  = (0.08f*width)/centerSize;
175 d40cfeb2 Leszek Koltunski
        }
176
177 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
178 24991bc2 Leszek Koltunski
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
179
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
180 9ae05f6c Leszek Koltunski
181 17600407 Leszek Koltunski
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
182 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
183 950511ed Leszek Koltunski
184 2bf60c29 Leszek Koltunski
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
185
186 392e16fd Leszek Koltunski
      mObjectEffects.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
187
      mObjectEffects.scale(mFactorObj);
188
      mObjectEffects.quaternion(mQuatInt1, rotateObj);
189
      mObjectEffects.quaternion(mQuatInt2, rotateObj);
190 833685d0 Leszek Koltunski
191 950511ed Leszek Koltunski
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
192
193 392e16fd Leszek Koltunski
      mCenterEffects.quaternion(mQuatInt1, rotateCen);
194
      mCenterEffects.quaternion(mQuatInt2, rotateCen);
195 950511ed Leszek Koltunski
196 392e16fd Leszek Koltunski
      mCenterEffects.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
197 8ff32d4d Leszek Koltunski
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+10) );
198 392e16fd Leszek Koltunski
      mCenterEffects.move(mCenterInter);
199
      mCenterEffects.scale(factorCen);
200 950511ed Leszek Koltunski
201 392e16fd Leszek Koltunski
      mRegionEffects.quaternion(mQuatInt1, rotateCen);
202
      mRegionEffects.quaternion(mQuatInt2, rotateCen);
203 6f779cd4 Leszek Koltunski
204 392e16fd Leszek Koltunski
      mRegionEffects.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
205 8ff32d4d Leszek Koltunski
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
206 392e16fd Leszek Koltunski
      mRegionEffects.move(mCenterInter);
207
      mRegionEffects.move(mRegionInter);
208
      mRegionEffects.scale(mRegionScaleInter);
209
      mRegionEffects.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
210 6f779cd4 Leszek Koltunski
211 f6d884d5 Leszek Koltunski
      int backgroundSize = mBackgroundTexture.getWidth();
212 56cbe1cf Leszek Koltunski
      float factorBackX = ((float)width)/backgroundSize;
213
      float factorBackY = ((float)height)/backgroundSize;
214 833685d0 Leszek Koltunski
215 392e16fd Leszek Koltunski
      mBackgroundEffects.move(new Static3D( -width/2, -height/2,-mFactorObj*(mObjWidth+mObjHeight)/2) );
216
      mBackgroundEffects.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
217 71d8ad03 Leszek Koltunski
218 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
219 08f92d82 Leszek Koltunski
      }
220
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222 56cbe1cf Leszek Koltunski
223
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
224 08f92d82 Leszek Koltunski
      {
225 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
226 e7a4ef16 Leszek Koltunski
227 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
228 bcc8e016 Leszek Koltunski
229 f6d884d5 Leszek Koltunski
      InputStream isB = act.getResources().openRawResource(R.raw.water);
230
      InputStream isC = act.getResources().openRawResource(R.raw.center);
231
      InputStream isR = act.getResources().openRawResource(R.raw.region);
232 71d8ad03 Leszek Koltunski
233 f6d884d5 Leszek Koltunski
      Bitmap bitmapB,bitmapC,bitmapR;
234 08f92d82 Leszek Koltunski
        
235
      try 
236
        {
237 f6d884d5 Leszek Koltunski
        bitmapB = BitmapFactory.decodeStream(isB);
238
        bitmapC = BitmapFactory.decodeStream(isC);
239
        bitmapR = BitmapFactory.decodeStream(isR);
240 71d8ad03 Leszek Koltunski
        }
241 08f92d82 Leszek Koltunski
      finally 
242
        {
243
        try 
244
          {
245 f6d884d5 Leszek Koltunski
          isB.close();
246
          isC.close();
247
          isR.close();
248 9167cfd4 Leszek Koltunski
          }
249 08f92d82 Leszek Koltunski
        catch(IOException e) { }
250
        }  
251
      
252 f6d884d5 Leszek Koltunski
      mObjectTexture.setTexture( act.getBitmap() );
253
      mBackgroundTexture.setTexture(bitmapB);
254
      mCenterTexture.setTexture(bitmapC);
255
      mRegionTexture.setTexture(bitmapR);
256 950511ed Leszek Koltunski
257 08f92d82 Leszek Koltunski
      try
258
        {
259 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
260 08f92d82 Leszek Koltunski
        }
261
      catch(Exception ex)
262
        {
263 76a81b6a Leszek Koltunski
        android.util.Log.e("Effects3D", ex.getMessage() );
264 08f92d82 Leszek Koltunski
        }
265
      }
266 56cbe1cf Leszek Koltunski
}