Project

General

Profile

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

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

1 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 8647eae2 Leszek Koltunski
// Copyright 2019 Leszek Koltunski                                                               //
3 5b4a2d76 Leszek Koltunski
//                                                                                               //
4 71c8884f Leszek Koltunski
// This file is part of Distorted.                                                               //
5 5b4a2d76 Leszek Koltunski
//                                                                                               //
6 71c8884f Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 5b4a2d76 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 5b4a2d76 Leszek Koltunski
// 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 71c8884f Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.examples.rubik;
21
22
import android.opengl.GLSurfaceView;
23
24 94cc96ff Leszek Koltunski
import org.distorted.library.effect.VertexEffectSink;
25 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
26 5b4a2d76 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
27 b62eb334 Leszek Koltunski
import org.distorted.library.message.EffectListener;
28 5b4a2d76 Leszek Koltunski
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 061449ed Leszek Koltunski
class RubikRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.ExceptionListener
36 5b4a2d76 Leszek Koltunski
{
37 e4c0057e Leszek Koltunski
    private static final float CUBE_SCREEN_RATIO = 0.5f;
38 8a40abf4 Leszek Koltunski
    private static final float CAMERA_DISTANCE   = 0.6f;  // 0.6 of the length of max(scrHeight,scrWidth)
39 6e18bd32 Leszek Koltunski
    private static final int MIN_CUBE_SIZE = 1;
40
    private static final int MAX_CUBE_SIZE = 6;
41 e4c0057e Leszek Koltunski
42 18709ae0 Leszek Koltunski
    private RubikSurfaceView mView;
43 5b4a2d76 Leszek Koltunski
    private DistortedScreen mScreen;
44 ba9ae2c8 Leszek Koltunski
    private Static3D mScale;
45 e3a72781 Leszek Koltunski
    private int mNextCubeSize;
46 6e18bd32 Leszek Koltunski
    private boolean mChangeCubeSizeNow;
47
    private int mNumCube;
48 8647eae2 Leszek Koltunski
    private RubikCube mCube;
49 5b4a2d76 Leszek Koltunski
50 af8b42cc Leszek Koltunski
    private int mScreenWidth, mScreenHeight;
51
52 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54 18709ae0 Leszek Koltunski
    RubikRenderer(RubikSurfaceView v)
55 5b4a2d76 Leszek Koltunski
      {
56
      mView = v;
57
58
      mScreen = new DistortedScreen();
59
60 af8b42cc Leszek Koltunski
      mScreenWidth = mScreenHeight = 0;
61
62 5b4a2d76 Leszek Koltunski
      mScale = new Static3D(1,1,1);
63 8647eae2 Leszek Koltunski
64 6e18bd32 Leszek Koltunski
      mNextCubeSize= MIN_CUBE_SIZE;
65
      mChangeCubeSizeNow = false;
66 e3a72781 Leszek Koltunski
67 6e18bd32 Leszek Koltunski
      mNumCube = 0;
68 5b4a2d76 Leszek Koltunski
      }
69
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71 6e18bd32 Leszek Koltunski
// change the cube size here, 'after the next render' as not to cause artifacts.
72 b62eb334 Leszek Koltunski
73 5b4a2d76 Leszek Koltunski
    public void onDrawFrame(GL10 glUnused) 
74
      {
75
      mScreen.render( System.currentTimeMillis() );
76 94cc96ff Leszek Koltunski
77 6e18bd32 Leszek Koltunski
      if( mChangeCubeSizeNow )
78 8647eae2 Leszek Koltunski
        {
79 6e18bd32 Leszek Koltunski
        mChangeCubeSizeNow = false;
80
        createNextCube();
81 e3a72781 Leszek Koltunski
        }
82 5b4a2d76 Leszek Koltunski
      }
83
84 b62eb334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
85
86 8d5a8e06 Leszek Koltunski
   public void effectFinished(final long effectID)
87 b62eb334 Leszek Koltunski
     {
88 8d5a8e06 Leszek Koltunski
     mNextCubeSize++;
89
     if( mNextCubeSize> MAX_CUBE_SIZE ) mNextCubeSize = MIN_CUBE_SIZE;
90
     mChangeCubeSizeNow = true;
91 b62eb334 Leszek Koltunski
     }
92
93 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
94
    
95 da231553 Leszek Koltunski
   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 7f986357 Leszek Koltunski
100 da231553 Leszek Koltunski
     mScreen.setProjection( fovInDegrees, 0.1f);
101
     mScreen.resize(width, height);
102 5b4a2d76 Leszek Koltunski
103 da231553 Leszek Koltunski
     recomputeScaleFactor(width,height);
104 5b4a2d76 Leszek Koltunski
105 da231553 Leszek Koltunski
     mScreenHeight = height;
106
     mScreenWidth  = width;
107
     }
108 5b4a2d76 Leszek Koltunski
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
    
111 da231553 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
112
     {
113
     createNextCube();
114
     VertexEffectSink.enable();
115 061449ed Leszek Koltunski
     DistortedLibrary.onCreate(mView.getContext(), this);
116
     }
117 da231553 Leszek Koltunski
118 061449ed Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
   public void distortedException(Exception ex)
121
     {
122
     android.util.Log.e("Rubik", ex.getMessage() );
123 da231553 Leszek Koltunski
     }
124 e4c0057e Leszek Koltunski
125 483ae94e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
126
127 6e18bd32 Leszek Koltunski
   private void createNextCube()
128 af8b42cc Leszek Koltunski
     {
129 6e18bd32 Leszek Koltunski
     if( mCube!=null ) mCube.releaseResources();
130 ba9ae2c8 Leszek Koltunski
     mCube = new RubikCube(mNextCubeSize, mScale);
131 6e18bd32 Leszek Koltunski
     mCube.createTexture();
132 8647eae2 Leszek Koltunski
133 6e18bd32 Leszek Koltunski
     if( mScreenWidth!=0 ) recomputeScaleFactor(mScreenWidth,mScreenHeight);
134 e4c0057e Leszek Koltunski
135 6e18bd32 Leszek Koltunski
     mScreen.detachAll();
136
     mCube.attachToScreen(mScreen);
137
     mCube.addRotation(this);
138
     RubikActivity act = mView.getRubikActivity();
139
     act.setText(++mNumCube);
140 e3a72781 Leszek Koltunski
     }
141
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
144 6e18bd32 Leszek Koltunski
   private float computeFOV(float cameraDistance, int screenHeight)
145 af8b42cc Leszek Koltunski
     {
146 6e18bd32 Leszek Koltunski
     double halfFOVInRadians = Math.atan( screenHeight/(2*cameraDistance) );
147
     return (float)(2*halfFOVInRadians*(180/Math.PI));
148 af8b42cc Leszek Koltunski
     }
149 e4c0057e Leszek Koltunski
150 d4374cd3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
151
152 e3a72781 Leszek Koltunski
   private void recomputeScaleFactor(int screenWidth, int screenHeight)
153 af8b42cc Leszek Koltunski
     {
154 55908771 Leszek Koltunski
     float scaleFactor = CUBE_SCREEN_RATIO * Math.min(screenWidth,screenHeight) / mCube.getSize();
155 af8b42cc Leszek Koltunski
     mScale.set(scaleFactor,scaleFactor,scaleFactor);
156
     }
157 5b4a2d76 Leszek Koltunski
}