Project

General

Profile

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

examples / src / main / java / org / distorted / examples / cubes / CubesRenderer.java @ 8a99c681

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 d218d64e leszek
import org.distorted.library.DistortedScreen;
32 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
33 b01acdaf Leszek Koltunski
import org.distorted.library.MeshObject;
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 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
43 427ab7bf Leszek Koltunski
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 b01acdaf Leszek Koltunski
    private MeshObject mMesh;
53 d218d64e leszek
    private DistortedScreen mScreen;
54 f6d884d5 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
55 8a99c681 Leszek Koltunski
    private int mObjWidth, mObjHeight, mObjDepth;
56 261fe5bd Leszek Koltunski
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 7fca6741 Leszek Koltunski
      mMesh = act.getMesh();
71 261fe5bd Leszek Koltunski
72 f6d884d5 Leszek Koltunski
      mObjWidth = mTexture.getWidth();
73
      mObjHeight= mTexture.getHeight();
74 8a99c681 Leszek Koltunski
      mObjDepth = mTexture.getDepth(mMesh);
75 261fe5bd Leszek Koltunski
76 7589635e Leszek Koltunski
      mQuat1 = new Static4D(0,0,0,1);  // unity
77
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
78 427ab7bf Leszek Koltunski
      
79 833685d0 Leszek Koltunski
      mQuatInt1 = new DynamicQuat(0,0.5f);
80
      mQuatInt2 = new DynamicQuat(0,0.5f);
81
82 427ab7bf Leszek Koltunski
      mQuatInt1.add(mQuat1);
83
      mQuatInt2.add(mQuat2);
84 392e16fd Leszek Koltunski
85 d218d64e leszek
      mScreen = new DistortedScreen();
86 427ab7bf Leszek Koltunski
      }
87
88
///////////////////////////////////////////////////////////////////////////////////////////////////
89
   
90
    public void onDrawFrame(GL10 glUnused) 
91
      {
92 fe59d375 Leszek Koltunski
      mScreen.render( 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 8c1caf83 leszek
    	
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.75f*height)/mObjHeight;
107
        }
108
      else
109
        {
110
        factor = (0.75f*width)/mObjWidth;
111
        }
112 261fe5bd Leszek Koltunski
113 8c1caf83 leszek
      mEffects.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
114
      mEffects.scale(factor);
115 8a99c681 Leszek Koltunski
      Static3D center = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, -(float)mObjDepth/2 );
116 261fe5bd Leszek Koltunski
117 392e16fd Leszek Koltunski
      mEffects.quaternion(mQuatInt1, center);
118
      mEffects.quaternion(mQuatInt2, center);
119 8c1caf83 leszek
       
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
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
128
      Bitmap bitmap;
129
        
130
      try 
131
        {
132
        bitmap = BitmapFactory.decodeStream(is);
133
        } 
134
      finally 
135
        {
136
        try 
137
          {
138
          is.close();
139
          } 
140
        catch(IOException e) { }
141
        }  
142
      
143 f6d884d5 Leszek Koltunski
      mTexture.setTexture(bitmap);
144 fe59d375 Leszek Koltunski
145
      mScreen.detachAll();
146
      mScreen.attach(mTexture,mEffects,mMesh);
147
148 427ab7bf Leszek Koltunski
      try
149
        {
150 76f9798b Leszek Koltunski
        Distorted.onCreate(mView.getContext());
151 427ab7bf Leszek Koltunski
        }
152
      catch(Exception ex)
153
        {
154
        android.util.Log.e("Cubes", ex.getMessage() );
155
        }
156
      }
157
}