Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikRenderer.java @ 6e18bd32

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.message.EffectListener;
28
import org.distorted.library.message.EffectMessage;
29
import org.distorted.library.type.Static3D;
30
import org.distorted.library.type.Static4D;
31

    
32
import javax.microedition.khronos.egl.EGLConfig;
33
import javax.microedition.khronos.opengles.GL10;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

    
44
    private RubikSurfaceView mView;
45
    private DistortedScreen mScreen;
46
    private Static3D mMove, mScale;
47
    private int mNextCubeSize;
48
    private boolean mChangeCubeSizeNow;
49
    private int mNumCube;
50
    private RubikCube mCube;
51

    
52
    private int mScreenWidth, mScreenHeight;
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
    RubikRenderer(RubikSurfaceView v)
57
      {
58
      mView = v;
59

    
60
      mScreen = new DistortedScreen();
61

    
62
      mScreenWidth = mScreenHeight = 0;
63

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

    
67
      mNextCubeSize= MIN_CUBE_SIZE;
68
      mChangeCubeSizeNow = false;
69

    
70
      mNumCube = 0;
71
      }
72

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74
// change the cube size here, 'after the next render' as not to cause artifacts.
75

    
76
    public void onDrawFrame(GL10 glUnused) 
77
      {
78
      mScreen.render( System.currentTimeMillis() );
79

    
80
      if( mChangeCubeSizeNow )
81
        {
82
        mChangeCubeSizeNow = false;
83
        createNextCube();
84
        }
85
      }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
// EffectListener. The library sends a message to us when it's time to call 'removeRotation'
89

    
90
   public void effectMessage(final EffectMessage em, final long effectID, final long objectID)
91
     {
92
     switch(em)
93
        {
94
        case EFFECT_FINISHED: mNextCubeSize++;
95
                              if( mNextCubeSize> MAX_CUBE_SIZE ) mNextCubeSize = MIN_CUBE_SIZE;
96
                              mChangeCubeSizeNow = true;
97
                              break;
98
        }
99
     }
100

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

    
108
      mScreen.setProjection( fovInDegrees, 0.1f);
109
      mScreen.resize(width, height);
110

    
111
      recomputeScaleFactor(width,height);
112

    
113
      mScreenHeight = height;
114
      mScreenWidth  = width;
115
      }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118
    
119
    public void onSurfaceCreated(GL10 glUnused, EGLConfig config) 
120
      {
121
      createNextCube();
122

    
123
      VertexEffectSink.enable();
124

    
125
      try
126
        {
127
        Distorted.onCreate(mView.getContext());
128
        }
129
      catch(Exception ex)
130
        {
131
        android.util.Log.e("Rubik", ex.getMessage() );
132
        }
133
      }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

    
137
   private void createNextCube()
138
     {
139
     if( mCube!=null ) mCube.releaseResources();
140
     mCube = new RubikCube(mNextCubeSize, mMove, mScale);
141
     mCube.createTexture();
142

    
143
     if( mScreenWidth!=0 ) recomputeScaleFactor(mScreenWidth,mScreenHeight);
144

    
145
     mScreen.detachAll();
146
     mCube.attachToScreen(mScreen);
147
     mCube.addRotation(this);
148
     RubikActivity act = mView.getRubikActivity();
149
     act.setText(++mNumCube);
150
     }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
   private float computeFOV(float cameraDistance, int screenHeight)
155
     {
156
     double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
157
     return (float)(2*halfFOVInRadians*(180/Math.PI));
158
     }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
   private void recomputeScaleFactor(int screenWidth, int screenHeight)
163
     {
164
     float cubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth);
165
     float texSize = mCube.getTextureSize();
166
     float scaleFactor = cubeSizeInScreenSpace/(texSize*mCube.getSize());
167

    
168
     mMove.set( (screenWidth-scaleFactor*texSize)/2 , (screenHeight-scaleFactor*texSize)/2 , -scaleFactor*texSize/2 );
169
     mScale.set(scaleFactor,scaleFactor,scaleFactor);
170
     }
171
}
(3-3/4)