Project

General

Profile

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

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

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.app.Activity;
23
import android.opengl.GLES30;
24
import android.opengl.GLSurfaceView;
25

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

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

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

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

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

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

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

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

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

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

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

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

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

    
83
       lastTime = time;
84
       }
85

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

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

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

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

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

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

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

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

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

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

    
138
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
139
      DistortedLibrary.setCull(true);
140

    
141
      if( !mDebugSent )
142
        {
143
        mDebugSent= true;
144
        Activity act = (Activity)mView.getContext();
145
        RubikNetwork network = RubikNetwork.getInstance();
146
        network.debug(act);
147
        }
148
      }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

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

    
160
     if( message==null ) message = "exception NULL";
161

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

    
181
     int glsl = DistortedLibrary.getGLSL();
182

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

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

    
193
   float getFPS()
194
     {
195
     return mFPS.getFPS();
196
     }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
   DistortedScreen getScreen()
201
     {
202
     return mScreen;
203
     }
204
}
(3-3/4)