Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikRenderer.java @ 687263cc

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
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 mMove, 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
      mMove  = new Static3D(0,0,0);
63
      mScale = new Static3D(1,1,1);
64

    
65
      mNextCubeSize= MIN_CUBE_SIZE;
66
      mChangeCubeSizeNow = false;
67

    
68
      mNumCube = 0;
69
      }
70

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

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

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

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

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

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

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

    
104
     recomputeScaleFactor(width,height);
105

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

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
113
     {
114
     createNextCube();
115

    
116
     VertexEffectSink.enable();
117

    
118
     try
119
       {
120
       DistortedLibrary.onCreate(mView.getContext());
121
       }
122
     catch(Exception ex)
123
       {
124
       android.util.Log.e("Rubik", ex.getMessage() );
125
       }
126
     }
127

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

    
130
   private void createNextCube()
131
     {
132
     if( mCube!=null ) mCube.releaseResources();
133
     mCube = new RubikCube(mNextCubeSize, mMove, mScale);
134
     mCube.createTexture();
135

    
136
     if( mScreenWidth!=0 ) recomputeScaleFactor(mScreenWidth,mScreenHeight);
137

    
138
     mScreen.detachAll();
139
     mCube.attachToScreen(mScreen);
140
     mCube.addRotation(this);
141
     RubikActivity act = mView.getRubikActivity();
142
     act.setText(++mNumCube);
143
     }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

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

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
   private void recomputeScaleFactor(int screenWidth, int screenHeight)
156
     {
157
     float cubeSizeInScreenSpace = CUBE_SCREEN_RATIO*(screenWidth>screenHeight ? screenHeight:screenWidth);
158
     float texSize = mCube.getStretchSize();
159
     float scaleFactor = cubeSizeInScreenSpace/(texSize*mCube.getSize());
160

    
161
     mMove.set( (screenWidth-scaleFactor*texSize)/2 , (screenHeight-scaleFactor*texSize)/2 , -scaleFactor*texSize/2 );
162
     mScale.set(scaleFactor,scaleFactor,scaleFactor);
163
     }
164
}
(3-3/4)