Project

General

Profile

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

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

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.ActivityManager;
23
import android.content.Context;
24
import android.content.pm.ConfigurationInfo;
25
import android.opengl.GLES30;
26
import android.opengl.GLSurfaceView;
27

    
28
import org.distorted.effects.BaseEffect;
29
import org.distorted.library.effect.EffectType;
30
import org.distorted.library.effect.VertexEffectQuaternion;
31
import org.distorted.library.effect.VertexEffectRotate;
32
import org.distorted.library.main.DistortedLibrary;
33
import org.distorted.library.main.DistortedScreen;
34
import org.distorted.library.mesh.MeshBase;
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 Fps mFPS;
50
   private boolean mErrorShown;
51
   private static boolean mSupportsGigaminx;
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
     mFPS = new Fps();
98
     mScreen = new DistortedScreen();
99
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
100
     }
101

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

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

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

    
117
   @Override
118
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
119
      {
120
      mScreen.resize(width,height);
121
      mView.setScreenSize(width,height);
122
      mView.getPreRender().setScreenSize(width);
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
      VertexEffectRotate.enable();
132
      VertexEffectQuaternion.enable();
133
      BaseEffect.Type.enableEffects();
134

    
135
      assignMaxComponents();
136

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

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
   public void distortedException(Exception ex)
143
     {
144
     String message = ex.getMessage();
145
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
146
     String version = GLES30.glGetString(GLES30.GL_VERSION);
147
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
148
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
149

    
150
     if( message==null ) message = "exception NULL";
151

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

    
171
     int glsl = DistortedLibrary.getGLSL();
172

    
173
     if( glsl< 300 && !mErrorShown )
174
       {
175
       mErrorShown = true;
176
       RubikActivity act = (RubikActivity)mView.getContext();
177
       act.OpenGLError();
178
       }
179
     }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
   private void assignMaxComponents()
184
     {
185
     Context context = mView.getContext();
186

    
187
     final ActivityManager activityManager     = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
188
     final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
189

    
190
     int glESversion = configurationInfo.reqGlEsVersion;
191
     int major = glESversion >> 16;
192
     int minor = glESversion & 0xff;
193

    
194
     MeshBase.setMaxEffComponents(242);   // the largest object, Gigaminx, has 242 components
195
     mSupportsGigaminx = true;
196

    
197
     if( major==3 && minor==0 )
198
       {
199
       String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
200
       String version = GLES30.glGetString(GLES30.GL_VERSION);
201

    
202
       if( vendor.contains("Qualcomm") && version.contains("V@331") )
203
         {
204
         // on driver version 331,  do not support Gigaminx.
205
         // Then Cube5 is the largest with 98 components.
206
         MeshBase.setMaxEffComponents(98);
207
         mSupportsGigaminx = false;
208
         }
209
       }
210
     }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
   public static boolean supportsGigaminx()
215
     {
216
     return mSupportsGigaminx;
217
     }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
   float getFPS()
222
     {
223
     return mFPS.getFPS();
224
     }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
   DistortedScreen getScreen()
229
     {
230
     return mScreen;
231
     }
232
}
(3-3/4)