Project

General

Profile

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

magiccube / src / main / java / org / distorted / config / ConfigRenderer.java @ 58fd2ec0

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

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

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

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

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

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

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68

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

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

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

    
90
         MatrixEffectRotate effect = new MatrixEffectRotate(mAngle, axis, center );
91
         object.applyEffect(effect,0);
92
         }
93

    
94
       boolean done = continueResetting(time);
95
       if( done ) mResettingObject = false;
96
       }
97
     }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
   public void setupReset()
102
     {
103
     if( !mResettingObject )
104
       {
105
       mResettingObject = true;
106
       mInitialPhase    = true;
107
       mStartTime       = System.currentTimeMillis();
108
       }
109
     }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
   private boolean continueResetting(long time)
114
     {
115
     long diff = time-mStartTime;
116
     float quotient = ((float)diff)/RESET_DURATION;
117

    
118
     if( mInitialPhase && quotient>0.5f )
119
       {
120
       mInitialPhase=false;
121
       mView.resetObject();
122
       }
123

    
124
     float angle = 720*quotient*quotient*(3-2*quotient);
125
     mAngle.set( angle );
126

    
127
     return quotient>1.0f;
128
     }
129

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

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

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
   DistortedScreen getScreen()
142
     {
143
     return mScreen;
144
     }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
   @Override
149
   public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
150
      {
151
      DistortedLibrary.setMax(EffectType.VERTEX, ObjectControl.MAX_QUATS+1);
152
      VertexEffectRotate.enable();
153
      VertexEffectQuaternion.enable();
154
      BaseEffect.Type.enableEffects();
155

    
156
      DistortedLibrary.onSurfaceCreated(this,1);
157
      DistortedLibrary.setCull(true);
158
      }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
   public void distortedException(Exception ex)
163
     {
164
     android.util.Log.e("Config", "unexpected exception: "+ex.getMessage() );
165
     }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
   public InputStream localFile(int fileID)
170
      {
171
      return mResources.openRawResource(fileID);
172
      }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
   public void logMessage(String message)
177
      {
178
      android.util.Log.e("Config", message );
179
      }
180
}
(3-3/6)