Project

General

Profile

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

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

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 dc10a48d Leszek Koltunski
import android.app.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.content.res.Resources;
26 5b4a2d76 Leszek Koltunski
import android.opengl.GLSurfaceView;
27
28 94cc96ff Leszek Koltunski
import org.distorted.library.effect.VertexEffectSink;
29 e3900503 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
30 5b4a2d76 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
31 b62eb334 Leszek Koltunski
import org.distorted.library.message.EffectListener;
32 5b4a2d76 Leszek Koltunski
import org.distorted.library.type.Static3D;
33
34 dc10a48d Leszek Koltunski
import java.io.InputStream;
35
36 5b4a2d76 Leszek Koltunski
import javax.microedition.khronos.egl.EGLConfig;
37
import javax.microedition.khronos.opengles.GL10;
38
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41 dc10a48d Leszek Koltunski
class RubikRenderer implements GLSurfaceView.Renderer, EffectListener, DistortedLibrary.LibraryUser
42 5b4a2d76 Leszek Koltunski
{
43 e4c0057e Leszek Koltunski
    private static final float CUBE_SCREEN_RATIO = 0.5f;
44 8a40abf4 Leszek Koltunski
    private static final float CAMERA_DISTANCE   = 0.6f;  // 0.6 of the length of max(scrHeight,scrWidth)
45 6e18bd32 Leszek Koltunski
    private static final int MIN_CUBE_SIZE = 1;
46
    private static final int MAX_CUBE_SIZE = 6;
47 e4c0057e Leszek Koltunski
48 dc10a48d Leszek Koltunski
    private final RubikSurfaceView mView;
49
    private final DistortedScreen mScreen;
50
    private final Static3D mScale;
51
    private final Resources mResources;
52
53
    private RubikCube mCube;
54 e3a72781 Leszek Koltunski
    private int mNextCubeSize;
55 6e18bd32 Leszek Koltunski
    private boolean mChangeCubeSizeNow;
56
    private int mNumCube;
57 af8b42cc Leszek Koltunski
    private int mScreenWidth, mScreenHeight;
58
59 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
60
61 18709ae0 Leszek Koltunski
    RubikRenderer(RubikSurfaceView v)
62 5b4a2d76 Leszek Koltunski
      {
63
      mView = v;
64 dc10a48d Leszek Koltunski
      mResources = v.getResources();
65 5b4a2d76 Leszek Koltunski
      mScreen = new DistortedScreen();
66
67 af8b42cc Leszek Koltunski
      mScreenWidth = mScreenHeight = 0;
68
69 5b4a2d76 Leszek Koltunski
      mScale = new Static3D(1,1,1);
70 8647eae2 Leszek Koltunski
71 6e18bd32 Leszek Koltunski
      mNextCubeSize= MIN_CUBE_SIZE;
72
      mChangeCubeSizeNow = false;
73 e3a72781 Leszek Koltunski
74 6e18bd32 Leszek Koltunski
      mNumCube = 0;
75 5b4a2d76 Leszek Koltunski
      }
76
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78 6e18bd32 Leszek Koltunski
// change the cube size here, 'after the next render' as not to cause artifacts.
79 b62eb334 Leszek Koltunski
80 5b4a2d76 Leszek Koltunski
    public void onDrawFrame(GL10 glUnused) 
81
      {
82
      mScreen.render( System.currentTimeMillis() );
83 94cc96ff Leszek Koltunski
84 6e18bd32 Leszek Koltunski
      if( mChangeCubeSizeNow )
85 8647eae2 Leszek Koltunski
        {
86 6e18bd32 Leszek Koltunski
        mChangeCubeSizeNow = false;
87
        createNextCube();
88 e3a72781 Leszek Koltunski
        }
89 5b4a2d76 Leszek Koltunski
      }
90
91 b62eb334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93 8d5a8e06 Leszek Koltunski
   public void effectFinished(final long effectID)
94 b62eb334 Leszek Koltunski
     {
95 8d5a8e06 Leszek Koltunski
     mNextCubeSize++;
96
     if( mNextCubeSize> MAX_CUBE_SIZE ) mNextCubeSize = MIN_CUBE_SIZE;
97
     mChangeCubeSizeNow = true;
98 b62eb334 Leszek Koltunski
     }
99
100 5b4a2d76 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
101
    
102 da231553 Leszek Koltunski
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
103
     {
104 dc10a48d Leszek Koltunski
     float cameraDistance = CAMERA_DISTANCE*(Math.max(width,height));
105 da231553 Leszek Koltunski
     float fovInDegrees   = computeFOV(cameraDistance,height);
106 7f986357 Leszek Koltunski
107 da231553 Leszek Koltunski
     mScreen.setProjection( fovInDegrees, 0.1f);
108
     mScreen.resize(width, height);
109 5b4a2d76 Leszek Koltunski
110 da231553 Leszek Koltunski
     recomputeScaleFactor(width,height);
111 5b4a2d76 Leszek Koltunski
112 da231553 Leszek Koltunski
     mScreenHeight = height;
113
     mScreenWidth  = width;
114
     }
115 5b4a2d76 Leszek Koltunski
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
    
118 da231553 Leszek Koltunski
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
119
     {
120
     createNextCube();
121
     VertexEffectSink.enable();
122 dc10a48d Leszek Koltunski
     DistortedLibrary.onSurfaceCreated(this);
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 dc10a48d Leszek Koltunski
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160
   public void distortedException(Exception ex)
161
     {
162
     android.util.Log.e("Rubik", ex.getMessage() );
163
     }
164
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
167
  public int openGlVersion()
168
     {
169
     Context context = mView.getContext();
170
     final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
171
     final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
172
     int glESversion = configurationInfo.reqGlEsVersion;
173
     int major = glESversion >> 16;
174
     int minor = glESversion & 0xff;
175
176
     return 100*major + 10*minor;
177
     }
178
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181
  public InputStream localFile(int fileID)
182
     {
183
     return mResources.openRawResource(fileID);
184
     }
185
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
188
  public void logMessage(String message)
189
     {
190
     android.util.Log.e("Rubik", message );
191
     }
192 5b4a2d76 Leszek Koltunski
}