Project

General

Profile

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

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

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.DistortedScreen;
33
import org.distorted.library.DistortedTexture;
34
import org.distorted.library.MeshObject;
35
import org.distorted.library.EffectTypes;
36
import org.distorted.library.type.DynamicQuat;
37
import org.distorted.library.type.Static4D;
38
import org.distorted.library.type.Static3D;
39
import org.distorted.library.Distorted;
40

    
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43
import android.opengl.GLES30;
44
import android.opengl.GLSurfaceView;
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
class CubesRenderer implements GLSurfaceView.Renderer 
49
{
50
    private GLSurfaceView mView;
51
    private DistortedTexture mTexture;
52
    private DistortedEffects mEffects;
53
    private MeshObject mMesh;
54
    private DistortedScreen mScreen;
55
    private DynamicQuat mQuatInt1, mQuatInt2;
56
    private int mObjWidth, mObjHeight;
57

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

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

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

    
69
      mEffects = new DistortedEffects();
70
      mTexture = act.getTexture();
71
      mMesh = act.getMesh();
72

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

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

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

    
85
      mScreen = new DistortedScreen();
86
      }
87

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

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

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

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

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

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

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