Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fragment3d / Fragment3DRenderer.java @ edafb4a7

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 float mFactorCen, mFactorObj;
59

    
60
    private int mObjWidth, mObjHeight;
61

    
62
    private DynamicQuat mQuatInt1, mQuatInt2;
63

    
64
    Static4D mQuat1, mQuat2;
65
    int mScreenMin;
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

    
69
    public Fragment3DRenderer(GLSurfaceView v)
70
      {
71
      mView = v;
72

    
73
      mObject = ((Fragment3DActivity)v.getContext()).getObject();
74

    
75
      mObjWidth = mObject.getWidth();
76
      mObjHeight= mObject.getHeight();
77

    
78
      mQuat1 = new Static4D(0,0,0,1);  // unity
79
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
80

    
81
      mQuatInt1 = new DynamicQuat(0,0.5f);
82
      mQuatInt2 = new DynamicQuat(0,0.5f);
83

    
84
      mQuatInt1.add(mQuat1);
85
      mQuatInt2.add(mQuat2);
86
      }
87

    
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
   
90
    public void onDrawFrame(GL10 glUnused) 
91
      {
92
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
93
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
94

    
95
      long time = System.currentTimeMillis();
96

    
97
      mObject.draw(time);
98

    
99
      mCenter = mView.
100

    
101
      mCenter.draw(time);
102
      }
103

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
    
106
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
107
      {
108
      mScreenMin = width<height ? width:height;
109

    
110
      mObject.abortEffects(EffectTypes.MATRIX);
111
      mCenter.abortEffects(EffectTypes.MATRIX);
112

    
113
      int centerSize = mCenter.getWidth();
114

    
115
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
116
        {
117
        mFactorObj = (0.70f*height)/mObjHeight;
118
        mFactorCen = (0.15f*height)/centerSize;
119
        }
120
      else
121
        {
122
        mFactorObj = (0.70f*width)/mObjWidth;
123
        mFactorCen = (0.15f*width)/centerSize;
124
        }
125

    
126

    
127
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
128

    
129
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
130
      mObject.scale(mFactorObj);
131
      mObject.quaternion(mQuatInt1, rotateObj);
132
      mObject.quaternion(mQuatInt2, rotateObj);
133

    
134
      Fragment3DActivity act = (Fragment3DActivity)mView.getContext();
135
      float cX = act.getCenterX();
136
      float cY = act.getCenterY();
137

    
138
      cX = (0.012f*cX-0.1f)*mObjWidth;
139
      cY = (0.012f*cY-0.1f)*mObjHeight;
140

    
141
      mMovePoint.set(cX*mFactorObj,cY*mFactorObj,0);
142

    
143
      Static3D rotateCen = new Static3D(width/2,height/2, 0);
144

    
145
      mCenter.quaternion(mQuatInt1, rotateCen);
146
      mCenter.quaternion(mQuatInt2, rotateCen);
147
      mCenter.move( new Static3D( (width -mFactorCen*centerSize-mFactorObj*mObjWidth )/2 ,
148
                                  (height-mFactorCen*centerSize-mFactorObj*mObjHeight)/2 , 10) );
149
      mCenter.move(mMoveInter);
150
      mCenter.scale(mFactorCen);
151

    
152
      setFragmentEffects();
153

    
154
      Distorted.onSurfaceChanged(width, height); 
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
    
159
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
160
      {
161
      Fragment3DActivity act = (Fragment3DActivity)mView.getContext();
162

    
163
      InputStream is1 = act.getResources().openRawResource(act.getBitmap());
164
      InputStream is2 = act.getResources().openRawResource(R.raw.center);
165

    
166
      Bitmap bitmap1,bitmap2;
167
        
168
      try 
169
        {
170
        bitmap1 = BitmapFactory.decodeStream(is1);
171
        bitmap2 = BitmapFactory.decodeStream(is2);
172
        }
173
      finally 
174
        {
175
        try 
176
          {
177
          is1.close();
178
          is2.close();
179
          } 
180
        catch(IOException e) { }
181
        }  
182
      
183
      mObject.setBitmap(bitmap1);
184
      mCenter.setBitmap(bitmap2);
185

    
186
      try
187
        {
188
        Distorted.onSurfaceCreated(mView.getContext());
189
        }
190
      catch(Exception ex)
191
        {
192
        android.util.Log.e("Fragment3D", ex.getMessage() );
193
        }
194
      }
195
}
(3-3/4)