Project

General

Profile

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

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

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