Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikRenderer.java @ 6a083c6a

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
import org.distorted.network.RubikNetwork;
33

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

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

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

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

    
45
   private final RubikSurfaceView mView;
46
   private final DistortedScreen mScreen;
47
   private final Fps mFPS;
48
   private boolean mErrorShown;
49
   private boolean mDebugSent;
50

    
51
   private static class Fps
52
     {
53
     private static final int NUM_FRAMES  = 100;
54

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

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

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

    
69
     void onRender(long time)
70
       {
71
       if( lastTime==0 ) lastTime = time;
72

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

    
78
       currFPS = ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
79

    
80
       lastTime = time;
81
       }
82

    
83
     float getFPS()
84
       {
85
       return currFPS;
86
       }
87
     }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

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

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

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

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

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

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

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

    
131
      VertexEffectRotate.enable();
132
      VertexEffectQuaternion.enable();
133
      BaseEffect.Type.enableEffects();
134

    
135
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
136

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

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

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

    
155
     if( message==null ) message = "exception NULL";
156

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

    
176
     int glsl = DistortedLibrary.getGLSL();
177

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

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187

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

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

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