Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikRenderer.java @ 483ae94e

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
    private static final float CAMERA_DISTANCE = 0.5f;     // 0.5 of the length of max(scrHeight,scrWidth)
40

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

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

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

    
56
      mScreen = new DistortedScreen();
57

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

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

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

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

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

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

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

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

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
    
100
    public void onSurfaceChanged(GL10 glUnused, int width, int height) 
101
      {
102
      float cameraDistance = CAMERA_DISTANCE*(width>height ? width:height);
103
      float fovInDegrees   = computeFOV(cameraDistance,height);
104

    
105
      mScreen.setProjection( fovInDegrees, 0.1f);
106
      mView.setScreenSize(width,height);
107
      mView.setCameraDist(cameraDistance);
108

    
109
      mCubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(width>height ? height:width);
110
      float texSize = mCube.getTextureSize();
111
      float scaleFactor = mCubeSizeInScreenSpace/(texSize*mCube.getSize());
112

    
113
      mMove.set( (width-scaleFactor*texSize)/2 , (height-scaleFactor*texSize)/2 , -scaleFactor*texSize/2 );
114
      mScale.set(scaleFactor,scaleFactor,scaleFactor);
115

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

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

    
127
      VertexEffectSink.enable();
128

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

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
    private float computeFOV(float cameraDistance, int screenHeight)
142
      {
143
      double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
144
      float fovInDegrees = (float)(2*halfFOVInRadians*(180/Math.PI));
145

    
146
      return fovInDegrees;
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
// no this will not race with onDrawFrame
151

    
152
    void finishRotation()
153
      {
154
      mFinishRotation = true;
155
      }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
    float returnCubeSizeInScreenSpace()
160
      {
161
      return mCubeSizeInScreenSpace;
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
    RubikCube getCube()
167
      {
168
      return mCube;
169
      }
170

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
// Initial rotation of the cube. Something semi-random that looks good.
173

    
174
    Static4D initializeQuat()
175
      {
176
      return new Static4D(-0.25189602f,0.3546389f,0.009657208f,0.90038127f);
177
      }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
    void setQuatCurrent(Static4D current)
182
      {
183
      mTempCurrent.set(current);
184
      mFinishDragCurrent = true;
185
      }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
    void setQuatAccumulated(Static4D accumulated)
190
      {
191
      mTempAccumulated.set(accumulated);
192
      mFinishDragAccumulated = true;
193
      }
194
}
(3-3/4)