Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigRenderer.java @ 427ba5bf

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2019 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.config;
11

    
12
import static org.distorted.library.helpers.QuatHelper.rotateVectorByInvertedQuat;
13

    
14
import android.content.res.Resources;
15
import android.opengl.GLSurfaceView;
16

    
17
import org.distorted.library.effect.EffectType;
18
import org.distorted.library.effect.MatrixEffectRotate;
19
import org.distorted.library.effect.VertexEffectQuaternion;
20
import org.distorted.library.effect.VertexEffectRotate;
21
import org.distorted.library.main.DistortedLibrary;
22
import org.distorted.library.main.DistortedScreen;
23
import org.distorted.library.type.Static1D;
24
import org.distorted.library.type.Static3D;
25
import org.distorted.library.type.Static4D;
26
import org.distorted.objectlib.effects.BaseEffect;
27
import org.distorted.objectlib.main.ObjectControl;
28
import org.distorted.objectlib.main.TwistyObject;
29

    
30
import java.io.InputStream;
31

    
32
import javax.microedition.khronos.egl.EGLConfig;
33
import javax.microedition.khronos.opengles.GL10;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
public class ConfigRenderer implements GLSurfaceView.Renderer, DistortedLibrary.LibraryUser
38
{
39
   private static final int RESET_DURATION = 1000;
40

    
41
   private final ConfigSurfaceView mView;
42
   private final Resources mResources;
43
   private final DistortedScreen mScreen;
44
   private final ObjectControl mControl;
45
   private boolean mResettingObject, mInitialPhase, mEffectApplied;
46
   private long mStartTime;
47
   private final Static1D mAngle;
48
   private long mRotateID;
49

    
50
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
   ConfigRenderer(ConfigSurfaceView v)
53
     {
54
     final float BRIGHTNESS = 0.333f;
55

    
56
     mResettingObject = false;
57
     mEffectApplied   = false;
58
     mAngle = new Static1D(0);
59

    
60
     mView = v;
61
     mControl = mView.getObjectControl();
62

    
63
     mResources = v.getResources();
64
     mScreen = new DistortedScreen();
65
     mScreen.glClearColor(BRIGHTNESS, BRIGHTNESS, BRIGHTNESS, 1.0f);
66
     }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
   @Override
71
   public void onDrawFrame(GL10 glUnused)
72
     {
73
     long time = System.currentTimeMillis();
74
     mControl.preRender();
75
     mScreen.render(time);
76

    
77
     if( mResettingObject )
78
       {
79
       if( !mEffectApplied )
80
         {
81
         mEffectApplied = true;
82
         ObjectControl control = mView.getObjectControl();
83
         TwistyObject object = control.getObject();
84

    
85
         Static4D quaternion = object.getRotationQuat();                       // always rotate around
86
         Static4D tmpAxis    = new Static4D(0,-1,0,0);                         // vert axis no matter
87
         Static4D rotated    = rotateVectorByInvertedQuat(tmpAxis,quaternion); // how cube is rotated
88
         Static3D axis       = new Static3D(rotated.get0(), rotated.get1(), rotated.get2());
89
         Static3D center     = new Static3D(0,0,0);
90

    
91
         MatrixEffectRotate effect = new MatrixEffectRotate(mAngle, axis, center );
92
         object.applyEffect(effect,0);
93
         mRotateID = effect.getID();
94
         }
95

    
96
       boolean done = continueResetting(time);
97
       if( done )
98
         {
99
         mResettingObject = false;
100
         mEffectApplied   = false;
101

    
102
         ObjectControl control = mView.getObjectControl();
103
         TwistyObject object = control.getObject();
104
         object.removeEffect(mRotateID);
105
         }
106
       }
107
     }
108

    
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
   public void setupReset()
112
     {
113
     if( !mResettingObject )
114
       {
115
       mResettingObject = true;
116
       mInitialPhase    = true;
117
       mStartTime       = System.currentTimeMillis();
118
       }
119
     }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
   private boolean continueResetting(long time)
124
     {
125
     long diff = time-mStartTime;
126
     float quotient = ((float)diff)/RESET_DURATION;
127

    
128
     if( mInitialPhase && quotient>0.5f )
129
       {
130
       mInitialPhase=false;
131
       mView.resetObject();
132
       }
133

    
134
     float angle = 720*quotient*quotient*(3-2*quotient);
135
     mAngle.set( angle );
136

    
137
     return quotient>1.0f;
138
     }
139

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

    
142
   @Override
143
   public void onSurfaceChanged(GL10 glUnused, int width, int height)
144
      {
145
      mScreen.resize(width,height);
146
      mView.setScreenSize(width,height);
147
      }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

    
151
   DistortedScreen getScreen()
152
     {
153
     return mScreen;
154
     }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
   @Override
159
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
160
      {
161
      DistortedLibrary.setMax(EffectType.VERTEX, ObjectControl.MAX_QUATS+1);
162
      VertexEffectRotate.enable();
163
      VertexEffectQuaternion.enable();
164
      BaseEffect.Type.enableEffects();
165

    
166
      DistortedLibrary.onSurfaceCreated(this,1);
167
      DistortedLibrary.setCull(true);
168
      }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
   public void distortedException(Exception ex)
173
     {
174
     android.util.Log.e("Config", "unexpected exception: "+ex.getMessage() );
175
     }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
   public InputStream localFile(int fileID)
180
      {
181
      return mResources.openRawResource(fileID);
182
      }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
   public void logMessage(String message)
187
      {
188
      android.util.Log.e("Config", message );
189
      }
190
}
(3-3/6)