Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesRenderer.java @ 833685d0

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

    
37
import android.graphics.Bitmap;
38
import android.graphics.BitmapFactory;
39
import android.opengl.GLES20;
40
import android.opengl.GLSurfaceView;
41

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

    
44
class CubesRenderer implements GLSurfaceView.Renderer 
45
{
46
    private static final int SIZE = 100;
47
	
48
    private int mCols, mRows;
49
	
50
    private GLSurfaceView mView;
51
    private DistortedCubes mCubes;
52
    private DynamicQuat mQuatInt1, mQuatInt2;
53
    
54
    Static4D mQuat1, mQuat2;
55
    int mScreenMin;
56
    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

    
59
    public CubesRenderer(GLSurfaceView v) 
60
      {
61
      mView = v;
62
      
63
      String shape = CubesActivity.getShape();
64
      
65
      mCols = CubesActivity.getCols();
66
      mRows = shape.length() / mCols;
67
      
68
      mCubes = new DistortedCubes( mCols, shape, SIZE);
69
      
70
      mQuat1 = new Static4D(0,0,0,1);  // unity
71
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
72
      
73
      mQuatInt1 = new DynamicQuat(0,0.5f);
74
      mQuatInt2 = new DynamicQuat(0,0.5f);
75

    
76
      mQuatInt1.add(mQuat1);
77
      mQuatInt2.add(mQuat2);
78
      }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
   
82
    public void onDrawFrame(GL10 glUnused) 
83
      {
84
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
85
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
86
      
87
      mCubes.draw(System.currentTimeMillis());
88
      }
89

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

    
98
      if( mRows/mCols > height/width )
99
        {
100
        int w = (height*mRows)/mCols;
101
        float factor = (float)height/(mRows*SIZE);
102

    
103
        mCubes.move( new Static3D((width-w)/2,0,0) );
104
        mCubes.scale(factor);
105
        }  
106
      else
107
        {   
108
        int h = (width*mRows)/mCols;
109
        float factor = (float)width/(mCols*SIZE);
110

    
111
        mCubes.move( new Static3D(0,(height-h)/2,0) );
112
        mCubes.scale(factor);
113
        }
114
    
115
      Static3D center = new Static3D(mCols*SIZE/2,mRows*SIZE/2, 0);
116
      
117
      mCubes.quaternion(mQuatInt1, center);
118
      mCubes.quaternion(mQuatInt2, center);
119
       
120
      Distorted.onSurfaceChanged(width, height); 
121
      }
122

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