Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is free software: you can redistribute it and/or modify                            //
7
// 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
// Magic Cube is distributed in the hope that it will be useful,                                 //
12
// 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
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19

    
20
package org.distorted.main;
21

    
22
import android.opengl.GLES30;
23
import android.opengl.GLSurfaceView;
24

    
25
import org.distorted.objectlib.effects.BaseEffect;
26
import org.distorted.library.effect.EffectType;
27
import org.distorted.library.effect.VertexEffectQuaternion;
28
import org.distorted.library.effect.VertexEffectRotate;
29
import org.distorted.library.main.DistortedLibrary;
30
import org.distorted.library.main.DistortedScreen;
31
import org.distorted.library.mesh.MeshBase;
32
import org.distorted.external.RubikNetwork;
33
import org.distorted.objectlib.main.ObjectControl;
34

    
35
import javax.microedition.khronos.egl.EGLConfig;
36
import javax.microedition.khronos.opengles.GL10;
37

    
38
import com.google.firebase.crashlytics.FirebaseCrashlytics;
39

    
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
43
{
44
   public static final float BRIGHTNESS = 0.30f;
45

    
46
   private final RubikSurfaceView mView;
47
   private final DistortedScreen mScreen;
48
   private final ObjectControl mControl;
49
   private final Fps mFPS;
50
   private boolean mErrorShown;
51
   private boolean mDebugSent;
52

    
53
   private static class Fps
54
     {
55
     private static final int NUM_FRAMES  = 100;
56

    
57
     private long lastTime=0;
58
     private final long[] durations;
59
     private int currDuration;
60
     private float currFPS;
61

    
62
     Fps()
63
       {
64
       durations = new long[NUM_FRAMES+1];
65
       currDuration = 0;
66

    
67
       for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16;
68
       durations[NUM_FRAMES] = NUM_FRAMES * 16;
69
       }
70

    
71
     void onRender(long time)
72
       {
73
       if( lastTime==0 ) lastTime = time;
74

    
75
       currDuration++;
76
       if (currDuration >= NUM_FRAMES) currDuration = 0;
77
       durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
78
       durations[currDuration] = time - lastTime;
79

    
80
       currFPS = ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
81

    
82
       lastTime = time;
83
       }
84

    
85
     float getFPS()
86
       {
87
       return currFPS;
88
       }
89
     }
90

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

    
93
   RubikRenderer(RubikSurfaceView v)
94
     {
95
     mErrorShown = false;
96
     mView = v;
97
     mControl = v.getObjectControl();
98
     mFPS = new Fps();
99
     mScreen = new DistortedScreen();
100
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
101
     }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104
// various things are done here delayed, 'after the next render' as not to be done mid-render and
105
// cause artifacts.
106

    
107
   @Override
108
   public void onDrawFrame(GL10 glUnused)
109
     {
110
     long time = System.currentTimeMillis();
111
     mFPS.onRender(time);
112
     mControl.preRender();
113
     mScreen.render(time);
114
     }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
   @Override
119
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
120
      {
121
      mScreen.resize(width,height);
122
      mView.setScreenSize(width,height);
123
      }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
   @Override
128
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
129
      {
130
      DistortedLibrary.setMax(EffectType.VERTEX,61);    // 60 Minx quaternions + rotate
131
      MeshBase.setMaxEffComponents(242);                // 242 moving parts (Gigaminx)
132

    
133
      VertexEffectRotate.enable();
134
      VertexEffectQuaternion.enable();
135
      BaseEffect.Type.enableEffects();
136

    
137
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
138

    
139
      if( /*!BuildConfig.DEBUG &&*/ !mDebugSent )
140
        {
141
        mDebugSent= true;
142
        RubikNetwork network = RubikNetwork.getInstance();
143
        network.debug();
144
        }
145
      }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
   public void distortedException(Exception ex)
150
     {
151
     String message = ex.getMessage();
152
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
153
     String version = GLES30.glGetString(GLES30.GL_VERSION);
154
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
155
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
156

    
157
     if( message==null ) message = "exception NULL";
158

    
159
     if( BuildConfig.DEBUG )
160
       {
161
       android.util.Log.e("DISTORTED", message );
162
       android.util.Log.e("DISTORTED", "GLSL Version "+shading);
163
       android.util.Log.e("DISTORTED", "GL Version "  +version);
164
       android.util.Log.e("DISTORTED", "GL Vendor "   +vendor);
165
       android.util.Log.e("DISTORTED", "GL Renderer " +renderer);
166
       }
167
     else
168
       {
169
       FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
170
       crashlytics.setCustomKey("DistortedError", message );
171
       crashlytics.setCustomKey("GLSL Version"  , shading );
172
       crashlytics.setCustomKey("GLversion"     , version );
173
       crashlytics.setCustomKey("GL Vendor "    , vendor  );
174
       crashlytics.setCustomKey("GLSLrenderer"  , renderer);
175
       crashlytics.recordException(ex);
176
       }
177

    
178
     int glsl = DistortedLibrary.getGLSL();
179

    
180
     if( glsl< 300 && !mErrorShown )
181
       {
182
       mErrorShown = true;
183
       RubikActivity act = (RubikActivity)mView.getContext();
184
       act.OpenGLError();
185
       }
186
     }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189

    
190
   float getFPS()
191
     {
192
     return mFPS.getFPS();
193
     }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196

    
197
   DistortedScreen getScreen()
198
     {
199
     return mScreen;
200
     }
201
}
(3-3/4)