Project

General

Profile

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

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

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 314bffaf Leszek Koltunski
import org.distorted.control.RubikControl;
26 d2556e79 Leszek Koltunski
import org.distorted.objectlib.effects.BaseEffect;
27 40ab026e Leszek Koltunski
import org.distorted.library.effect.EffectType;
28 98904e45 Leszek Koltunski
import org.distorted.library.effect.VertexEffectQuaternion;
29 27e6c301 Leszek Koltunski
import org.distorted.library.effect.VertexEffectRotate;
30 e1111500 Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
31 0c52af30 Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
32 42661133 Leszek Koltunski
import org.distorted.library.mesh.MeshBase;
33 6a083c6a Leszek Koltunski
import org.distorted.network.RubikNetwork;
34 0c52af30 Leszek Koltunski
35
import javax.microedition.khronos.egl.EGLConfig;
36
import javax.microedition.khronos.opengles.GL10;
37
38 31911113 Leszek Koltunski
import com.google.firebase.crashlytics.FirebaseCrashlytics;
39
40 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42 c1df2105 Leszek Koltunski
public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
43 0c52af30 Leszek Koltunski
{
44 2e0258fe Leszek Koltunski
   public static final float BRIGHTNESS = 0.30f;
45
46 4b4c217e Leszek Koltunski
   private final RubikSurfaceView mView;
47
   private final DistortedScreen mScreen;
48
   private final Fps mFPS;
49 e7e0a94d Leszek Koltunski
   private boolean mErrorShown;
50 6a083c6a Leszek Koltunski
   private boolean mDebugSent;
51 314bffaf Leszek Koltunski
   private RubikControl mControl;
52 7eae2d49 Leszek Koltunski
53
   private static class Fps
54
     {
55
     private static final int NUM_FRAMES  = 100;
56
57
     private long lastTime=0;
58 42661133 Leszek Koltunski
     private final long[] durations;
59 7eae2d49 Leszek Koltunski
     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 47ba5ddc Leszek Koltunski
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92
93 8becce57 Leszek Koltunski
   RubikRenderer(RubikSurfaceView v)
94 47ba5ddc Leszek Koltunski
     {
95 e7e0a94d Leszek Koltunski
     mErrorShown = false;
96 8becce57 Leszek Koltunski
     mView = v;
97 7eae2d49 Leszek Koltunski
     mFPS = new Fps();
98 8becce57 Leszek Koltunski
     mScreen = new DistortedScreen();
99 14bd7976 Leszek Koltunski
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
100 47ba5ddc Leszek Koltunski
     }
101
102 314bffaf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104
   public void setControlState(boolean on)
105
     {
106
     mControl = on ? RubikControl.getInstance() : null;
107
     }
108
109 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
110
// various things are done here delayed, 'after the next render' as not to be done mid-render and
111
// cause artifacts.
112
113 a7a7cc9c Leszek Koltunski
   @Override
114
   public void onDrawFrame(GL10 glUnused)
115
     {
116 7eae2d49 Leszek Koltunski
     long time = System.currentTimeMillis();
117
     mFPS.onRender(time);
118 5a4d4fba Leszek Koltunski
     mView.getPreRender().preRender();
119 7eae2d49 Leszek Koltunski
     mScreen.render(time);
120 314bffaf Leszek Koltunski
121
     if( mControl!=null ) mControl.postDrawFrame(time);
122 a7a7cc9c Leszek Koltunski
     }
123 0c52af30 Leszek Koltunski
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125 aa8b36aa Leszek Koltunski
126
   @Override
127
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
128
      {
129 8becce57 Leszek Koltunski
      mScreen.resize(width,height);
130 123d6172 Leszek Koltunski
      mView.setScreenSize(width,height);
131 e06e1b7e Leszek Koltunski
      mView.getPreRender().setScreenSize(width);
132 aa8b36aa Leszek Koltunski
      }
133
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
136
   @Override
137
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
138
      {
139 c0460c5c Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX,61);    // 60 Minx quaternions + rotate
140 b4a9a34f Leszek Koltunski
      MeshBase.setMaxEffComponents(242);                // 242 moving parts (Gigaminx)
141
142 27e6c301 Leszek Koltunski
      VertexEffectRotate.enable();
143 98904e45 Leszek Koltunski
      VertexEffectQuaternion.enable();
144 64975793 Leszek Koltunski
      BaseEffect.Type.enableEffects();
145 aa8b36aa Leszek Koltunski
146 d7de3072 Leszek Koltunski
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
147 6a083c6a Leszek Koltunski
148 52573991 Leszek Koltunski
      if( !BuildConfig.DEBUG && !mDebugSent )
149 6a083c6a Leszek Koltunski
        {
150
        mDebugSent= true;
151
        RubikNetwork network = RubikNetwork.getInstance();
152
        network.debug( (RubikActivity)mView.getContext());
153
        }
154 aa8b36aa Leszek Koltunski
      }
155
156 c1df2105 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
157
158
   public void distortedException(Exception ex)
159
     {
160
     String message = ex.getMessage();
161
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
162
     String version = GLES30.glGetString(GLES30.GL_VERSION);
163
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
164
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
165
166
     if( message==null ) message = "exception NULL";
167
168
     if( BuildConfig.DEBUG )
169
       {
170
       android.util.Log.e("DISTORTED", message );
171
       android.util.Log.e("DISTORTED", "GLSL Version "+shading);
172
       android.util.Log.e("DISTORTED", "GL Version "  +version);
173
       android.util.Log.e("DISTORTED", "GL Vendor "   +vendor);
174
       android.util.Log.e("DISTORTED", "GL Renderer " +renderer);
175
       }
176
     else
177
       {
178
       FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
179
       crashlytics.setCustomKey("DistortedError", message );
180
       crashlytics.setCustomKey("GLSL Version"  , shading );
181
       crashlytics.setCustomKey("GLversion"     , version );
182
       crashlytics.setCustomKey("GL Vendor "    , vendor  );
183
       crashlytics.setCustomKey("GLSLrenderer"  , renderer);
184
       crashlytics.recordException(ex);
185
       }
186
187
     int glsl = DistortedLibrary.getGLSL();
188
189 e7e0a94d Leszek Koltunski
     if( glsl< 300 && !mErrorShown )
190 c1df2105 Leszek Koltunski
       {
191 e7e0a94d Leszek Koltunski
       mErrorShown = true;
192 c1df2105 Leszek Koltunski
       RubikActivity act = (RubikActivity)mView.getContext();
193 e7e0a94d Leszek Koltunski
       act.OpenGLError();
194 c1df2105 Leszek Koltunski
       }
195
     }
196
197 7eae2d49 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
198
199
   float getFPS()
200
     {
201
     return mFPS.getFPS();
202
     }
203
204 aa8b36aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
205 0c52af30 Leszek Koltunski
206 8becce57 Leszek Koltunski
   DistortedScreen getScreen()
207 434f2f5a Leszek Koltunski
     {
208 64975793 Leszek Koltunski
     return mScreen;
209 434f2f5a Leszek Koltunski
     }
210 0c52af30 Leszek Koltunski
}