Project

General

Profile

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

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

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

    
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
    private DistortedObject mObject;
49
    private GridObject mGrid;
50
    private int mObjWidth, mObjHeight;
51

    
52
    private DynamicQuat mQuatInt1, mQuatInt2;
53
    
54
    Static4D mQuat1, mQuat2;
55
    int mScreenMin;
56
    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

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

    
65
      mObject = act.getObject();
66
      mGrid   = act.getGrid();
67

    
68
      mObjWidth = mObject.getWidth();
69
      mObjHeight= mObject.getHeight();
70

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

    
77
      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
      mObject.draw(System.currentTimeMillis(),mGrid);
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
      float factor;
97

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

    
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
       
114
      Distorted.onSurfaceChanged(width, height); 
115
      }
116

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

    
123
      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
      mObject.setTexture(bitmap);
140
      
141
      try
142
        {
143
        Distorted.onSurfaceCreated(mView.getContext());
144
        }
145
      catch(Exception ex)
146
        {
147
        android.util.Log.e("Cubes", ex.getMessage() );
148
        }
149
      }
150
}
(2-2/3)