23
23
package processing .vr ;
24
24
25
25
import com .google .vr .sdk .base .Eye ;
26
+ import com .google .vr .sdk .base .FieldOfView ;
26
27
import com .google .vr .sdk .base .HeadTransform ;
27
28
import com .google .vr .sdk .base .Viewport ;
28
29
35
36
public class PGraphicsVR extends PGraphics3D {
36
37
private boolean initialized = false ;
37
38
39
+ // Head properties, independent of eye view
38
40
public int eyeType ;
39
41
public float [] headView ;
40
42
public float [] headRotation ;
43
+ public float [] translationVector ;
44
+ public float [] forwardVector ;
45
+ public float [] rightVector ;
46
+ public float [] upVector ;
47
+
48
+ // Eye properties
49
+ public FieldOfView eyeFov ;
50
+ public Viewport eyeViewPort ;
51
+ public float [] eyeView ;
52
+ public float [] eyePerspective ;
41
53
42
- private Viewport viewPort ;
43
54
private PMatrix3D viewMatrix ;
44
55
private PMatrix3D perspectiveMatrix ;
45
56
46
-
47
57
@ Override
48
58
protected PGL createPGL (PGraphicsOpenGL pg ) {
49
59
return new PGLES (pg );
@@ -53,10 +63,10 @@ protected PGL createPGL(PGraphicsOpenGL pg) {
53
63
@ Override
54
64
public void beginDraw () {
55
65
super .beginDraw ();
56
- pgl .viewport (viewPort .x , viewPort .y , viewPort .width , viewPort .height );
66
+ pgl .viewport (eyeViewPort .x , eyeViewPort .y , eyeViewPort .width , eyeViewPort .height );
57
67
// The camera up direction is along -Y, because of the axis inversion
58
68
// in Processing
59
- camera (0.0f , 0.0f , defCameraZ , 0.0f , 0.0f , 0.0f , 0.0f , - 1.0f , 0.0f );
69
+ camera (0.0f , 0.0f , defCameraZ , 0.0f , 0.0f , 0.0f , 0.0f , 1.0f , 0.0f );
60
70
setProjection (perspectiveMatrix );
61
71
preApplyMatrix (viewMatrix );
62
72
}
@@ -77,38 +87,69 @@ protected void headTransform(HeadTransform headTransform) {
77
87
// other operations.
78
88
headTransform .getHeadView (headView , 0 );
79
89
headTransform .getQuaternion (headRotation , 0 );
90
+ headTransform .getTranslation (translationVector , 0 );
91
+ headTransform .getForwardVector (forwardVector , 0 );
92
+ headTransform .getRightVector (rightVector , 0 );
93
+ headTransform .getUpVector (upVector , 0 );
80
94
}
81
95
82
96
83
97
protected void eyeTransform (Eye eye ) {
84
98
eyeType = eye .getType ();
85
- viewPort = eye .getViewport ();
99
+ eyeViewPort = eye .getViewport ();
100
+ eyeFov = eye .getFov ();
86
101
87
102
// Matrices in Processing are row-major, and GVR API is column-major
88
103
// Also, need to invert Y coordinate, that's why the minus in front of p[5]
89
- float [] p = eye .getPerspective (cameraNear , cameraFar );
90
- perspectiveMatrix .set (p [0 ], p [4 ], p [8 ], p [12 ],
91
- p [1 ], - p [5 ], p [9 ], p [13 ],
92
- p [2 ], p [6 ], p [10 ], p [14 ],
93
- p [3 ], p [7 ], p [11 ], p [15 ]);
94
-
95
- float [] v = eye .getEyeView ();
96
- viewMatrix .set (v [0 ], v [4 ], v [8 ], v [12 ],
97
- v [1 ], v [5 ], v [9 ], v [13 ],
98
- v [2 ], v [6 ], v [10 ], v [14 ],
99
- v [3 ], v [7 ], v [11 ], v [15 ]);
104
+ 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
+ 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 ]);
100
115
}
101
116
102
117
103
118
private void initVR () {
104
119
if (!initialized ) {
105
120
headRotation = new float [4 ];
106
121
headView = new float [16 ];
122
+ translationVector = new float [3 ];
123
+ forwardVector = new float [3 ];
124
+ rightVector = new float [3 ];
125
+ upVector = new float [3 ];
107
126
108
127
perspectiveMatrix = new PMatrix3D ();
109
128
viewMatrix = new PMatrix3D ();
110
129
111
130
initialized = true ;
112
131
}
113
132
}
133
+
134
+ @ Override
135
+ protected void updateGLNormal () {
136
+ if (glNormal == null ) {
137
+ glNormal = new float [9 ];
138
+ }
139
+
140
+ // Since Y is inverted in VR, we need to invert the normal calculation so
141
+ // lighting works as it should, even if we provide the normals as before.
142
+ glNormal [0 ] = -modelviewInv .m00 ;
143
+ glNormal [1 ] = -modelviewInv .m01 ;
144
+ glNormal [2 ] = -modelviewInv .m02 ;
145
+
146
+ glNormal [3 ] = -modelviewInv .m10 ;
147
+ glNormal [4 ] = -modelviewInv .m11 ;
148
+ glNormal [5 ] = -modelviewInv .m12 ;
149
+
150
+ glNormal [6 ] = -modelviewInv .m20 ;
151
+ glNormal [7 ] = -modelviewInv .m21 ;
152
+ glNormal [8 ] = -modelviewInv .m22 ;
153
+ }
154
+
114
155
}
0 commit comments