Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikRenderer.java @ 306aa049

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