Project

General

Profile

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

magiccube / src / main / java / org / distorted / main / RubikRenderer.java @ 85038b84

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, mEmptyScreen;
49
   private final ObjectControl mControl;
50
   private final Fps mFPS;
51
   private boolean mErrorShown;
52
   private boolean mDebugSent;
53
   private boolean mRenderingOn;
54

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

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

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

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

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

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

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

    
84
       lastTime = time;
85
       }
86

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

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
   RubikRenderer(RubikSurfaceView v)
96
     {
97
     mRenderingOn= true;
98
     mErrorShown = false;
99
     mView = v;
100
     mControl = v.getObjectControl();
101
     mFPS = new Fps();
102

    
103
     mScreen = new DistortedScreen();
104
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
105
     mEmptyScreen = new DistortedScreen();
106
     mEmptyScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
107
     }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
// various things are done here delayed, 'after the next render' as not to be done mid-render and
111
// cause artifacts.
112

    
113
   @Override
114
   public void onDrawFrame(GL10 glUnused)
115
     {
116
     long time = System.currentTimeMillis();
117

    
118
     if( mRenderingOn )
119
       {
120
       mFPS.onRender(time);
121
       mControl.preRender();
122
       mScreen.render(time);
123
       }
124
     else
125
       {
126
       mEmptyScreen.render(time);
127
       }
128
     }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
   @Override
133
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
134
      {
135
      mScreen.resize(width,height);
136
      mEmptyScreen.resize(width,height);
137
      mView.setScreenSize(width,height);
138
      }
139

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

    
142
   @Override
143
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
144
      {
145
      DistortedLibrary.setMax(EffectType.VERTEX,ObjectControl.MAX_QUATS+1);
146
      MeshBase.setMaxEffComponents(ObjectControl.MAX_MOVING_PARTS);
147

    
148
      VertexEffectRotate.enable();
149
      VertexEffectQuaternion.enable();
150
      BaseEffect.Type.enableEffects();
151

    
152
      DistortedLibrary.onSurfaceCreated(mView.getContext(),this,1);
153
      DistortedLibrary.setCull(true);
154

    
155
      if( !mDebugSent )
156
        {
157
        mDebugSent= true;
158
        Activity act = (Activity)mView.getContext();
159
        RubikNetwork network = RubikNetwork.getInstance();
160
        network.debug(act);
161
        }
162
      }
163

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

    
166
   public void distortedException(Exception ex)
167
     {
168
     String message = ex.getMessage();
169
     String shading = GLES30.glGetString(GLES30.GL_SHADING_LANGUAGE_VERSION);
170
     String version = GLES30.glGetString(GLES30.GL_VERSION);
171
     String vendor  = GLES30.glGetString(GLES30.GL_VENDOR);
172
     String renderer= GLES30.glGetString(GLES30.GL_RENDERER);
173

    
174
     if( message==null ) message = "exception NULL";
175

    
176
     if( BuildConfig.DEBUG )
177
       {
178
       android.util.Log.e("DISTORTED", message );
179
       android.util.Log.e("DISTORTED", "GLSL Version "+shading);
180
       android.util.Log.e("DISTORTED", "GL Version "  +version);
181
       android.util.Log.e("DISTORTED", "GL Vendor "   +vendor);
182
       android.util.Log.e("DISTORTED", "GL Renderer " +renderer);
183
       }
184
     else
185
       {
186
       FirebaseCrashlytics crashlytics = FirebaseCrashlytics.getInstance();
187
       crashlytics.setCustomKey("DistortedError", message );
188
       crashlytics.setCustomKey("GLSL Version"  , shading );
189
       crashlytics.setCustomKey("GLversion"     , version );
190
       crashlytics.setCustomKey("GL Vendor "    , vendor  );
191
       crashlytics.setCustomKey("GLSLrenderer"  , renderer);
192
       crashlytics.recordException(ex);
193
       }
194

    
195
     int glsl = DistortedLibrary.getGLSL();
196

    
197
     if( glsl< 300 && !mErrorShown )
198
       {
199
       mErrorShown = true;
200
       RubikActivity act = (RubikActivity)mView.getContext();
201
       act.OpenGLError();
202
       }
203
     }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
   void switchRendering(boolean on)
208
     {
209
     mRenderingOn = on;
210
     }
211

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

    
214
   float getFPS()
215
     {
216
     return mFPS.getFPS();
217
     }
218

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

    
221
   DistortedScreen getScreen()
222
     {
223
     return mScreen;
224
     }
225
}
(3-3/4)