Project

General

Profile

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

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

1 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4 fdec60a3 Leszek Koltunski
// This file is part of Magic Cube.                                                              //
5 0c52af30 Leszek Koltunski
//                                                                                               //
6 fdec60a3 Leszek Koltunski
// Magic Cube is free software: you can redistribute it and/or modify                            //
7 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// Magic Cube is distributed in the hope that it will be useful,                                 //
12 0c52af30 Leszek Koltunski
// 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 fdec60a3 Leszek Koltunski
// along with Magic Cube.  If not, see <http://www.gnu.org/licenses/>.                           //
18 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 1f9772f3 Leszek Koltunski
package org.distorted.main;
21 0c52af30 Leszek Koltunski
22 31911113 Leszek Koltunski
import android.opengl.GLES30;
23 0c52af30 Leszek Koltunski
import android.opengl.GLSurfaceView;
24 31911113 Leszek Koltunski
25 1f9772f3 Leszek Koltunski
import org.distorted.effects.BaseEffect;
26 40ab026e Leszek Koltunski
import org.distorted.library.effect.EffectType;
27 98904e45 Leszek Koltunski
import org.distorted.library.effect.VertexEffectQuaternion;
28 27e6c301 Leszek Koltunski
import org.distorted.library.effect.VertexEffectRotate;
29 e1111500 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
30 0c52af30 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
31 42661133 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
32 6a083c6a Leszek Koltunski
import org.distorted.network.RubikNetwork;
33 0c52af30 Leszek Koltunski
34
import javax.microedition.khronos.egl.EGLConfig;
35
import javax.microedition.khronos.opengles.GL10;
36
37 31911113 Leszek Koltunski
import com.google.firebase.crashlytics.FirebaseCrashlytics;
38
39 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
40
41 c1df2105 Leszek Koltunski
public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
42 0c52af30 Leszek Koltunski
{
43 2e0258fe Leszek Koltunski
   public static final float BRIGHTNESS = 0.30f;
44
45 4b4c217e Leszek Koltunski
   private final RubikSurfaceView mView;
46
   private final DistortedScreen mScreen;
47
   private final Fps mFPS;
48 e7e0a94d Leszek Koltunski
   private boolean mErrorShown;
49 6a083c6a Leszek Koltunski
   private boolean mDebugSent;
50 7eae2d49 Leszek Koltunski
51
   private static class Fps
52
     {
53
     private static final int NUM_FRAMES  = 100;
54
55
     private long lastTime=0;
56 42661133 Leszek Koltunski
     private final long[] durations;
57 7eae2d49 Leszek Koltunski
     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 47ba5ddc Leszek Koltunski
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91 8becce57 Leszek Koltunski
   RubikRenderer(RubikSurfaceView v)
92 47ba5ddc Leszek Koltunski
     {
93 e7e0a94d Leszek Koltunski
     mErrorShown = false;
94 8becce57 Leszek Koltunski
     mView = v;
95 7eae2d49 Leszek Koltunski
     mFPS = new Fps();
96 8becce57 Leszek Koltunski
     mScreen = new DistortedScreen();
97 14bd7976 Leszek Koltunski
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
98 47ba5ddc Leszek Koltunski
     }
99
100 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
101
// various things are done here delayed, 'after the next render' as not to be done mid-render and
102
// cause artifacts.
103
104 a7a7cc9c Leszek Koltunski
   @Override
105
   public void onDrawFrame(GL10 glUnused)
106
     {
107 7eae2d49 Leszek Koltunski
     long time = System.currentTimeMillis();
108
     mFPS.onRender(time);
109 5a4d4fba Leszek Koltunski
     mView.getPreRender().preRender();
110 7eae2d49 Leszek Koltunski
     mScreen.render(time);
111 a7a7cc9c Leszek Koltunski
     }
112 0c52af30 Leszek Koltunski
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114 aa8b36aa Leszek Koltunski
115
   @Override
116
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
117
      {
118 8becce57 Leszek Koltunski
      mScreen.resize(width,height);
119 123d6172 Leszek Koltunski
      mView.setScreenSize(width,height);
120 e06e1b7e Leszek Koltunski
      mView.getPreRender().setScreenSize(width);
121 aa8b36aa Leszek Koltunski
      }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125
   @Override
126
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
127
      {
128 c0460c5c Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX,61);    // 60 Minx quaternions + rotate
129 b4a9a34f Leszek Koltunski
      MeshBase.setMaxEffComponents(242);                // 242 moving parts (Gigaminx)
130
131 27e6c301 Leszek Koltunski
      VertexEffectRotate.enable();
132 98904e45 Leszek Koltunski
      VertexEffectQuaternion.enable();
133 64975793 Leszek Koltunski
      BaseEffect.Type.enableEffects();
134 aa8b36aa Leszek Koltunski
135 d7de3072 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
136 6a083c6a Leszek Koltunski
137 52573991 Leszek Koltunski
      if( !BuildConfig.DEBUG && !mDebugSent )
138 6a083c6a Leszek Koltunski
        {
139
        mDebugSent= true;
140
        RubikNetwork network = RubikNetwork.getInstance();
141
        network.debug( (RubikActivity)mView.getContext());
142
        }
143 aa8b36aa Leszek Koltunski
      }
144
145 c1df2105 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e7e0a94d Leszek Koltunski
     if( glsl< 300 && !mErrorShown )
179 c1df2105 Leszek Koltunski
       {
180 e7e0a94d Leszek Koltunski
       mErrorShown = true;
181 c1df2105 Leszek Koltunski
       RubikActivity act = (RubikActivity)mView.getContext();
182 e7e0a94d Leszek Koltunski
       act.OpenGLError();
183 c1df2105 Leszek Koltunski
       }
184
     }
185
186 7eae2d49 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
187
188
   float getFPS()
189
     {
190
     return mFPS.getFPS();
191
     }
192
193 aa8b36aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
194 0c52af30 Leszek Koltunski
195 8becce57 Leszek Koltunski
   DistortedScreen getScreen()
196 434f2f5a Leszek Koltunski
     {
197 64975793 Leszek Koltunski
     return mScreen;
198 434f2f5a Leszek Koltunski
     }
199 0c52af30 Leszek Koltunski
}