Project

General

Profile

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

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

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.GLES30;
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.DistortedNode;
31
import org.distorted.library.DistortedScreen;
32
import org.distorted.library.DistortedTexture;
33
import org.distorted.library.MeshFlat;
34
import org.distorted.library.MeshObject;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.DynamicQuat;
38
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
class Effects3DRenderer implements GLSurfaceView.Renderer
50
{
51
    private GLSurfaceView mView;
52
    private DistortedTexture mObjectTexture, mBackgroundTexture, mCenterTexture, mRegionTexture;
53
    private DistortedEffects mObjectEffects, mBackgroundEffects, mCenterEffects, mRegionEffects;
54
    private DistortedScreen mScreen;
55
    private DistortedNode mCenterNode, mRegionNode;
56
    private MeshFlat mQuad;
57
    private MeshObject mObjectMesh;
58
    private int mObjWidth, mObjHeight, mObjDepth;
59
    private DynamicQuat mQuatInt1, mQuatInt2;
60

    
61
    private Dynamic3D mCenterInter, mRegionInter;
62
    private Static3D mCenterPoint, mRegionPoint;
63
    private Dynamic3D mRegionScaleInter;
64
    private Static3D mRegionScalePoint;
65

    
66
    private float mFactorObj, mFactorReg;
67

    
68
    Static4D mQuat1, mQuat2;
69
    int mScreenMin;
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
    Effects3DRenderer(GLSurfaceView v)
74
      {
75
      mView = v;
76

    
77
      Effects3DActivity act = (Effects3DActivity)v.getContext();
78

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

    
90
      mObjWidth = mObjectTexture.getWidth();
91
      mObjHeight= mObjectTexture.getHeight();
92
      mObjDepth = mObjectTexture.getDepth(mObjectMesh);
93

    
94
      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

    
103
      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

    
111
      mRegionScaleInter = new Dynamic3D();
112
      mRegionScalePoint = new Static3D(0,0,0);
113
      mRegionScaleInter.add(mRegionScalePoint);
114

    
115
      mCenterNode = new DistortedNode(mCenterTexture, mCenterEffects, mQuad);
116
      mRegionNode = new DistortedNode(mRegionTexture, mRegionEffects, mQuad);
117

    
118
      mScreen = new DistortedScreen();
119
      mScreen.attach(mBackgroundTexture, mBackgroundEffects, mQuad      );
120
      mScreen.attach(mObjectTexture    , mObjectEffects    , mObjectMesh);
121

    
122
    //if( Effects3DActivity.supportsCenter() )
123
        mScreen.attach(mCenterNode);
124
    //if( Effects3DActivity.supportsRegion() )
125
        mScreen.attach(mRegionNode);
126
      }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
    void setCenter(float x, float y, float z)
131
      {
132
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,mFactorObj*z);
133
      }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
    void setRegion(float x, float y, float r)
138
      {
139
      mFactorReg = 2*mFactorObj*r/mRegionTexture.getWidth();
140
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
141
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
142
      }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
    public void onDrawFrame(GL10 glUnused)
147
      {
148
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
149
      mScreen.render(System.currentTimeMillis());
150
      }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

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

    
158
      mObjectEffects.abortEffects(EffectTypes.MATRIX);
159
      mBackgroundEffects.abortEffects(EffectTypes.MATRIX);
160
      mCenterEffects.abortEffects(EffectTypes.MATRIX);
161
      mRegionEffects.abortEffects(EffectTypes.MATRIX);
162

    
163
      float factorCen;
164
      int centerSize = mCenterTexture.getWidth();
165
      int regionSize = mRegionTexture.getWidth();
166

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

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

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

    
185
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
186

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

    
192
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
193

    
194
      mCenterEffects.quaternion(mQuatInt1, rotateCen);
195
      mCenterEffects.quaternion(mQuatInt2, rotateCen);
196

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

    
202
      mRegionEffects.quaternion(mQuatInt1, rotateCen);
203
      mRegionEffects.quaternion(mQuatInt2, rotateCen);
204

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

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

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

    
219
      mScreen.resize(width, height);
220
      }
221

    
222
///////////////////////////////////////////////////////////////////////////////////////////////////
223

    
224
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
225
      {
226
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
227

    
228
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
229

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

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

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