Project

General

Profile

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

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

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.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

    
33
import javax.microedition.khronos.egl.EGLConfig;
34
import javax.microedition.khronos.opengles.GL10;
35

    
36
import com.google.firebase.crashlytics.FirebaseCrashlytics;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

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

    
44
   private final RubikSurfaceView mView;
45
   private final DistortedScreen mScreen;
46
   private final Fps mFPS;
47
   private boolean mErrorShown;
48

    
49
   private static class Fps
50
     {
51
     private static final int NUM_FRAMES  = 100;
52

    
53
     private long lastTime=0;
54
     private final long[] durations;
55
     private int currDuration;
56
     private float currFPS;
57

    
58
     Fps()
59
       {
60
       durations = new long[NUM_FRAMES+1];
61
       currDuration = 0;
62

    
63
       for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16;
64
       durations[NUM_FRAMES] = NUM_FRAMES * 16;
65
       }
66

    
67
     void onRender(long time)
68
       {
69
       if( lastTime==0 ) lastTime = time;
70

    
71
       currDuration++;
72
       if (currDuration >= NUM_FRAMES) currDuration = 0;
73
       durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
74
       durations[currDuration] = time - lastTime;
75

    
76
       currFPS = ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
77

    
78
       lastTime = time;
79
       }
80

    
81
     float getFPS()
82
       {
83
       return currFPS;
84
       }
85
     }
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
   RubikRenderer(RubikSurfaceView v)
90
     {
91
     mErrorShown = false;
92
     mView = v;
93
     mFPS = new Fps();
94
     mScreen = new DistortedScreen();
95
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
96
     }
97

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

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

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

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

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

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

    
129
      VertexEffectRotate.enable();
130
      VertexEffectQuaternion.enable();
131
      BaseEffect.Type.enableEffects();
132

    
133
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
134
      }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
   public void distortedException(Exception ex)
139
     {
140
     String message = ex.getMessage();
141
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
142
     String version = GLES30.glGetString(GLES30.GL_VERSION);
143
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
144
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
145

    
146
     if( message==null ) message = "exception NULL";
147

    
148
     if( BuildConfig.DEBUG )
149
       {
150
       android.util.Log.e("DISTORTED", message );
151
       android.util.Log.e("DISTORTED", "GLSL Version "+shading);
152
       android.util.Log.e("DISTORTED", "GL Version "  +version);
153
       android.util.Log.e("DISTORTED", "GL Vendor "   +vendor);
154
       android.util.Log.e("DISTORTED", "GL Renderer " +renderer);
155
       }
156
     else
157
       {
158
       FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
159
       crashlytics.setCustomKey("DistortedError", message );
160
       crashlytics.setCustomKey("GLSL Version"  , shading );
161
       crashlytics.setCustomKey("GLversion"     , version );
162
       crashlytics.setCustomKey("GL Vendor "    , vendor  );
163
       crashlytics.setCustomKey("GLSLrenderer"  , renderer);
164
       crashlytics.recordException(ex);
165
       }
166

    
167
     int glsl = DistortedLibrary.getGLSL();
168

    
169
     if( glsl< 300 && !mErrorShown )
170
       {
171
       mErrorShown = true;
172
       RubikActivity act = (RubikActivity)mView.getContext();
173
       act.OpenGLError();
174
       }
175
     }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
   float getFPS()
180
     {
181
     return mFPS.getFPS();
182
     }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
   DistortedScreen getScreen()
187
     {
188
     return mScreen;
189
     }
190
}
(3-3/4)