Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesRenderer.java @ f6d884d5

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.cubes;
21

    
22
import java.io.IOException;
23
import java.io.InputStream;
24

    
25
import javax.microedition.khronos.egl.EGLConfig;
26
import javax.microedition.khronos.opengles.GL10;
27

    
28
import org.distorted.examples.R;
29

    
30
import org.distorted.library.DistortedTexture;
31
import org.distorted.library.DistortedEffectQueues;
32
import org.distorted.library.GridObject;
33
import org.distorted.library.EffectTypes;
34
import org.distorted.library.type.DynamicQuat;
35
import org.distorted.library.type.Static4D;
36
import org.distorted.library.type.Static3D;
37
import org.distorted.library.Distorted;
38

    
39
import android.graphics.Bitmap;
40
import android.graphics.BitmapFactory;
41
import android.opengl.GLES20;
42
import android.opengl.GLSurfaceView;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
class CubesRenderer implements GLSurfaceView.Renderer 
47
{
48
    private GLSurfaceView mView;
49
    private DistortedTexture mTexture;
50
    private DistortedEffectQueues mQueues;
51
    private GridObject mGrid;
52
    private DynamicQuat mQuatInt1, mQuatInt2;
53
    private int mObjWidth, mObjHeight;
54

    
55
    Static4D mQuat1, mQuat2;
56
    int mScreenMin;
57
    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
    CubesRenderer(GLSurfaceView v)
61
      {
62
      mView = v;
63

    
64
      CubesActivity act = (CubesActivity)v.getContext();
65

    
66
      mQueues  = new DistortedEffectQueues();
67
      mTexture = act.getTexture();
68
      mGrid    = act.getGrid();
69

    
70
      mObjWidth = mTexture.getWidth();
71
      mObjHeight= mTexture.getHeight();
72

    
73
      mQuat1 = new Static4D(0,0,0,1);  // unity
74
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
75
      
76
      mQuatInt1 = new DynamicQuat(0,0.5f);
77
      mQuatInt2 = new DynamicQuat(0,0.5f);
78

    
79
      mQuatInt1.add(mQuat1);
80
      mQuatInt2.add(mQuat2);
81
      }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
   
85
    public void onDrawFrame(GL10 glUnused) 
86
      {
87
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
88
      mQueues.draw(System.currentTimeMillis(),mTexture,mGrid);
89
      }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
    
93
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
94
      {
95
      mScreenMin = width<height ? width:height;
96
    	
97
      mQueues.abortEffects(EffectTypes.MATRIX);
98
      float factor;
99

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

    
109
      mQueues.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
110
      mQueues.scale(factor);
111
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
112

    
113
      mQueues.quaternion(mQuatInt1, center);
114
      mQueues.quaternion(mQuatInt2, center);
115
       
116
      Distorted.onSurfaceChanged(width, height); 
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
    
121
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
122
      {
123
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
124

    
125
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
126
      Bitmap bitmap;
127
        
128
      try 
129
        {
130
        bitmap = BitmapFactory.decodeStream(is);
131
        } 
132
      finally 
133
        {
134
        try 
135
          {
136
          is.close();
137
          } 
138
        catch(IOException e) { }
139
        }  
140
      
141
      mTexture.setTexture(bitmap);
142
      
143
      try
144
        {
145
        Distorted.onSurfaceCreated(mView.getContext());
146
        }
147
      catch(Exception ex)
148
        {
149
        android.util.Log.e("Cubes", ex.getMessage() );
150
        }
151
      }
152
}
(2-2/3)