Project

General

Profile

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

examples / src / main / java / org / distorted / examples / effects3d / Effects3DRenderer.java @ 4d5b37fe

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
import android.opengl.GLES20;
25
import android.opengl.GLSurfaceView;
26
27
import org.distorted.examples.R;
28
import org.distorted.library.Distorted;
29 794e6c4f Leszek Koltunski
import org.distorted.library.DistortedBitmap;
30 261fe5bd Leszek Koltunski
import org.distorted.library.DistortedObject;
31 08f92d82 Leszek Koltunski
import org.distorted.library.EffectTypes;
32 950511ed Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
33 833685d0 Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
34 08f92d82 Leszek Koltunski
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36
37
import java.io.IOException;
38
import java.io.InputStream;
39
40
import javax.microedition.khronos.egl.EGLConfig;
41
import javax.microedition.khronos.opengles.GL10;
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45 76a81b6a Leszek Koltunski
class Effects3DRenderer implements GLSurfaceView.Renderer
46 08f92d82 Leszek Koltunski
{
47
    private GLSurfaceView mView;
48 d2337a3a Leszek Koltunski
    private DistortedObject mObject;
49 56cbe1cf Leszek Koltunski
    private DistortedBitmap mBackground;
50 950511ed Leszek Koltunski
    private DistortedBitmap mCenter;
51 6f779cd4 Leszek Koltunski
    private DistortedBitmap mRegion;
52 8ff32d4d Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
53 833685d0 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
54 950511ed Leszek Koltunski
55 24991bc2 Leszek Koltunski
    private Dynamic3D mCenterInter, mRegionInter;
56
    private Static3D mCenterPoint, mRegionPoint;
57 6f779cd4 Leszek Koltunski
    private Dynamic3D mRegionScaleInter;
58
    private Static3D mRegionScalePoint;
59
60
    private float mFactorObj, mFactorReg;
61 950511ed Leszek Koltunski
62 833685d0 Leszek Koltunski
    Static4D mQuat1, mQuat2;
63 d2337a3a Leszek Koltunski
    int mScreenMin;
64 833685d0 Leszek Koltunski
65 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67 4f8ca8ff Leszek Koltunski
    Effects3DRenderer(GLSurfaceView v)
68 08f92d82 Leszek Koltunski
      {
69
      mView = v;
70 d40cfeb2 Leszek Koltunski
71 76a81b6a Leszek Koltunski
      mObject     = ((Effects3DActivity)v.getContext()).getObject();
72 56cbe1cf Leszek Koltunski
      mBackground = new DistortedBitmap(100, 100, 1);
73 950511ed Leszek Koltunski
      mCenter     = new DistortedBitmap(100, 100, 1);
74 6f779cd4 Leszek Koltunski
      mRegion     = new DistortedBitmap(100, 100, 1);
75 d40cfeb2 Leszek Koltunski
76 261fe5bd Leszek Koltunski
      mObjWidth = mObject.getWidth();
77
      mObjHeight= mObject.getHeight();
78 8ff32d4d Leszek Koltunski
      mObjDepth = mObject.getDepth();
79 d40cfeb2 Leszek Koltunski
80 833685d0 Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
81
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
82
83
      mQuatInt1 = new DynamicQuat(0,0.5f);
84
      mQuatInt2 = new DynamicQuat(0,0.5f);
85
86
      mQuatInt1.add(mQuat1);
87
      mQuatInt2.add(mQuat2);
88 950511ed Leszek Koltunski
89 24991bc2 Leszek Koltunski
      mCenterInter= new Dynamic3D();
90
      mCenterPoint= new Static3D(0,0,0);
91
      mCenterInter.add(mCenterPoint);
92
93
      mRegionInter= new Dynamic3D();
94
      mRegionPoint= new Static3D(0,0,0);
95
      mRegionInter.add(mRegionPoint);
96 6f779cd4 Leszek Koltunski
97
      mRegionScaleInter = new Dynamic3D();
98
      mRegionScalePoint = new Static3D(0,0,0);
99
      mRegionScaleInter.add(mRegionScalePoint);
100 950511ed Leszek Koltunski
      }
101
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104 4f8ca8ff Leszek Koltunski
    void setCenter(float x, float y)
105 950511ed Leszek Koltunski
      {
106 24991bc2 Leszek Koltunski
      mCenterPoint.set(mFactorObj*x,mFactorObj*y,0);
107 08f92d82 Leszek Koltunski
      }
108
109 6f779cd4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111 4f8ca8ff Leszek Koltunski
    void setRegion(float x, float y, float r)
112 6f779cd4 Leszek Koltunski
      {
113 17600407 Leszek Koltunski
      mFactorReg = 2*mFactorObj*r/mRegion.getWidth();
114 24991bc2 Leszek Koltunski
      mRegionPoint.set(mFactorObj*x,mFactorObj*y,0);
115 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
116 6f779cd4 Leszek Koltunski
      }
117
118 08f92d82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
119 56cbe1cf Leszek Koltunski
120
    public void onDrawFrame(GL10 glUnused)
121 08f92d82 Leszek Koltunski
      {
122
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
123 71d8ad03 Leszek Koltunski
124
      long time = System.currentTimeMillis();
125
126 56cbe1cf Leszek Koltunski
      mBackground.draw(time);
127 71d8ad03 Leszek Koltunski
      mObject.draw(time);
128 24991bc2 Leszek Koltunski
129 4d5b37fe Leszek Koltunski
      if( Effects3DActivity.supportsCenter() )
130 76a81b6a Leszek Koltunski
        {
131
        mCenter.draw(time);
132
        if( Effects3DActivity.supportsRegion() ) mRegion.draw(time);
133
        }
134 08f92d82 Leszek Koltunski
      }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137 56cbe1cf Leszek Koltunski
138
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
139 08f92d82 Leszek Koltunski
      {
140 833685d0 Leszek Koltunski
      mScreenMin = width<height ? width:height;
141
142 261fe5bd Leszek Koltunski
      mObject.abortEffects(EffectTypes.MATRIX);
143 56cbe1cf Leszek Koltunski
      mBackground.abortEffects(EffectTypes.MATRIX);
144 950511ed Leszek Koltunski
      mCenter.abortEffects(EffectTypes.MATRIX);
145 6f779cd4 Leszek Koltunski
      mRegion.abortEffects(EffectTypes.MATRIX);
146 950511ed Leszek Koltunski
147
      float factorCen;
148
      int centerSize = mCenter.getWidth();
149 6f779cd4 Leszek Koltunski
      int regionSize = mRegion.getWidth();
150
151 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
152 d40cfeb2 Leszek Koltunski
        {
153 950511ed Leszek Koltunski
        mFactorObj = (0.80f*height)/mObjHeight;
154
        factorCen  = (0.08f*height)/centerSize;
155 261fe5bd Leszek Koltunski
        }
156 d40cfeb2 Leszek Koltunski
      else
157
        {
158 950511ed Leszek Koltunski
        mFactorObj = (0.80f*width)/mObjWidth;
159
        factorCen  = (0.08f*width)/centerSize;
160 d40cfeb2 Leszek Koltunski
        }
161
162 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
163 24991bc2 Leszek Koltunski
      mCenterPoint.set(mFactorObj*act.getCenterX(),mFactorObj*act.getCenterY(),0);
164
      mRegionPoint.set(mFactorObj*act.getRegionX(),mFactorObj*act.getRegionY(),0);
165 9ae05f6c Leszek Koltunski
166 17600407 Leszek Koltunski
      mFactorReg = 2*mFactorObj*act.getRegionR()/regionSize;
167 9ae05f6c Leszek Koltunski
      mRegionScalePoint.set(mFactorReg,mFactorReg,mFactorReg);
168 950511ed Leszek Koltunski
169 2bf60c29 Leszek Koltunski
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
170
171 950511ed Leszek Koltunski
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
172
      mObject.scale(mFactorObj);
173 2bf60c29 Leszek Koltunski
      mObject.quaternion(mQuatInt1, rotateObj);
174
      mObject.quaternion(mQuatInt2, rotateObj);
175 833685d0 Leszek Koltunski
176 950511ed Leszek Koltunski
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
177
178
      mCenter.quaternion(mQuatInt1, rotateCen);
179
      mCenter.quaternion(mQuatInt2, rotateCen);
180
181
      mCenter.move( new Static3D( (width -factorCen*centerSize-mFactorObj*mObjWidth )/2 ,
182 8ff32d4d Leszek Koltunski
                                  (height-factorCen*centerSize-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+10) );
183 24991bc2 Leszek Koltunski
      mCenter.move(mCenterInter);
184 950511ed Leszek Koltunski
      mCenter.scale(factorCen);
185
186 6f779cd4 Leszek Koltunski
      mRegion.quaternion(mQuatInt1, rotateCen);
187
      mRegion.quaternion(mQuatInt2, rotateCen);
188
189 9ae05f6c Leszek Koltunski
      mRegion.move( new Static3D( (width -mFactorObj*mObjWidth )/2 ,
190 8ff32d4d Leszek Koltunski
                                  (height-mFactorObj*mObjHeight)/2 , mFactorObj*mObjDepth/2+12) );
191 24991bc2 Leszek Koltunski
      mRegion.move(mCenterInter);
192
      mRegion.move(mRegionInter);
193 6f779cd4 Leszek Koltunski
      mRegion.scale(mRegionScaleInter);
194 9ae05f6c Leszek Koltunski
      mRegion.move( new Static3D( -regionSize/2 , -regionSize/2 , 0) );
195 6f779cd4 Leszek Koltunski
196 56cbe1cf Leszek Koltunski
      int backgroundSize = mBackground.getWidth();
197
      float factorBackX = ((float)width)/backgroundSize;
198
      float factorBackY = ((float)height)/backgroundSize;
199 833685d0 Leszek Koltunski
200 950511ed Leszek Koltunski
      mBackground.move(new Static3D( -width/2, -height/2,-mFactorObj*(mObjWidth+mObjHeight)/2) );
201 56cbe1cf Leszek Koltunski
      mBackground.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
202 71d8ad03 Leszek Koltunski
203 56cbe1cf Leszek Koltunski
      Distorted.onSurfaceChanged(width, height);
204 08f92d82 Leszek Koltunski
      }
205
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207 56cbe1cf Leszek Koltunski
208
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
209 08f92d82 Leszek Koltunski
      {
210 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
211
212 76a81b6a Leszek Koltunski
      Effects3DActivity act = (Effects3DActivity)mView.getContext();
213 bcc8e016 Leszek Koltunski
214 9167cfd4 Leszek Koltunski
      InputStream is1 = act.getResources().openRawResource(R.raw.water);
215
      InputStream is2 = act.getResources().openRawResource(R.raw.center);
216
      InputStream is3 = act.getResources().openRawResource(R.raw.region);
217 71d8ad03 Leszek Koltunski
218 9167cfd4 Leszek Koltunski
      Bitmap bitmap1,bitmap2,bitmap3;
219 08f92d82 Leszek Koltunski
        
220
      try 
221
        {
222 71d8ad03 Leszek Koltunski
        bitmap1 = BitmapFactory.decodeStream(is1);
223
        bitmap2 = BitmapFactory.decodeStream(is2);
224 950511ed Leszek Koltunski
        bitmap3 = BitmapFactory.decodeStream(is3);
225 71d8ad03 Leszek Koltunski
        }
226 08f92d82 Leszek Koltunski
      finally 
227
        {
228
        try 
229
          {
230 71d8ad03 Leszek Koltunski
          is1.close();
231
          is2.close();
232 950511ed Leszek Koltunski
          is3.close();
233 9167cfd4 Leszek Koltunski
          }
234 08f92d82 Leszek Koltunski
        catch(IOException e) { }
235
        }  
236
      
237 9167cfd4 Leszek Koltunski
      mObject.setBitmap( act.getBitmap() );
238
      mBackground.setBitmap(bitmap1);
239
      mCenter.setBitmap(bitmap2);
240
      mRegion.setBitmap(bitmap3);
241 950511ed Leszek Koltunski
242 08f92d82 Leszek Koltunski
      try
243
        {
244
        Distorted.onSurfaceCreated(mView.getContext());
245
        }
246
      catch(Exception ex)
247
        {
248 76a81b6a Leszek Koltunski
        android.util.Log.e("Effects3D", ex.getMessage() );
249 08f92d82 Leszek Koltunski
        }
250
      }
251 56cbe1cf Leszek Koltunski
}