Project

General

Profile

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

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

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