Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.main;
11

    
12
import android.app.Activity;
13
import android.opengl.GLES30;
14
import android.opengl.GLSurfaceView;
15

    
16
import org.distorted.library.main.InternalOutputSurface;
17
import org.distorted.objectlib.effects.BaseEffect;
18
import org.distorted.library.effect.EffectType;
19
import org.distorted.library.effect.VertexEffectQuaternion;
20
import org.distorted.library.effect.VertexEffectRotate;
21
import org.distorted.library.main.DistortedLibrary;
22
import org.distorted.library.main.DistortedScreen;
23
import org.distorted.library.mesh.MeshBase;
24
import org.distorted.external.RubikNetwork;
25
import org.distorted.objectlib.main.ObjectControl;
26
import org.distorted.overlays.OverlayGeneric;
27

    
28
import javax.microedition.khronos.egl.EGLConfig;
29
import javax.microedition.khronos.opengles.GL10;
30

    
31
import com.google.firebase.crashlytics.FirebaseCrashlytics;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
36
{
37
   public static final float BRIGHTNESS = 0.30f;
38

    
39
   private final RubikSurfaceView mView;
40
   private final DistortedScreen mScreen;
41
   private final ObjectControl mControl;
42
   private final Fps mFPS;
43
   private boolean mErrorShown;
44
   private boolean mDebugSent;
45

    
46
   private static class Fps
47
     {
48
     private static final int NUM_FRAMES  = 100;
49

    
50
     private long lastTime=0;
51
     private final long[] durations;
52
     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

    
84
///////////////////////////////////////////////////////////////////////////////////////////////////
85

    
86
   RubikRenderer(RubikSurfaceView v)
87
     {
88
     mErrorShown = false;
89
     mView = v;
90
     mControl = v.getObjectControl();
91
     mFPS = new Fps();
92
     mScreen = new DistortedScreen();
93
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
94
     mScreen.enableDepthStencil(InternalOutputSurface.DEPTH_NO_STENCIL);
95
     }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// various things are done here delayed, 'after the next render' as not to be done mid-render and
99
// cause artifacts.
100

    
101
   @Override
102
   public void onDrawFrame(GL10 glUnused)
103
     {
104
     long time = System.currentTimeMillis();
105
     mFPS.onRender(time);
106
     mControl.preRender();
107
     mScreen.render(time);
108
     }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
   @Override
113
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
114
      {
115
      mScreen.resize(width,height);
116
      mView.setScreenSize(width,height);
117
      }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
   @Override
122
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
123
      {
124
      DistortedLibrary.setMax(EffectType.VERTEX,ObjectControl.MAX_QUATS+1);
125
      MeshBase.setMaxEffComponents(ObjectControl.MAX_MOVING_PARTS);
126

    
127
      VertexEffectRotate.enable();
128
      VertexEffectQuaternion.enable();
129
      BaseEffect.Type.enableEffects();
130
      OverlayGeneric.enableEffects();
131

    
132
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
133
      DistortedLibrary.setCull(true);
134

    
135
      if( !mDebugSent )
136
        {
137
        mDebugSent= true;
138
        Activity act = (Activity)mView.getContext();
139
        RubikNetwork network = RubikNetwork.getInstance();
140
        network.debug(act);
141
        }
142
      }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
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
     if( glsl< 300 && !mErrorShown )
178
       {
179
       mErrorShown = true;
180
       RubikActivity act = (RubikActivity)mView.getContext();
181
       act.OpenGLError();
182
       }
183
     }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186

    
187
   float getFPS()
188
     {
189
     return mFPS.getFPS();
190
     }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193

    
194
   DistortedScreen getScreen()
195
     {
196
     return mScreen;
197
     }
198
}
(3-3/4)