Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 7fca6741

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.GLES20;
25
import android.opengl.GLSurfaceView;
26

    
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29
import org.distorted.library.DistortedEffects;
30
import org.distorted.library.DistortedFramebuffer;
31
import org.distorted.library.DistortedTexture;
32
import org.distorted.library.MeshFlat;
33
import org.distorted.library.MeshObject;
34
import org.distorted.library.EffectTypes;
35
import org.distorted.library.type.Dynamic3D;
36
import org.distorted.library.type.DynamicQuat;
37
import org.distorted.library.type.Static3D;
38
import org.distorted.library.type.Static4D;
39

    
40
import java.io.IOException;
41
import java.io.InputStream;
42

    
43
import javax.microedition.khronos.egl.EGLConfig;
44
import javax.microedition.khronos.opengles.GL10;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
class Effects3DRenderer implements GLSurfaceView.Renderer
49
{
50
    private GLSurfaceView mView;
51
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
52
    private DistortedEffects mObjectEffects, mBackgroundEffects, mCenterEffects, mRegionEffects;
53
    private DistortedFramebuffer mScreen;
54
    private MeshFlat mQuad;
55
    private MeshObject mObjectMesh;
56
    private int mObjWidth, mObjHeight, mObjDepth;
57
    private DynamicQuat mQuatInt1, mQuatInt2;
58

    
59
    private Dynamic3D mCenterInter, mRegionInter;
60
    private Static3D mCenterPoint, mRegionPoint;
61
    private Dynamic3D mRegionScaleInter;
62
    private Static3D mRegionScalePoint;
63

    
64
    private float mFactorObj, mFactorReg;
65

    
66
    Static4D mQuat1, mQuat2;
67
    int mScreenMin;
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
    Effects3DRenderer(GLSurfaceView v)
72
      {
73
      mView = v;
74

    
75
      Effects3DActivity act = (Effects3DActivity)v.getContext();
76

    
77
      mObjectTexture     = act.getTexture();
78
      mObjectMesh = act.getMesh();
79
      mObjectEffects     = act.getEffects();
80
      mBackgroundTexture = new DistortedTexture(100,100);
81
      mCenterTexture     = new DistortedTexture(100,100);
82
      mRegionTexture     = new DistortedTexture(100,100);
83
      mQuad              = new MeshFlat(1,1);
84
      mBackgroundEffects = new DistortedEffects();
85
      mCenterEffects     = new DistortedEffects();
86
      mRegionEffects     = new DistortedEffects();
87

    
88
      mObjWidth = mObjectTexture.getWidth();
89
      mObjHeight= mObjectTexture.getHeight();
90
      mObjDepth = mObjectTexture.getDepth(mObjectMesh);
91

    
92
      mQuat1 = new Static4D(0,0,0,1);  // unity
93
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
94

    
95
      mQuatInt1 = new DynamicQuat(0,0.5f);
96
      mQuatInt2 = new DynamicQuat(0,0.5f);
97

    
98
      mQuatInt1.add(mQuat1);
99
      mQuatInt2.add(mQuat2);
100

    
101
      mCenterInter= new Dynamic3D();
102
      mCenterPoint= new Static3D(0,0,0);
103
      mCenterInter.add(mCenterPoint);
104

    
105
      mRegionInter= new Dynamic3D();
106
      mRegionPoint= new Static3D(0,0,0);
107
      mRegionInter.add(mRegionPoint);
108

    
109
      mRegionScaleInter = new Dynamic3D();
110
      mRegionScalePoint = new Static3D(0,0,0);
111
      mRegionScaleInter.add(mRegionScalePoint);
112

    
113
      mScreen = new DistortedFramebuffer(0);
114
      }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
    void setCenter(float x, float y, float z)
119
      {
120
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
121
      }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
    void setRegion(float x, float y, float r)
126
      {
127
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
128
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
129
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
130
      }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
    public void onDrawFrame(GL10 glUnused)
135
      {
136
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
137

    
138
      long time = System.currentTimeMillis();
139

    
140
      mScreen.renderTo(mBackgroundTexture,mQuad      ,mBackgroundEffects,time);
141
      mScreen.renderTo(mObjectTexture    , mObjectMesh,mObjectEffects    ,time);
142

    
143
      if( Effects3DActivity.supportsCenter() )
144
        {
145
        mScreen.renderTo(mCenterTexture,mQuad,mCenterEffects, time);
146
        if( Effects3DActivity.supportsRegion() ) mScreen.renderTo(mRegionTexture,mQuad,mRegionEffects,time);
147
        }
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
153
      {
154
      mScreenMin = width<height ? width:height;
155

    
156
      mObjectEffects.abortEffects(EffectTypes.MATRIX);
157
      mBackgroundEffects.abortEffects(EffectTypes.MATRIX);
158
      mCenterEffects.abortEffects(EffectTypes.MATRIX);
159
      mRegionEffects.abortEffects(EffectTypes.MATRIX);
160

    
161
      float factorCen;
162
      int centerSize = mCenterTexture.getWidth();
163
      int regionSize = mRegionTexture.getWidth();
164

    
165
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
166
        {
167
        mFactorObj = (0.80f*height)/mObjHeight;
168
        factorCen  = (0.08f*height)/centerSize;
169
        }
170
      else
171
        {
172
        mFactorObj = (0.80f*width)/mObjWidth;
173
        factorCen  = (0.08f*width)/centerSize;
174
        }
175

    
176
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
177
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
178
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
179

    
180
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
181
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
182

    
183
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
184

    
185
      mObjectEffects.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
186
      mObjectEffects.scale(mFactorObj);
187
      mObjectEffects.quaternion(mQuatInt1, rotateObj);
188
      mObjectEffects.quaternion(mQuatInt2, rotateObj);
189

    
190
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
191

    
192
      mCenterEffects.quaternion(mQuatInt1, rotateCen);
193
      mCenterEffects.quaternion(mQuatInt2, rotateCen);
194

    
195
      mCenterEffects.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
196
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+10) );
197
      mCenterEffects.move(mCenterInter);
198
      mCenterEffects.scale(factorCen);
199

    
200
      mRegionEffects.quaternion(mQuatInt1, rotateCen);
201
      mRegionEffects.quaternion(mQuatInt2, rotateCen);
202

    
203
      mRegionEffects.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
204
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
205
      mRegionEffects.move(mCenterInter);
206
      mRegionEffects.move(mRegionInter);
207
      mRegionEffects.scale(mRegionScaleInter);
208
      mRegionEffects.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
209

    
210
      int backgroundSize = mBackgroundTexture.getWidth();
211
      float factorBackX = ((float)width)/backgroundSize;
212
      float factorBackY = ((float)height)/backgroundSize;
213

    
214
      mBackgroundEffects.move(new Static3D( -width/2, -height/2,-mFactorObj*(mObjWidth+mObjHeight)/2) );
215
      mBackgroundEffects.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
216

    
217
      mScreen.resize(width, height);
218
      }
219

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

    
222
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
223
      {
224
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
225

    
226
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
227

    
228
      InputStream isB = act.getResources().openRawResource(R.raw.water);
229
      InputStream isC = act.getResources().openRawResource(R.raw.center);
230
      InputStream isR = act.getResources().openRawResource(R.raw.region);
231

    
232
      Bitmap bitmapB,bitmapC,bitmapR;
233
        
234
      try 
235
        {
236
        bitmapB = BitmapFactory.decodeStream(isB);
237
        bitmapC = BitmapFactory.decodeStream(isC);
238
        bitmapR = BitmapFactory.decodeStream(isR);
239
        }
240
      finally 
241
        {
242
        try 
243
          {
244
          isB.close();
245
          isC.close();
246
          isR.close();
247
          }
248
        catch(IOException e) { }
249
        }  
250
      
251
      mObjectTexture.setTexture( act.getBitmap() );
252
      mBackgroundTexture.setTexture(bitmapB);
253
      mCenterTexture.setTexture(bitmapC);
254
      mRegionTexture.setTexture(bitmapR);
255

    
256
      try
257
        {
258
        Distorted.onCreate(mView.getContext());
259
        }
260
      catch(Exception ex)
261
        {
262
        android.util.Log.e("Effects3D", ex.getMessage() );
263
        }
264
      }
265
}
(3-3/4)