Project

General

Profile

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

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

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 d218d64e leszek
import org.distorted.library.DistortedScreen;
33 f6d884d5 Leszek Koltunski
import org.distorted.library.DistortedTexture;
34 b01acdaf Leszek Koltunski
import org.distorted.library.MeshObject;
35 95593730 Leszek Koltunski
import org.distorted.library.EffectTypes;
36 7589635e Leszek Koltunski
import org.distorted.library.type.DynamicQuat;
37
import org.distorted.library.type.Static4D;
38
import org.distorted.library.type.Static3D;
39 5068fa06 Leszek Koltunski
import org.distorted.library.Distorted;
40 427ab7bf Leszek Koltunski
41
import android.graphics.Bitmap;
42
import android.graphics.BitmapFactory;
43 41a81a14 Leszek Koltunski
import android.opengl.GLES30;
44 427ab7bf Leszek Koltunski
import android.opengl.GLSurfaceView;
45
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
48
class CubesRenderer implements GLSurfaceView.Renderer 
49
{
50
    private GLSurfaceView mView;
51 f6d884d5 Leszek Koltunski
    private DistortedTexture mTexture;
52 d04a4886 Leszek Koltunski
    private DistortedEffects mEffects;
53 b01acdaf Leszek Koltunski
    private MeshObject mMesh;
54 d218d64e leszek
    private DistortedScreen mScreen;
55 f6d884d5 Leszek Koltunski
    private DynamicQuat mQuatInt1, mQuatInt2;
56 261fe5bd Leszek Koltunski
    private int mObjWidth, mObjHeight;
57
58 7589635e Leszek Koltunski
    Static4D mQuat1, mQuat2;
59 427ab7bf Leszek Koltunski
    int mScreenMin;
60
    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 2b261a18 Leszek Koltunski
    CubesRenderer(GLSurfaceView v)
64 427ab7bf Leszek Koltunski
      {
65
      mView = v;
66 261fe5bd Leszek Koltunski
67 e8b6aa95 Leszek Koltunski
      CubesActivity act = (CubesActivity)v.getContext();
68
69 d04a4886 Leszek Koltunski
      mEffects = new DistortedEffects();
70 f6d884d5 Leszek Koltunski
      mTexture = act.getTexture();
71 7fca6741 Leszek Koltunski
      mMesh = act.getMesh();
72 261fe5bd Leszek Koltunski
73 f6d884d5 Leszek Koltunski
      mObjWidth = mTexture.getWidth();
74
      mObjHeight= mTexture.getHeight();
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 41a81a14 Leszek Koltunski
      GLES30.glClear( GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);
93 b01acdaf Leszek Koltunski
      mScreen.renderTo(mTexture, mMesh, mEffects, System.currentTimeMillis() );
94 427ab7bf Leszek Koltunski
      }
95
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97
    
98
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
99
      {
100
      mScreenMin = width<height ? width:height;
101
    	
102 392e16fd Leszek Koltunski
      mEffects.abortEffects(EffectTypes.MATRIX);
103 261fe5bd Leszek Koltunski
      float factor;
104 427ab7bf Leszek Koltunski
105 261fe5bd Leszek Koltunski
      if( width*mObjHeight > height*mObjWidth ) // screen is more 'horizontal' than the Object
106 427ab7bf Leszek Koltunski
        {
107 ff31e865 Leszek Koltunski
        factor = (0.75f*height)/mObjHeight;
108 261fe5bd Leszek Koltunski
        }
109 427ab7bf Leszek Koltunski
      else
110 261fe5bd Leszek Koltunski
        {
111 ff31e865 Leszek Koltunski
        factor = (0.75f*width)/mObjWidth;
112 427ab7bf Leszek Koltunski
        }
113 261fe5bd Leszek Koltunski
114 392e16fd Leszek Koltunski
      mEffects.move( new Static3D( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0) );
115
      mEffects.scale(factor);
116 ff31e865 Leszek Koltunski
      Static3D center = new Static3D( (float)mObjWidth/2, (float)mObjHeight/2, 0.0f );
117 261fe5bd Leszek Koltunski
118 392e16fd Leszek Koltunski
      mEffects.quaternion(mQuatInt1, center);
119
      mEffects.quaternion(mQuatInt2, center);
120 427ab7bf Leszek Koltunski
       
121 392e16fd Leszek Koltunski
      mScreen.resize(width, height);
122 427ab7bf Leszek Koltunski
      }
123
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125
    
126
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
127
      {
128 41a81a14 Leszek Koltunski
      GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
129 e7a4ef16 Leszek Koltunski
130 427ab7bf Leszek Koltunski
      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 f6d884d5 Leszek Koltunski
      mTexture.setTexture(bitmap);
147 427ab7bf Leszek Koltunski
      
148
      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
}