Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikRenderer.java @ 061449ed

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.DistortedLibrary;
26
import org.distorted.library.main.DistortedScreen;
27
import org.distorted.library.message.EffectListener;
28
import org.distorted.library.type.Static3D;
29

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

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

    
35
class RubikRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.ExceptionListener
36
{
37
    private static final float CUBE_SCREEN_RATIO = 0.5f;
38
    private static final float CAMERA_DISTANCE   = 0.6f;  // 0.6 of the length of max(scrHeight,scrWidth)
39
    private static final int MIN_CUBE_SIZE = 1;
40
    private static final int MAX_CUBE_SIZE = 6;
41

    
42
    private RubikSurfaceView mView;
43
    private DistortedScreen mScreen;
44
    private Static3D mScale;
45
    private int mNextCubeSize;
46
    private boolean mChangeCubeSizeNow;
47
    private int mNumCube;
48
    private RubikCube mCube;
49

    
50
    private int mScreenWidth, mScreenHeight;
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
    RubikRenderer(RubikSurfaceView v)
55
      {
56
      mView = v;
57

    
58
      mScreen = new DistortedScreen();
59

    
60
      mScreenWidth = mScreenHeight = 0;
61

    
62
      mScale = new Static3D(1,1,1);
63

    
64
      mNextCubeSize= MIN_CUBE_SIZE;
65
      mChangeCubeSizeNow = false;
66

    
67
      mNumCube = 0;
68
      }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
// change the cube size here, 'after the next render' as not to cause artifacts.
72

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

    
77
      if( mChangeCubeSizeNow )
78
        {
79
        mChangeCubeSizeNow = false;
80
        createNextCube();
81
        }
82
      }
83

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
   public void effectFinished(final long effectID)
87
     {
88
     mNextCubeSize++;
89
     if( mNextCubeSize> MAX_CUBE_SIZE ) mNextCubeSize = MIN_CUBE_SIZE;
90
     mChangeCubeSizeNow = true;
91
     }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
    
95
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
96
     {
97
     float cameraDistance = CAMERA_DISTANCE*(width>height ? width:height);
98
     float fovInDegrees   = computeFOV(cameraDistance,height);
99

    
100
     mScreen.setProjection( fovInDegrees, 0.1f);
101
     mScreen.resize(width, height);
102

    
103
     recomputeScaleFactor(width,height);
104

    
105
     mScreenHeight = height;
106
     mScreenWidth  = width;
107
     }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
    
111
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
112
     {
113
     createNextCube();
114
     VertexEffectSink.enable();
115
     DistortedLibrary.onCreate(mView.getContext(), this);
116
     }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119

    
120
   public void distortedException(Exception ex)
121
     {
122
     android.util.Log.e("Rubik", ex.getMessage() );
123
     }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
   private void createNextCube()
128
     {
129
     if( mCube!=null ) mCube.releaseResources();
130
     mCube = new RubikCube(mNextCubeSize, mScale);
131
     mCube.createTexture();
132

    
133
     if( mScreenWidth!=0 ) recomputeScaleFactor(mScreenWidth,mScreenHeight);
134

    
135
     mScreen.detachAll();
136
     mCube.attachToScreen(mScreen);
137
     mCube.addRotation(this);
138
     RubikActivity act = mView.getRubikActivity();
139
     act.setText(++mNumCube);
140
     }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143

    
144
   private float computeFOV(float cameraDistance, int screenHeight)
145
     {
146
     double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
147
     return (float)(2*halfFOVInRadians*(180/Math.PI));
148
     }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
   private void recomputeScaleFactor(int screenWidth, int screenHeight)
153
     {
154
     float scaleFactor = CUBE_SCREEN_RATIO * Math.min(screenWidth,screenHeight) / mCube.getSize();
155
     mScale.set(scaleFactor,scaleFactor,scaleFactor);
156
     }
157
}
(3-3/4)