Project

General

Profile

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

examples / src / main / java / org / distorted / examples / rubik / RubikRenderer.java @ e3900503

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of DistortedLibrary.                                                               //
5
//                                                                                               //
6
// DistortedLibrary 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
// DistortedLibrary 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 DistortedLibrary.  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.message.EffectMessage;
29
import org.distorted.library.type.Static3D;
30

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

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

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

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

    
51
    private int mScreenWidth, mScreenHeight;
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

    
59
      mScreen = new DistortedScreen();
60

    
61
      mScreenWidth = mScreenHeight = 0;
62

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

    
66
      mNextCubeSize= MIN_CUBE_SIZE;
67
      mChangeCubeSizeNow = false;
68

    
69
      mNumCube = 0;
70
      }
71

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

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

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
// EffectListener. The library sends a message to us when it's time to create a new cube.
88

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

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

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

    
110
     recomputeScaleFactor(width,height);
111

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

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

    
122
     VertexEffectSink.enable();
123

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

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

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

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

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

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

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

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

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

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