23
23
package processing .vr ;
24
24
25
25
import com .google .vr .sdk .base .Eye ;
26
- import com .google .vr .sdk .base .FieldOfView ;
27
26
import com .google .vr .sdk .base .HeadTransform ;
28
27
import com .google .vr .sdk .base .Viewport ;
29
28
30
- import processing .core .PMatrix3D ;
29
+ import processing .core .PApplet ;
30
+ import processing .core .PGraphics ;
31
31
import processing .opengl .PGL ;
32
32
import processing .opengl .PGLES ;
33
33
import processing .opengl .PGraphics3D ;
36
36
public class PGraphicsVR extends PGraphics3D {
37
37
private boolean initialized = false ;
38
38
39
- // Head properties, independent of eye view
39
+ public HeadTransform headTransform ;
40
+ public Eye eye ;
40
41
public int eyeType ;
41
- public float [] headView ;
42
- public float [] headRotation ;
43
- public float [] translationVector ;
44
- public float [] forwardVector ;
45
- public float [] rightVector ;
46
- public float [] upVector ;
42
+ public float forwardX ;
43
+ public float forwardY ;
44
+ public float forwardZ ;
47
45
48
46
// Eye properties
49
- public FieldOfView eyeFov ;
50
- public Viewport eyeViewPort ;
51
- public float [] eyeView ;
52
- public float [] eyePerspective ;
53
-
54
- private PMatrix3D viewMatrix ;
55
- private PMatrix3D perspectiveMatrix ;
47
+ private Viewport eyeViewport ;
48
+ private float [] forwardVector ;
49
+ private float [] eyeView ;
50
+ private float [] eyePerspective ;
56
51
57
52
@ Override
58
53
protected PGL createPGL (PGraphicsOpenGL pg ) {
@@ -63,70 +58,147 @@ protected PGL createPGL(PGraphicsOpenGL pg) {
63
58
@ Override
64
59
public void beginDraw () {
65
60
super .beginDraw ();
66
- pgl .viewport (eyeViewPort .x , eyeViewPort .y , eyeViewPort .width , eyeViewPort .height );
67
- // The camera up direction is along -Y, because of the axis inversion
68
- // in Processing
69
- camera (0.0f , 0.0f , defCameraZ , 0.0f , 0.0f , 0.0f , 0.0f , 1.0f , 0.0f );
70
- setProjection (perspectiveMatrix );
71
- preApplyMatrix (viewMatrix );
61
+ updateView ();
62
+ }
63
+
64
+
65
+ @ Override
66
+ public void camera (float eyeX , float eyeY , float eyeZ ,
67
+ float centerX , float centerY , float centerZ ,
68
+ float upX , float upY , float upZ ) {
69
+ PGraphics .showWarning ("The camera cannnot be modified in VR mode" );
70
+ }
71
+
72
+
73
+ @ Override
74
+ public void perspective (float fov , float aspect , float zNear , float zFar ) {
75
+ PGraphics .showWarning ("Perspective cannnot be modified in VR mode" );
76
+ }
77
+
78
+
79
+ @ Override
80
+ protected void defaultCamera () {
81
+ // do nothing
82
+ }
83
+
84
+
85
+ @ Override
86
+ protected void defaultPerspective () {
87
+ // do nothing
88
+ }
89
+
90
+
91
+ protected void updateView () {
92
+ pgl .viewport (eyeViewport .x , eyeViewport .y , eyeViewport .width , eyeViewport .height );
93
+ setCameraVR (0.0f , 0.0f , defCameraZ , 0.0f , 0.0f , 0.0f , 0.0f , 1.0f , 0.0f );
94
+ setProjectionVR ();
72
95
}
73
96
74
97
75
- public void preApplyMatrix (PMatrix3D source ) {
76
- modelview .preApply (source );
98
+ protected void setCameraVR (float eyeX , float eyeY , float eyeZ ,
99
+ float centerX , float centerY , float centerZ ,
100
+ float upX , float upY , float upZ ) {
101
+ cameraX = eyeX ;
102
+ cameraY = eyeY ;
103
+ cameraZ = eyeZ ;
104
+
105
+ // Calculating Z vector
106
+ float z0 = eyeX - centerX ;
107
+ float z1 = eyeY - centerY ;
108
+ float z2 = eyeZ - centerZ ;
109
+ eyeDist = PApplet .sqrt (z0 * z0 + z1 * z1 + z2 * z2 );
110
+ if (nonZero (eyeDist )) {
111
+ z0 /= eyeDist ;
112
+ z1 /= eyeDist ;
113
+ z2 /= eyeDist ;
114
+ }
115
+
116
+ // Calculating Y vector
117
+ float y0 = upX ;
118
+ float y1 = upY ;
119
+ float y2 = upZ ;
120
+
121
+ // Computing X vector as Y cross Z
122
+ float x0 = y1 * z2 - y2 * z1 ;
123
+ float x1 = -y0 * z2 + y2 * z0 ;
124
+ float x2 = y0 * z1 - y1 * z0 ;
125
+
126
+ // Recompute Y = Z cross X
127
+ y0 = z1 * x2 - z2 * x1 ;
128
+ y1 = -z0 * x2 + z2 * x0 ;
129
+ y2 = z0 * x1 - z1 * x0 ;
130
+
131
+ // Cross product gives area of parallelogram, which is < 1.0 for
132
+ // non-perpendicular unit-length vectors; so normalize x, y here:
133
+ float xmag = PApplet .sqrt (x0 * x0 + x1 * x1 + x2 * x2 );
134
+ if (nonZero (xmag )) {
135
+ x0 /= xmag ;
136
+ x1 /= xmag ;
137
+ x2 /= xmag ;
138
+ }
139
+
140
+ float ymag = PApplet .sqrt (y0 * y0 + y1 * y1 + y2 * y2 );
141
+ if (nonZero (ymag )) {
142
+ y0 /= ymag ;
143
+ y1 /= ymag ;
144
+ y2 /= ymag ;
145
+ }
146
+
147
+ // Pre-apply the eye view matrix:
148
+ // https://developers.google.com/vr/android/reference/com/google/vr/sdk/base/Eye.html#getEyeView()
149
+ modelview .set (eyeView [0 ], eyeView [4 ], eyeView [8 ], eyeView [12 ],
150
+ eyeView [1 ], eyeView [5 ], eyeView [9 ], eyeView [13 ],
151
+ eyeView [2 ], eyeView [6 ], eyeView [10 ], eyeView [14 ],
152
+ eyeView [3 ], eyeView [7 ], eyeView [11 ], eyeView [15 ]);
153
+ modelview .apply (x0 , x1 , x2 , 0 ,
154
+ y0 , y1 , y2 , 0 ,
155
+ z0 , z1 , z2 , 0 ,
156
+ 0 , 0 , 0 , 1 );
157
+ float tx = -eyeX ;
158
+ float ty = -eyeY ;
159
+ float tz = -eyeZ ;
160
+ modelview .translate (tx , ty , tz );
161
+
77
162
modelviewInv .set (modelview );
78
163
modelviewInv .invert ();
164
+
165
+ camera .set (modelview );
166
+ cameraInv .set (modelviewInv );
167
+ }
168
+
169
+
170
+ protected void setProjectionVR () {
171
+ // Matrices in Processing are row-major, and GVR API is column-major
172
+ projection .set (eyePerspective [0 ], eyePerspective [4 ], eyePerspective [8 ], eyePerspective [12 ],
173
+ eyePerspective [1 ], eyePerspective [5 ], eyePerspective [9 ], eyePerspective [13 ],
174
+ eyePerspective [2 ], eyePerspective [6 ], eyePerspective [10 ], eyePerspective [14 ],
175
+ eyePerspective [3 ], eyePerspective [7 ], eyePerspective [11 ], eyePerspective [15 ]);
79
176
updateProjmodelview ();
80
177
}
81
178
82
179
83
- protected void headTransform (HeadTransform headTransform ) {
180
+ protected void headTransform (HeadTransform ht ) {
84
181
initVR ();
85
-
86
- // Get the head view and rotation so the user can use them for object selection and
87
- // other operations.
88
- headTransform .getHeadView (headView , 0 );
89
- headTransform .getQuaternion (headRotation , 0 );
90
- headTransform .getTranslation (translationVector , 0 );
182
+ headTransform = ht ;
91
183
headTransform .getForwardVector (forwardVector , 0 );
92
- headTransform .getRightVector (rightVector , 0 );
93
- headTransform .getUpVector (upVector , 0 );
184
+ forwardX = forwardVector [0 ];
185
+ forwardY = forwardVector [1 ];
186
+ forwardZ = forwardVector [2 ];
94
187
}
95
188
96
189
97
- protected void eyeTransform (Eye eye ) {
190
+ protected void eyeTransform (Eye e ) {
191
+ eye = e ;
98
192
eyeType = eye .getType ();
99
- eyeViewPort = eye .getViewport ();
100
- eyeFov = eye .getFov ();
101
-
102
- // Matrices in Processing are row-major, and GVR API is column-major
103
- // Also, need to invert Y coordinate, that's why the minus in front of p[5]
193
+ eyeViewport = eye .getViewport ();
104
194
eyePerspective = eye .getPerspective (cameraNear , cameraFar );
105
- perspectiveMatrix .set (eyePerspective [0 ], eyePerspective [4 ], eyePerspective [8 ], eyePerspective [12 ],
106
- eyePerspective [1 ], eyePerspective [5 ], eyePerspective [9 ], eyePerspective [13 ],
107
- eyePerspective [2 ], eyePerspective [6 ], eyePerspective [10 ], eyePerspective [14 ],
108
- eyePerspective [3 ], eyePerspective [7 ], eyePerspective [11 ], eyePerspective [15 ]);
109
-
110
195
eyeView = eye .getEyeView ();
111
- viewMatrix .set (eyeView [0 ], eyeView [4 ], eyeView [8 ], eyeView [12 ],
112
- eyeView [1 ], eyeView [5 ], eyeView [9 ], eyeView [13 ],
113
- eyeView [2 ], eyeView [6 ], eyeView [10 ], eyeView [14 ],
114
- eyeView [3 ], eyeView [7 ], eyeView [11 ], eyeView [15 ]);
115
196
}
116
197
117
198
118
199
private void initVR () {
119
200
if (!initialized ) {
120
- headRotation = new float [4 ];
121
- headView = new float [16 ];
122
- translationVector = new float [3 ];
123
201
forwardVector = new float [3 ];
124
- rightVector = new float [3 ];
125
- upVector = new float [3 ];
126
-
127
- perspectiveMatrix = new PMatrix3D ();
128
- viewMatrix = new PMatrix3D ();
129
-
130
202
initialized = true ;
131
203
}
132
204
}
0 commit comments