Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesRenderer.java @ 76f9798b

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

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
class CubesRenderer implements GLSurfaceView.Renderer 
48
{
49
    private GLSurfaceView mView;
50
    private DistortedTexture mTexture;
51
    private DistortedEffects mEffects;
52
    private GridObject mGrid;
53
    private DistortedFramebuffer mScreen;
54
    private DynamicQuat mQuatInt1, mQuatInt2;
55
    private int mObjWidth, mObjHeight;
56

    
57
    Static4D mQuat1, mQuat2;
58
    int mScreenMin;
59
    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
    CubesRenderer(GLSurfaceView v)
63
      {
64
      mView = v;
65

    
66
      CubesActivity act = (CubesActivity)v.getContext();
67

    
68
      mEffects = new DistortedEffects();
69
      mTexture = act.getTexture();
70
      mGrid    = act.getGrid();
71

    
72
      mObjWidth = mTexture.getWidth();
73
      mObjHeight= mTexture.getHeight();
74

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

    
81
      mQuatInt1.add(mQuat1);
82
      mQuatInt2.add(mQuat2);
83

    
84
      mScreen = new DistortedFramebuffer(0);
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
   
89
    public void onDrawFrame(GL10 glUnused) 
90
      {
91
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
92
      mScreen.renderTo(mTexture, mGrid, mEffects, System.currentTimeMillis() );
93
      }
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
    
97
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
98
      {
99
      mScreenMin = width<height ? width:height;
100
    	
101
      mEffects.abortEffects(EffectTypes.MATRIX);
102
      float factor;
103

    
104
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
105
        {
106
        factor = (0.8f*height)/mObjHeight;
107
        }
108
      else
109
        {
110
        factor = (0.8f*width)/mObjWidth;
111
        }
112

    
113
      mEffects.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
114
      mEffects.scale(factor);
115
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
116

    
117
      mEffects.quaternion(mQuatInt1, center);
118
      mEffects.quaternion(mQuatInt2, center);
119
       
120
      mScreen.resize(width, height);
121
      }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
    
125
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
126
      {
127
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
128

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