Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikRenderer.java @ 935f3663

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
32
import javax.microedition.khronos.egl.EGLConfig;
33
import javax.microedition.khronos.opengles.GL10;
34
35 31911113 Leszek Koltunski
import com.google.firebase.crashlytics.FirebaseCrashlytics;
36
37 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
38
39 c1df2105 Leszek Koltunski
public class RubikRenderer implements GLSurfaceView.Renderer, DistortedLibrary.ExceptionListener
40 0c52af30 Leszek Koltunski
{
41 8becce57 Leszek Koltunski
   private RubikSurfaceView mView;
42
   private DistortedScreen mScreen;
43 7eae2d49 Leszek Koltunski
   private Fps mFPS;
44
45
   private static class Fps
46
     {
47
     private static final int NUM_FRAMES  = 100;
48
49
     private long lastTime=0;
50
     private long[] durations;
51
     private int currDuration;
52
     private float currFPS;
53
54
     Fps()
55
       {
56
       durations = new long[NUM_FRAMES+1];
57
       currDuration = 0;
58
59
       for (int i=0; i<NUM_FRAMES+1; i++) durations[i] = 16;
60
       durations[NUM_FRAMES] = NUM_FRAMES * 16;
61
       }
62
63
     void onRender(long time)
64
       {
65
       if( lastTime==0 ) lastTime = time;
66
67
       currDuration++;
68
       if (currDuration >= NUM_FRAMES) currDuration = 0;
69
       durations[NUM_FRAMES] += ((time - lastTime) - durations[currDuration]);
70
       durations[currDuration] = time - lastTime;
71
72
       currFPS = ((int)(10000.0f*NUM_FRAMES/durations[NUM_FRAMES]))/10.0f;
73
74
       lastTime = time;
75
       }
76
77
     float getFPS()
78
       {
79
       return currFPS;
80
       }
81
     }
82 47ba5ddc Leszek Koltunski
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
85 8becce57 Leszek Koltunski
   RubikRenderer(RubikSurfaceView v)
86 47ba5ddc Leszek Koltunski
     {
87 5b893eee Leszek Koltunski
     final float BRIGHTNESS = 0.19f;
88 14bd7976 Leszek Koltunski
89 8becce57 Leszek Koltunski
     mView = v;
90 7eae2d49 Leszek Koltunski
     mFPS = new Fps();
91 8becce57 Leszek Koltunski
     mScreen = new DistortedScreen();
92 14bd7976 Leszek Koltunski
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
93 47ba5ddc Leszek Koltunski
     }
94
95 0c52af30 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
96
// various things are done here delayed, 'after the next render' as not to be done mid-render and
97
// cause artifacts.
98
99 a7a7cc9c Leszek Koltunski
   @Override
100
   public void onDrawFrame(GL10 glUnused)
101
     {
102 7eae2d49 Leszek Koltunski
     long time = System.currentTimeMillis();
103
     mFPS.onRender(time);
104 5a4d4fba Leszek Koltunski
     mView.getPreRender().preRender();
105 7eae2d49 Leszek Koltunski
     mScreen.render(time);
106 a7a7cc9c Leszek Koltunski
     }
107 0c52af30 Leszek Koltunski
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109 aa8b36aa Leszek Koltunski
110
   @Override
111
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
112
      {
113 8becce57 Leszek Koltunski
      mScreen.resize(width,height);
114 123d6172 Leszek Koltunski
      mView.setScreenSize(width,height);
115 5a4d4fba Leszek Koltunski
      mView.getPreRender().setScreenSize(width,height);
116 aa8b36aa Leszek Koltunski
      }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
   @Override
121
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
122
      {
123 10585385 Leszek Koltunski
      DistortedLibrary.setMax(EffectType.VERTEX,25);    // 24 Cube Quats + Rotate
124 27e6c301 Leszek Koltunski
      VertexEffectRotate.enable();
125 98904e45 Leszek Koltunski
      VertexEffectQuaternion.enable();
126 64975793 Leszek Koltunski
      BaseEffect.Type.enableEffects();
127 aa8b36aa Leszek Koltunski
128 c1df2105 Leszek Koltunski
      DistortedLibrary.onCreate(mView.getContext(),this,1);
129 aa8b36aa Leszek Koltunski
      }
130
131 c1df2105 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
132
133
   public void distortedException(Exception ex)
134
     {
135
     String message = ex.getMessage();
136
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
137
     String version = GLES30.glGetString(GLES30.GL_VERSION);
138
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
139
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
140
141
     if( message==null ) message = "exception NULL";
142
143
     if( BuildConfig.DEBUG )
144
       {
145
       android.util.Log.e("DISTORTED", message );
146
       android.util.Log.e("DISTORTED", "GLSL Version "+shading);
147
       android.util.Log.e("DISTORTED", "GL Version "  +version);
148
       android.util.Log.e("DISTORTED", "GL Vendor "   +vendor);
149
       android.util.Log.e("DISTORTED", "GL Renderer " +renderer);
150
       }
151
     else
152
       {
153
       FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
154
       crashlytics.setCustomKey("DistortedError", message );
155
       crashlytics.setCustomKey("GLSL Version"  , shading );
156
       crashlytics.setCustomKey("GLversion"     , version );
157
       crashlytics.setCustomKey("GL Vendor "    , vendor  );
158
       crashlytics.setCustomKey("GLSLrenderer"  , renderer);
159
       crashlytics.recordException(ex);
160
       }
161
162
     int glsl = DistortedLibrary.getGLSL();
163
164
     if( glsl< 300 )
165
       {
166
       RubikActivity act = (RubikActivity)mView.getContext();
167
       act.OpenGLError(message);
168
       }
169
     }
170
171 7eae2d49 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
172
173
   float getFPS()
174
     {
175
     return mFPS.getFPS();
176
     }
177
178 aa8b36aa Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
179 0c52af30 Leszek Koltunski
180 8becce57 Leszek Koltunski
   DistortedScreen getScreen()
181 434f2f5a Leszek Koltunski
     {
182 64975793 Leszek Koltunski
     return mScreen;
183 434f2f5a Leszek Koltunski
     }
184 0c52af30 Leszek Koltunski
}