Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fragment3d / Fragment3DRenderer.java @ 56e4be07

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.fragment3d;
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.DistortedBitmap;
30
import org.distorted.library.DistortedCubes;
31
import org.distorted.library.DistortedObject;
32
import org.distorted.library.EffectNames;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Dynamic2D;
36
import org.distorted.library.type.Dynamic3D;
37
import org.distorted.library.type.DynamicQuat;
38
import org.distorted.library.type.Static1D;
39
import org.distorted.library.type.Static2D;
40
import org.distorted.library.type.Static3D;
41
import org.distorted.library.type.Static4D;
42

    
43
import java.io.IOException;
44
import java.io.InputStream;
45

    
46
import javax.microedition.khronos.egl.EGLConfig;
47
import javax.microedition.khronos.opengles.GL10;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
class Fragment3DRenderer implements GLSurfaceView.Renderer
52
{
53
    private static final int SIZE = 100;
54

    
55
    private GLSurfaceView mView;
56
    private DistortedObject mObject;
57
    private DistortedBitmap mCenter;
58
    private DistortedBitmap mBackground;
59
    private float mFactorCen, mFactorObj;
60

    
61
    private int mObjWidth, mObjHeight;
62

    
63
    private DynamicQuat mQuatInt1, mQuatInt2;
64

    
65
    private Dynamic3D mMoveInter;
66
    private Static3D mMovePoint;
67

    
68
    Static4D mQuat1, mQuat2;
69
    int mScreenMin;
70

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

    
73
    public void setCenter(float x, float y)
74
      {
75
      x = (0.012f*x-0.1f)*mObjWidth;
76
      y = (0.012f*y-0.1f)*mObjHeight;
77

    
78
      mMovePoint.set(mFactorObj*x,mFactorObj*y,0);
79
      }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
    public Fragment3DRenderer(GLSurfaceView v)
84
      {
85
      mView = v;
86

    
87
      mObject     = ((Fragment3DActivity)v.getContext()).getObject();
88
      mCenter     = new DistortedBitmap(SIZE, SIZE, 1);
89
      mBackground = new DistortedBitmap(SIZE, SIZE, 1);
90

    
91
      mObjWidth = mObject.getWidth();
92
      mObjHeight= mObject.getHeight();
93

    
94
      mMovePoint= new Static3D(0,0,0);
95
      mMoveInter= new Dynamic3D();
96
      mMoveInter.add(mMovePoint);
97

    
98
      mQuat1 = new Static4D(0,0,0,1);  // unity
99
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
100

    
101
      mQuatInt1 = new DynamicQuat(0,0.5f);
102
      mQuatInt2 = new DynamicQuat(0,0.5f);
103

    
104
      mQuatInt1.add(mQuat1);
105
      mQuatInt2.add(mQuat2);
106
      }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
   
110
    public void onDrawFrame(GL10 glUnused) 
111
      {
112
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
113
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
114

    
115
      long time = System.currentTimeMillis();
116

    
117
      mBackground.draw(time);
118
      mObject.draw(time);
119
      mCenter.draw(time);
120
      }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
    
124
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
125
      {
126
      mScreenMin = width<height ? width:height;
127

    
128
      mObject.abortEffects(EffectTypes.MATRIX);
129
      mCenter.abortEffects(EffectTypes.MATRIX);
130
      mBackground.abortEffects(EffectTypes.MATRIX);
131

    
132
      int centerSize = mCenter.getWidth();
133

    
134
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
135
        {
136
        mFactorObj = (0.70f*height)/mObjHeight;
137
        mFactorCen = (0.15f*height)/centerSize;
138
        }
139
      else
140
        {
141
        mFactorObj = (0.70f*width)/mObjWidth;
142
        mFactorCen = (0.15f*width)/centerSize;
143
        }
144

    
145
      Fragment3DActivity act = (Fragment3DActivity)mView.getContext();
146
      float cX = act.getCenterX();
147
      float cY = act.getCenterY();
148

    
149
      cX = (0.012f*cX-0.1f)*mObjWidth;
150
      cY = (0.012f*cY-0.1f)*mObjHeight;
151

    
152
      mMovePoint.set(cX*mFactorObj,cY*mFactorObj,0);
153

    
154
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
155

    
156
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
157
      mObject.scale(mFactorObj);
158
      mObject.quaternion(mQuatInt1, rotateObj);
159
      mObject.quaternion(mQuatInt2, rotateObj);
160

    
161
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
162

    
163
      mCenter.quaternion(mQuatInt1, rotateCen);
164
      mCenter.quaternion(mQuatInt2, rotateCen);
165
      mCenter.move( new Static3D( (width -mFactorCen*centerSize-mFactorObj*mObjWidth )/2 ,
166
                                  (height-mFactorCen*centerSize-mFactorObj*mObjHeight)/2 , 10) );
167
      mCenter.move(mMoveInter);
168
      mCenter.scale(mFactorCen);
169

    
170
      int backgroundSize = mBackground.getWidth();
171
      float factorBackX = ((float)width)/backgroundSize;
172
      float factorBackY = ((float)height)/backgroundSize;
173

    
174
      mBackground.move(new Static3D( -width/2, -height/2,-mFactorObj*(mObjWidth+mObjHeight)/2) );
175
      mBackground.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
176

    
177
      Distorted.onSurfaceChanged(width, height); 
178
      }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
    
182
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
183
      {
184
      Fragment3DActivity act = (Fragment3DActivity)mView.getContext();
185

    
186
      InputStream is1 = act.getResources().openRawResource(act.getBitmap());
187
      InputStream is2 = act.getResources().openRawResource(R.raw.center);
188
      InputStream is3 = act.getResources().openRawResource(R.raw.water);
189

    
190
      Bitmap bitmap1,bitmap2,bitmap3;
191
        
192
      try 
193
        {
194
        bitmap1 = BitmapFactory.decodeStream(is1);
195
        bitmap2 = BitmapFactory.decodeStream(is2);
196
        bitmap3 = BitmapFactory.decodeStream(is3);
197
        }
198
      finally 
199
        {
200
        try 
201
          {
202
          is1.close();
203
          is2.close();
204
          is3.close();
205
          } 
206
        catch(IOException e) { }
207
        }  
208
      
209
      mObject.setBitmap(bitmap1);
210
      mCenter.setBitmap(bitmap2);
211
      mBackground.setBitmap(bitmap3)
212
      ;
213
      try
214
        {
215
        Distorted.onSurfaceCreated(mView.getContext());
216
        }
217
      catch(Exception ex)
218
        {
219
        android.util.Log.e("Fragment3D", ex.getMessage() );
220
        }
221
      }
222
}
(3-3/4)