2323package processing .vr ;
2424
2525import com .google .vr .sdk .base .Eye ;
26+ import com .google .vr .sdk .base .FieldOfView ;
2627import com .google .vr .sdk .base .HeadTransform ;
2728import com .google .vr .sdk .base .Viewport ;
2829
3536public class PGraphicsVR extends PGraphics3D {
3637 private boolean initialized = false ;
3738
39+ // Head properties, independent of eye view
3840 public int eyeType ;
3941 public float [] headView ;
4042 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 ;
4153
42- private Viewport viewPort ;
4354 private PMatrix3D viewMatrix ;
4455 private PMatrix3D perspectiveMatrix ;
4556
46-
4757 @ Override
4858 protected PGL createPGL (PGraphicsOpenGL pg ) {
4959 return new PGLES (pg );
@@ -53,10 +63,10 @@ protected PGL createPGL(PGraphicsOpenGL pg) {
5363 @ Override
5464 public void beginDraw () {
5565 super .beginDraw ();
56- pgl .viewport (viewPort .x , viewPort .y , viewPort .width , viewPort .height );
66+ pgl .viewport (eyeViewPort .x , eyeViewPort .y , eyeViewPort .width , eyeViewPort .height );
5767 // The camera up direction is along -Y, because of the axis inversion
5868 // 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 );
6070 setProjection (perspectiveMatrix );
6171 preApplyMatrix (viewMatrix );
6272 }
@@ -77,38 +87,69 @@ protected void headTransform(HeadTransform headTransform) {
7787 // other operations.
7888 headTransform .getHeadView (headView , 0 );
7989 headTransform .getQuaternion (headRotation , 0 );
90+ headTransform .getTranslation (translationVector , 0 );
91+ headTransform .getForwardVector (forwardVector , 0 );
92+ headTransform .getRightVector (rightVector , 0 );
93+ headTransform .getUpVector (upVector , 0 );
8094 }
8195
8296
8397 protected void eyeTransform (Eye eye ) {
8498 eyeType = eye .getType ();
85- viewPort = eye .getViewport ();
99+ eyeViewPort = eye .getViewport ();
100+ eyeFov = eye .getFov ();
86101
87102 // Matrices in Processing are row-major, and GVR API is column-major
88103 // 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 ]);
100115 }
101116
102117
103118 private void initVR () {
104119 if (!initialized ) {
105120 headRotation = new float [4 ];
106121 headView = new float [16 ];
122+ translationVector = new float [3 ];
123+ forwardVector = new float [3 ];
124+ rightVector = new float [3 ];
125+ upVector = new float [3 ];
107126
108127 perspectiveMatrix = new PMatrix3D ();
109128 viewMatrix = new PMatrix3D ();
110129
111130 initialized = true ;
112131 }
113132 }
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+
114155}
0 commit comments