Project

General

Profile

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

examples / src / main / java / org / distorted / examples / vertex3d / Vertex3DRenderer.java @ 56cbe1cf

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.vertex3d;
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.DistortedObject;
31
import org.distorted.library.EffectTypes;
32
import org.distorted.library.type.DynamicQuat;
33
import org.distorted.library.type.Static3D;
34
import org.distorted.library.type.Static4D;
35

    
36
import java.io.IOException;
37
import java.io.InputStream;
38

    
39
import javax.microedition.khronos.egl.EGLConfig;
40
import javax.microedition.khronos.opengles.GL10;
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
class Vertex3DRenderer implements GLSurfaceView.Renderer
45
{
46
    private GLSurfaceView mView;
47
    private DistortedObject mObject;
48
    private DistortedBitmap mBackground;
49
    private int mObjWidth, mObjHeight;
50
    private DynamicQuat mQuatInt1, mQuatInt2;
51
    Static4D mQuat1, mQuat2;
52
    int mScreenMin;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
    public Vertex3DRenderer(GLSurfaceView v)
57
      {
58
      mView = v;
59

    
60
      mObject     = ((Vertex3DActivity)v.getContext()).getObject();
61
      mBackground = new DistortedBitmap(100, 100, 1);
62

    
63
      mObjWidth = mObject.getWidth();
64
      mObjHeight= mObject.getHeight();
65

    
66
      mQuat1 = new Static4D(0,0,0,1);  // unity
67
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
68

    
69
      mQuatInt1 = new DynamicQuat(0,0.5f);
70
      mQuatInt2 = new DynamicQuat(0,0.5f);
71

    
72
      mQuatInt1.add(mQuat1);
73
      mQuatInt2.add(mQuat2);
74
      }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
    public void onDrawFrame(GL10 glUnused)
79
      {
80
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
81
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
82

    
83
      long time = System.currentTimeMillis();
84

    
85
      mBackground.draw(time);
86
      mObject.draw(time);
87
      }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
    public void onSurfaceChanged(GL10 glUnused, int width, int height)
92
      {
93
      mScreenMin = width<height ? width:height;
94

    
95
      mObject.abortEffects(EffectTypes.MATRIX);
96
      mBackground.abortEffects(EffectTypes.MATRIX);
97
      float factorObj;
98

    
99
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
100
        {
101
        factorObj = (0.80f*height)/mObjHeight;
102
        }
103
      else
104
        {
105
        factorObj = (0.80f*width)/mObjWidth;
106
        }
107

    
108
      Static3D rotateObj = new Static3D(mObjWidth/2,mObjHeight/2, 0);
109

    
110
      mObject.move( new Static3D( (width-factorObj*mObjWidth)/2 , (height-factorObj*mObjHeight)/2 , 0) );
111
      mObject.scale(factorObj);
112
      mObject.quaternion(mQuatInt1, rotateObj);
113
      mObject.quaternion(mQuatInt2, rotateObj);
114

    
115
      int backgroundSize = mBackground.getWidth();
116
      float factorBackX = ((float)width)/backgroundSize;
117
      float factorBackY = ((float)height)/backgroundSize;
118

    
119
      mBackground.move(new Static3D( -width/2, -height/2,-factorObj*(mObjWidth+mObjHeight)/2) );
120
      mBackground.scale(new Static3D(2*factorBackX, 2*factorBackY, 1.0f) );
121

    
122
      Distorted.onSurfaceChanged(width, height);
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
128
      {
129
      Vertex3DActivity act = (Vertex3DActivity)mView.getContext();
130

    
131
      InputStream is1 = act.getResources().openRawResource(act.getBitmap());
132
      InputStream is2 = act.getResources().openRawResource(R.raw.water);
133

    
134
      Bitmap bitmap1,bitmap2;
135
        
136
      try 
137
        {
138
        bitmap1 = BitmapFactory.decodeStream(is1);
139
        bitmap2 = BitmapFactory.decodeStream(is2);
140
        }
141
      finally 
142
        {
143
        try 
144
          {
145
          is1.close();
146
          is2.close();
147
          } 
148
        catch(IOException e) { }
149
        }  
150
      
151
      mObject.setBitmap(bitmap1);
152
      mBackground.setBitmap(bitmap2)
153
      ;
154
      try
155
        {
156
        Distorted.onSurfaceCreated(mView.getContext());
157
        }
158
      catch(Exception ex)
159
        {
160
        android.util.Log.e("Vertex3D", ex.getMessage() );
161
        }
162
      }
163
}
(3-3/4)