Project

General

Profile

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

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

1 bc0a685b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 427ab7bf Leszek Koltunski
20 5068fa06 Leszek Koltunski
package org.distorted.examples.cubes;
21 427ab7bf Leszek Koltunski
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 5068fa06 Leszek Koltunski
import org.distorted.examples.R;
29 427ab7bf Leszek Koltunski
30 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
31
import org.distorted.library.DistortedEffectQueues;
32 10b7e588 Leszek Koltunski
import org.distorted.library.GridObject;
33 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
34 7589635e Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
35
import org.distorted.library.type.Static4D;
36
import org.distorted.library.type.Static3D;
37 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
38 427ab7bf Leszek Koltunski
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 f6d884d5 Leszek Koltunski
    private DistortedTexture mTexture;
50
    private DistortedEffectQueues mQueues;
51 10b7e588 Leszek Koltunski
    private GridObject mGrid;
52 f6d884d5 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
53 261fe5bd Leszek Koltunski
    private int mObjWidth, mObjHeight;
54
55 7589635e Leszek Koltunski
    Static4D mQuat1, mQuat2;
56 427ab7bf Leszek Koltunski
    int mScreenMin;
57
    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 2b261a18 Leszek Koltunski
    CubesRenderer(GLSurfaceView v)
61 427ab7bf Leszek Koltunski
      {
62
      mView = v;
63 261fe5bd Leszek Koltunski
64 e8b6aa95 Leszek Koltunski
      CubesActivity act = (CubesActivity)v.getContext();
65
66 f6d884d5 Leszek Koltunski
      mQueues  = new DistortedEffectQueues();
67
      mTexture = act.getTexture();
68
      mGrid    = act.getGrid();
69 261fe5bd Leszek Koltunski
70 f6d884d5 Leszek Koltunski
      mObjWidth = mTexture.getWidth();
71
      mObjHeight= mTexture.getHeight();
72 261fe5bd Leszek Koltunski
73 7589635e Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
74
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
75 427ab7bf Leszek Koltunski
      
76 833685d0 Leszek Koltunski
      mQuatInt1 = new DynamicQuat(0,0.5f);
77
      mQuatInt2 = new DynamicQuat(0,0.5f);
78
79 427ab7bf Leszek Koltunski
      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 f6d884d5 Leszek Koltunski
      mQueues.draw(System.currentTimeMillis(),mTexture,mGrid);
89 427ab7bf Leszek Koltunski
      }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
    
93
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
94
      {
95
      mScreenMin = width<height ? width:height;
96
    	
97 f6d884d5 Leszek Koltunski
      mQueues.abortEffects(EffectTypes.MATRIX);
98 261fe5bd Leszek Koltunski
      float factor;
99 427ab7bf Leszek Koltunski
100 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
101 427ab7bf Leszek Koltunski
        {
102 261fe5bd Leszek Koltunski
        factor = (0.8f*height)/mObjHeight;
103
        }
104 427ab7bf Leszek Koltunski
      else
105 261fe5bd Leszek Koltunski
        {
106
        factor = (0.8f*width)/mObjWidth;
107 427ab7bf Leszek Koltunski
        }
108 261fe5bd Leszek Koltunski
109 f6d884d5 Leszek Koltunski
      mQueues.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
110
      mQueues.scale(factor);
111 261fe5bd Leszek Koltunski
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
112
113 f6d884d5 Leszek Koltunski
      mQueues.quaternion(mQuatInt1, center);
114
      mQueues.quaternion(mQuatInt2, center);
115 427ab7bf Leszek Koltunski
       
116
      Distorted.onSurfaceChanged(width, height); 
117
      }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
    
121
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
122
      {
123 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
124
125 427ab7bf Leszek Koltunski
      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 f6d884d5 Leszek Koltunski
      mTexture.setTexture(bitmap);
142 427ab7bf Leszek Koltunski
      
143
      try
144
        {
145 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
146 427ab7bf Leszek Koltunski
        }
147
      catch(Exception ex)
148
        {
149
        android.util.Log.e("Cubes", ex.getMessage() );
150
        }
151
      }
152
}