Project

General

Profile

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

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

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.effect.MatrixEffectMove;
31
import org.distorted.library.effect.MatrixEffectQuaternion;
32
import org.distorted.library.effect.MatrixEffectScale;
33
import org.distorted.library.main.DistortedEffects;
34
import org.distorted.library.main.DistortedScreen;
35
import org.distorted.library.main.DistortedTexture;
36
import org.distorted.library.main.MeshObject;
37
import org.distorted.library.type.DynamicQuat;
38
import org.distorted.library.type.Static4D;
39
import org.distorted.library.type.Static3D;
40
import org.distorted.library.main.Distorted;
41

    
42
import android.graphics.Bitmap;
43
import android.graphics.BitmapFactory;
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 int mObjWidth, mObjHeight, mObjDepth;
56
    private Static3D mMove, mScale, mCenter;
57

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

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

    
67
      mMove = new Static3D(0,0,0);
68
      mScale= new Static3D(1,1,1);
69
      mCenter=new Static3D(0,0,0);
70

    
71
      CubesActivity act = (CubesActivity)v.getContext();
72

    
73
      mTexture = act.getTexture();
74
      mMesh = act.getMesh();
75

    
76
      mObjWidth = mTexture.getWidth();
77
      mObjHeight= mTexture.getHeight();
78
      mObjDepth = mTexture.getDepth(mMesh);
79

    
80
      mQuat1 = new Static4D(0,0,0,1);  // unity
81
      mQuat2 = new Static4D(0,0,0,1);  // quaternions
82
      
83
      DynamicQuat quatInt1 = new DynamicQuat(0,0.5f);
84
      DynamicQuat quatInt2 = new DynamicQuat(0,0.5f);
85

    
86
      quatInt1.add(mQuat1);
87
      quatInt2.add(mQuat2);
88

    
89
      mEffects = new DistortedEffects();
90
      mEffects.apply( new MatrixEffectMove(mMove) );
91
      mEffects.apply( new MatrixEffectScale(mScale));
92
      mEffects.apply( new MatrixEffectQuaternion(quatInt1, mCenter) );
93
      mEffects.apply( new MatrixEffectQuaternion(quatInt2, mCenter) );
94

    
95
      mScreen = new DistortedScreen();
96
      }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
   
100
    public void onDrawFrame(GL10 glUnused) 
101
      {
102
      mScreen.render( System.currentTimeMillis() );
103
      }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
    
107
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
108
      {
109
      mScreenMin = width<height ? width:height;
110
      float factor = ( width*mObjHeight > height*mObjWidth ) ? (0.75f*height)/mObjHeight :  (0.75f*width)/mObjWidth;
111
      mMove.set( (width-factor*mObjWidth)/2 , (height-factor*mObjHeight)/2 , 0);
112
      mScale.set(factor,factor,factor);
113
      mCenter.set( (float)mObjWidth/2, (float)mObjHeight/2, -(float)mObjDepth/2 );
114
      mScreen.resize(width, height);
115
      }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
120
      {
121
      InputStream is = mView.getContext().getResources().openRawResource(R.raw.grid);
122
      Bitmap bitmap;
123
        
124
      try 
125
        {
126
        bitmap = BitmapFactory.decodeStream(is);
127
        } 
128
      finally 
129
        {
130
        try 
131
          {
132
          is.close();
133
          } 
134
        catch(IOException e) { }
135
        }  
136
      
137
      mTexture.setTexture(bitmap);
138

    
139
      mScreen.detachAll();
140
      mScreen.attach(mTexture,mEffects,mMesh);
141

    
142
      try
143
        {
144
        Distorted.onCreate(mView.getContext());
145
        }
146
      catch(Exception ex)
147
        {
148
        android.util.Log.e("Cubes", ex.getMessage() );
149
        }
150
      }
151
}
(2-2/3)