Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikRenderer.java @ 6b1998e0

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