Project

General

Profile

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

examples / src / main / java / org / distorted / examples / fragment3d / Fragment3DRenderer.java @ 75c7def3

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 mBackground;
58
    private float 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
      mBackground = new DistortedBitmap(SIZE, SIZE, 1);
75

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

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

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

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

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

    
96
      long time = System.currentTimeMillis();
97

    
98
      mBackground.draw(time);
99
      mObject.draw(time);
100
      }
101

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

    
108
      mObject.abortEffects(EffectTypes.MATRIX);
109
      mBackground.abortEffects(EffectTypes.MATRIX);
110

    
111
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
112
        {
113
        mFactorObj = (0.70f*height)/mObjHeight;
114
        }
115
      else
116
        {
117
        mFactorObj = (0.70f*width)/mObjWidth;
118
        }
119

    
120
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
121

    
122
      mObject.move( new Static3D( (width-mFactorObj*mObjWidth)/2 , (height-mFactorObj*mObjHeight)/2 , 0) );
123
      mObject.scale(mFactorObj);
124
      mObject.quaternion(mQuatInt1, rotateObj);
125
      mObject.quaternion(mQuatInt2, rotateObj);
126

    
127
      int backgroundSize = mBackground.getWidth();
128
      float factorBackX = ((float)width)/backgroundSize;
129
      float factorBackY = ((float)height)/backgroundSize;
130

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

    
134
      Distorted.onSurfaceChanged(width, height); 
135
      }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
    
139
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
140
      {
141
      Fragment3DActivity act = (Fragment3DActivity)mView.getContext();
142

    
143
      InputStream is1 = act.getResources().openRawResource(act.getBitmap());
144
      InputStream is2 = act.getResources().openRawResource(R.raw.water);
145

    
146
      Bitmap bitmap1,bitmap2;
147
        
148
      try 
149
        {
150
        bitmap1 = BitmapFactory.decodeStream(is1);
151
        bitmap2 = BitmapFactory.decodeStream(is2);
152
        }
153
      finally 
154
        {
155
        try 
156
          {
157
          is1.close();
158
          is2.close();
159
          } 
160
        catch(IOException e) { }
161
        }  
162
      
163
      mObject.setBitmap(bitmap1);
164
      mBackground.setBitmap(bitmap2)
165
      ;
166
      try
167
        {
168
        Distorted.onSurfaceCreated(mView.getContext());
169
        }
170
      catch(Exception ex)
171
        {
172
        android.util.Log.e("Fragment3D", ex.getMessage() );
173
        }
174
      }
175
}
(3-3/4)