Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikRenderer.java @ 7f62afde

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 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.rubik;
21

    
22
import android.opengl.GLSurfaceView;
23

    
24
import org.distorted.library.effect.VertexEffectSink;
25
import org.distorted.library.main.Distorted;
26
import org.distorted.library.main.DistortedScreen;
27
import org.distorted.library.type.Static3D;
28
import org.distorted.library.type.Static4D;
29

    
30
import javax.microedition.khronos.egl.EGLConfig;
31
import javax.microedition.khronos.opengles.GL10;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
class RubikRenderer implements GLSurfaceView.Renderer
36
{
37
    private static final int NUM_CUBES = 4;
38
    private static final float CUBE_SCREEN_RATIO = 0.5f;
39

    
40
    private RubikSurfaceView mView;
41
    private DistortedScreen mScreen;
42
    private Static3D mMove, mScale;
43
    private Static4D mQuatCurrent, mQuatAccumulated;
44
    private Static4D mTempCurrent, mTempAccumulated;
45
    private float mScaleFactor;
46
    private boolean mFinishRotation, mFinishDragCurrent, mFinishDragAccumulated;
47
    private RubikCube mCube;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
    RubikRenderer(RubikSurfaceView v)
52
      {
53
      mView = v;
54

    
55
      mScreen = new DistortedScreen();
56

    
57
      mTempCurrent     = new Static4D(0,0,0,1);
58
      mTempAccumulated = initializeQuat();
59
      mQuatCurrent     = new Static4D(0,0,0,1);
60
      mQuatAccumulated = initializeQuat();
61

    
62
      mMove  = new Static3D(0,0,0);
63
      mScale = new Static3D(1,1,1);
64

    
65
      mFinishRotation        = false;
66
      mFinishDragCurrent     = false;
67
      mFinishDragAccumulated = false;
68

    
69
      mCube = new RubikCube(NUM_CUBES, mMove, mScale, mQuatCurrent, mQuatAccumulated);
70
      }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
   
74
    public void onDrawFrame(GL10 glUnused) 
75
      {
76
      mScreen.render( System.currentTimeMillis() );
77

    
78
      if( mFinishDragCurrent )
79
        {
80
        mFinishDragCurrent = false;
81
        mQuatCurrent.set(mTempCurrent);
82
        }
83

    
84
      if( mFinishDragAccumulated )
85
        {
86
        mFinishDragAccumulated = false;
87
        mQuatAccumulated.set(mTempAccumulated);
88
        }
89

    
90
      if( mFinishRotation )
91
        {
92
        mFinishRotation=false;
93
        mCube.finishRotationCalledOnNextRender();
94
        }
95
      }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
    
99
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
100
      {
101
      float fovInDegrees = (width>height ? 60.0f : 90.0f);
102

    
103
      mScreen.setProjection( fovInDegrees, 0.1f);
104

    
105
      mView.setScreenSize(width,height);
106
      mView.recomputeCameraDistance(fovInDegrees*Math.PI/180);
107

    
108
      float w = mCube.getWidth();
109
      float h = mCube.getHeight();
110
      float d = mCube.getDepth();
111

    
112
      mScaleFactor = CUBE_SCREEN_RATIO*(width>height ? height:width)/mCube.getSizeInModelSpace();
113

    
114
      mMove.set( (width-mScaleFactor*w)/2 , (height-mScaleFactor*h)/2 , -mScaleFactor*d/2 );
115
      mScale.set(mScaleFactor,mScaleFactor,mScaleFactor);
116

    
117
      mScreen.resize(width, height);
118
      }
119

    
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
    
122
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
123
      {
124
      mCube.createTexture();
125
      mScreen.detachAll();
126
      mCube.attachToScreen(mScreen);
127

    
128
      VertexEffectSink.enable();
129

    
130
      try
131
        {
132
        Distorted.onCreate(mView.getContext());
133
        }
134
      catch(Exception ex)
135
        {
136
        android.util.Log.e("Rubik", ex.getMessage() );
137
        }
138
      }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
// no this will not race with onDrawFrame
142

    
143
    void finishRotation()
144
      {
145
      mFinishRotation = true;
146
      }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
    float returnCubeSizeInScreenSpace()
151
      {
152
      return mScaleFactor*mCube.getSizeInModelSpace();
153
      }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

    
157
    RubikCube getCube()
158
      {
159
      return mCube;
160
      }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
// Initial rotation of the cube. Something semi-random that looks good.
164

    
165
    Static4D initializeQuat()
166
      {
167
      return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
168
      }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
    void setQuatCurrent(Static4D current)
173
      {
174
      mTempCurrent.set(current);
175
      mFinishDragCurrent = true;
176
      }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

    
180
    void setQuatAccumulated(Static4D accumulated)
181
      {
182
      mTempAccumulated.set(accumulated);
183
      mFinishDragAccumulated = true;
184
      }
185
}
(3-3/4)