Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesRenderer.java @ 10b7e588

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 261fe5bd Leszek Koltunski
import org.distorted.library.DistortedObject;
31 10b7e588 Leszek Koltunski
import org.distorted.library.GridObject;
32 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
33 7589635e Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
34
import org.distorted.library.type.Static4D;
35
import org.distorted.library.type.Static3D;
36 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
37 427ab7bf Leszek Koltunski
38
import android.graphics.Bitmap;
39
import android.graphics.BitmapFactory;
40
import android.opengl.GLES20;
41
import android.opengl.GLSurfaceView;
42
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44
45
class CubesRenderer implements GLSurfaceView.Renderer 
46
{
47
    private GLSurfaceView mView;
48 261fe5bd Leszek Koltunski
    private DistortedObject mObject;
49 10b7e588 Leszek Koltunski
    private GridObject mGrid;
50 261fe5bd Leszek Koltunski
    private int mObjWidth, mObjHeight;
51
52 7589635e Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
53 427ab7bf Leszek Koltunski
    
54 7589635e Leszek Koltunski
    Static4D mQuat1, mQuat2;
55 427ab7bf Leszek Koltunski
    int mScreenMin;
56
    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
59 2b261a18 Leszek Koltunski
    CubesRenderer(GLSurfaceView v)
60 427ab7bf Leszek Koltunski
      {
61
      mView = v;
62 261fe5bd Leszek Koltunski
63 e8b6aa95 Leszek Koltunski
      CubesActivity act = (CubesActivity)v.getContext();
64
65
      mObject = act.getObject();
66
      mGrid   = act.getGrid();
67 261fe5bd Leszek Koltunski
68
      mObjWidth = mObject.getWidth();
69
      mObjHeight= mObject.getHeight();
70
71 7589635e Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
72
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
73 427ab7bf Leszek Koltunski
      
74 833685d0 Leszek Koltunski
      mQuatInt1 = new DynamicQuat(0,0.5f);
75
      mQuatInt2 = new DynamicQuat(0,0.5f);
76
77 427ab7bf Leszek Koltunski
      mQuatInt1.add(mQuat1);
78
      mQuatInt2.add(mQuat2);
79
      }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
   
83
    public void onDrawFrame(GL10 glUnused) 
84
      {
85
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
86 e8b6aa95 Leszek Koltunski
      mObject.draw(System.currentTimeMillis(),mGrid);
87 427ab7bf Leszek Koltunski
      }
88
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
    
91
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
92
      {
93
      mScreenMin = width<height ? width:height;
94
    	
95 261fe5bd Leszek Koltunski
      mObject.abortEffects(EffectTypes.MATRIX);
96
      float factor;
97 427ab7bf Leszek Koltunski
98 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
99 427ab7bf Leszek Koltunski
        {
100 261fe5bd Leszek Koltunski
        factor = (0.8f*height)/mObjHeight;
101
        }
102 427ab7bf Leszek Koltunski
      else
103 261fe5bd Leszek Koltunski
        {
104
        factor = (0.8f*width)/mObjWidth;
105 427ab7bf Leszek Koltunski
        }
106 261fe5bd Leszek Koltunski
107
      mObject.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
108
      mObject.scale(factor);
109
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
110
111
      mObject.quaternion(mQuatInt1, center);
112
      mObject.quaternion(mQuatInt2, center);
113 427ab7bf Leszek Koltunski
       
114
      Distorted.onSurfaceChanged(width, height); 
115
      }
116
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
120
      {
121 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
122
123 427ab7bf Leszek Koltunski
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
124
      Bitmap bitmap;
125
        
126
      try 
127
        {
128
        bitmap = BitmapFactory.decodeStream(is);
129
        } 
130
      finally 
131
        {
132
        try 
133
          {
134
          is.close();
135
          } 
136
        catch(IOException e) { }
137
        }  
138
      
139 e8b6aa95 Leszek Koltunski
      mObject.setTexture(bitmap);
140 427ab7bf Leszek Koltunski
      
141
      try
142
        {
143 ed1c0b33 Leszek Koltunski
        Distorted.onSurfaceCreated(mView.getContext());
144 427ab7bf Leszek Koltunski
        }
145
      catch(Exception ex)
146
        {
147
        android.util.Log.e("Cubes", ex.getMessage() );
148
        }
149
      }
150
}