Project

General

Profile

Download (7.03 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / main / RubikRenderer.java @ b4a9a34f

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 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 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.main;
21 0c52af30 Leszek Koltunski
22 31911113 Leszek Koltunski
import android.opengl.GLES30;
23 0c52af30 Leszek Koltunski
import android.opengl.GLSurfaceView;
24 31911113 Leszek Koltunski
25 1f9772f3 Leszek Koltunski
import org.distorted.effects.BaseEffect;
26 40ab026e Leszek Koltunski
import org.distorted.library.effect.EffectType;
27 98904e45 Leszek Koltunski
import org.distorted.library.effect.VertexEffectQuaternion;
28 27e6c301 Leszek Koltunski
import org.distorted.library.effect.VertexEffectRotate;
29 e1111500 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
30 0c52af30 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
31 42661133 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
32 0c52af30 Leszek Koltunski
33
import javax.microedition.khronos.egl.EGLConfig;
34
import javax.microedition.khronos.opengles.GL10;
35
36 31911113 Leszek Koltunski
import com.google.firebase.crashlytics.FirebaseCrashlytics;
37
38 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
39
40 c1df2105 Leszek Koltunski
public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
41 0c52af30 Leszek Koltunski
{
42 2e0258fe Leszek Koltunski
   public static final float BRIGHTNESS = 0.30f;
43
44 4b4c217e Leszek Koltunski
   private final RubikSurfaceView mView;
45
   private final DistortedScreen mScreen;
46
   private final Fps mFPS;
47 e7e0a94d Leszek Koltunski
   private boolean mErrorShown;
48 7eae2d49 Leszek Koltunski
49
   private static class Fps
50
     {
51
     private static final int NUM_FRAMES  = 100;
52
53
     private long lastTime=0;
54 42661133 Leszek Koltunski
     private final long[] durations;
55 7eae2d49 Leszek Koltunski
     private int currDuration;
56
     private float currFPS;
57
58
     Fps()
59
       {
60
       durations = new long[NUM_FRAMES+1];
61
       currDuration = 0;
62
63
       for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16;
64
       durations[NUM_FRAMES] = NUM_FRAMES * 16;
65
       }
66
67
     void onRender(long time)
68
       {
69
       if( lastTime==0 ) lastTime = time;
70
71
       currDuration++;
72
       if (currDuration >= NUM_FRAMES) currDuration = 0;
73
       durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
74
       durations[currDuration] = time - lastTime;
75
76
       currFPS = ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
77
78
       lastTime = time;
79
       }
80
81
     float getFPS()
82
       {
83
       return currFPS;
84
       }
85
     }
86 47ba5ddc Leszek Koltunski
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88
89 8becce57 Leszek Koltunski
   RubikRenderer(RubikSurfaceView v)
90 47ba5ddc Leszek Koltunski
     {
91 e7e0a94d Leszek Koltunski
     mErrorShown = false;
92 8becce57 Leszek Koltunski
     mView = v;
93 7eae2d49 Leszek Koltunski
     mFPS = new Fps();
94 8becce57 Leszek Koltunski
     mScreen = new DistortedScreen();
95 14bd7976 Leszek Koltunski
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
96 47ba5ddc Leszek Koltunski
     }
97
98 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
99
// various things are done here delayed, 'after the next render' as not to be done mid-render and
100
// cause artifacts.
101
102 a7a7cc9c Leszek Koltunski
   @Override
103
   public void onDrawFrame(GL10 glUnused)
104
     {
105 7eae2d49 Leszek Koltunski
     long time = System.currentTimeMillis();
106
     mFPS.onRender(time);
107 5a4d4fba Leszek Koltunski
     mView.getPreRender().preRender();
108 7eae2d49 Leszek Koltunski
     mScreen.render(time);
109 a7a7cc9c Leszek Koltunski
     }
110 0c52af30 Leszek Koltunski
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112 aa8b36aa Leszek Koltunski
113
   @Override
114
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
115
      {
116 8becce57 Leszek Koltunski
      mScreen.resize(width,height);
117 123d6172 Leszek Koltunski
      mView.setScreenSize(width,height);
118 e06e1b7e Leszek Koltunski
      mView.getPreRender().setScreenSize(width);
119 aa8b36aa Leszek Koltunski
      }
120
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
123
   @Override
124
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
125
      {
126 c0460c5c Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX,61);    // 60 Minx quaternions + rotate
127 b4a9a34f Leszek Koltunski
      MeshBase.setMaxEffComponents(242);                // 242 moving parts (Gigaminx)
128
129 27e6c301 Leszek Koltunski
      VertexEffectRotate.enable();
130 98904e45 Leszek Koltunski
      VertexEffectQuaternion.enable();
131 64975793 Leszek Koltunski
      BaseEffect.Type.enableEffects();
132 aa8b36aa Leszek Koltunski
133 d7de3072 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
134 aa8b36aa Leszek Koltunski
      }
135
136 c1df2105 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
137
138
   public void distortedException(Exception ex)
139
     {
140
     String message = ex.getMessage();
141
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
142
     String version = GLES30.glGetString(GLES30.GL_VERSION);
143
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
144
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
145
146
     if( message==null ) message = "exception NULL";
147
148
     if( BuildConfig.DEBUG )
149
       {
150
       android.util.Log.e("DISTORTED", message );
151
       android.util.Log.e("DISTORTED", "GLSL Version "+shading);
152
       android.util.Log.e("DISTORTED", "GL Version "  +version);
153
       android.util.Log.e("DISTORTED", "GL Vendor "   +vendor);
154
       android.util.Log.e("DISTORTED", "GL Renderer " +renderer);
155
       }
156
     else
157
       {
158
       FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
159
       crashlytics.setCustomKey("DistortedError", message );
160
       crashlytics.setCustomKey("GLSL Version"  , shading );
161
       crashlytics.setCustomKey("GLversion"     , version );
162
       crashlytics.setCustomKey("GL Vendor "    , vendor  );
163
       crashlytics.setCustomKey("GLSLrenderer"  , renderer);
164
       crashlytics.recordException(ex);
165
       }
166
167
     int glsl = DistortedLibrary.getGLSL();
168
169 e7e0a94d Leszek Koltunski
     if( glsl< 300 && !mErrorShown )
170 c1df2105 Leszek Koltunski
       {
171 e7e0a94d Leszek Koltunski
       mErrorShown = true;
172 c1df2105 Leszek Koltunski
       RubikActivity act = (RubikActivity)mView.getContext();
173 e7e0a94d Leszek Koltunski
       act.OpenGLError();
174 c1df2105 Leszek Koltunski
       }
175
     }
176
177 7eae2d49 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
178
179
   float getFPS()
180
     {
181
     return mFPS.getFPS();
182
     }
183
184 aa8b36aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
185 0c52af30 Leszek Koltunski
186 8becce57 Leszek Koltunski
   DistortedScreen getScreen()
187 434f2f5a Leszek Koltunski
     {
188 64975793 Leszek Koltunski
     return mScreen;
189 434f2f5a Leszek Koltunski
     }
190 0c52af30 Leszek Koltunski
}