Project

General

Profile

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

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

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 d04a4886 Leszek Koltunski
import org.distorted.library.DistortedEffects;
31 392e16fd Leszek Koltunski
import org.distorted.library.DistortedFramebuffer;
32 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
33 10b7e588 Leszek Koltunski
import org.distorted.library.GridObject;
34 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
35 7589635e Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
36
import org.distorted.library.type.Static4D;
37
import org.distorted.library.type.Static3D;
38 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
39 427ab7bf Leszek Koltunski
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 f6d884d5 Leszek Koltunski
    private DistortedTexture mTexture;
51 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
52 10b7e588 Leszek Koltunski
    private GridObject mGrid;
53 392e16fd Leszek Koltunski
    private DistortedFramebuffer mScreen;
54 f6d884d5 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
55 261fe5bd Leszek Koltunski
    private int mObjWidth, mObjHeight;
56
57 7589635e Leszek Koltunski
    Static4D mQuat1, mQuat2;
58 427ab7bf Leszek Koltunski
    int mScreenMin;
59
    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62 2b261a18 Leszek Koltunski
    CubesRenderer(GLSurfaceView v)
63 427ab7bf Leszek Koltunski
      {
64
      mView = v;
65 261fe5bd Leszek Koltunski
66 e8b6aa95 Leszek Koltunski
      CubesActivity act = (CubesActivity)v.getContext();
67
68 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
69 f6d884d5 Leszek Koltunski
      mTexture = act.getTexture();
70
      mGrid    = act.getGrid();
71 261fe5bd Leszek Koltunski
72 f6d884d5 Leszek Koltunski
      mObjWidth = mTexture.getWidth();
73
      mObjHeight= mTexture.getHeight();
74 261fe5bd Leszek Koltunski
75 7589635e Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
76
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
77 427ab7bf Leszek Koltunski
      
78 833685d0 Leszek Koltunski
      mQuatInt1 = new DynamicQuat(0,0.5f);
79
      mQuatInt2 = new DynamicQuat(0,0.5f);
80
81 427ab7bf Leszek Koltunski
      mQuatInt1.add(mQuat1);
82
      mQuatInt2.add(mQuat2);
83 392e16fd Leszek Koltunski
84
      mScreen = new DistortedFramebuffer(0);
85 427ab7bf Leszek Koltunski
      }
86
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
   
89
    public void onDrawFrame(GL10 glUnused) 
90
      {
91
      GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
92 bc29e409 Leszek Koltunski
      mScreen.renderTo(mTexture, mGrid, mEffects, System.currentTimeMillis() );
93 427ab7bf Leszek Koltunski
      }
94
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96
    
97
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
98
      {
99
      mScreenMin = width<height ? width:height;
100
    	
101 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
102 261fe5bd Leszek Koltunski
      float factor;
103 427ab7bf Leszek Koltunski
104 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
105 427ab7bf Leszek Koltunski
        {
106 261fe5bd Leszek Koltunski
        factor = (0.8f*height)/mObjHeight;
107
        }
108 427ab7bf Leszek Koltunski
      else
109 261fe5bd Leszek Koltunski
        {
110
        factor = (0.8f*width)/mObjWidth;
111 427ab7bf Leszek Koltunski
        }
112 261fe5bd Leszek Koltunski
113 392e16fd Leszek Koltunski
      mEffects.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
114
      mEffects.scale(factor);
115 261fe5bd Leszek Koltunski
      Static3D center = new Static3D(mObjWidth/2,mObjHeight/2, 0);
116
117 392e16fd Leszek Koltunski
      mEffects.quaternion(mQuatInt1, center);
118
      mEffects.quaternion(mQuatInt2, center);
119 427ab7bf Leszek Koltunski
       
120 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
121 427ab7bf Leszek Koltunski
      }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
    
125
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
126
      {
127 e7a4ef16 Leszek Koltunski
      GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
128
129 427ab7bf Leszek Koltunski
      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 f6d884d5 Leszek Koltunski
      mTexture.setTexture(bitmap);
146 427ab7bf Leszek Koltunski
      
147
      try
148
        {
149 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
150 427ab7bf Leszek Koltunski
        }
151
      catch(Exception ex)
152
        {
153
        android.util.Log.e("Cubes", ex.getMessage() );
154
        }
155
      }
156
}