OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "modules/vr/VRFrameData.h" |
| 6 |
| 7 #include "modules/vr/VREyeParameters.h" |
| 8 #include "modules/vr/VRPose.h" |
| 9 |
| 10 #include <cmath> |
| 11 |
| 12 namespace blink { |
| 13 |
| 14 // TODO(bajones): All of the matrix math here is temporary. It will be removed |
| 15 // once the VRService has been updated to allow the underlying VR APIs to |
| 16 // provide the projection and view matrices directly. |
| 17 |
| 18 // Build a projection matrix from a field of view and near/far planes. |
| 19 void projectionFromFieldOfView(DOMFloat32Array* outArray, VRFieldOfView* fov, fl
oat depthNear, float depthFar) |
| 20 { |
| 21 float upTan = tanf(fov->upDegrees() * M_PI / 180.0); |
| 22 float downTan = tanf(fov->downDegrees() * M_PI / 180.0); |
| 23 float leftTan = tanf(fov->leftDegrees() * M_PI / 180.0); |
| 24 float rightTan = tanf(fov->rightDegrees() * M_PI / 180.0); |
| 25 float xScale = 2.0f / (leftTan + rightTan); |
| 26 float yScale = 2.0f / (upTan + downTan); |
| 27 |
| 28 float* out = outArray->data(); |
| 29 out[0] = xScale; |
| 30 out[1] = 0.0f; |
| 31 out[2] = 0.0f; |
| 32 out[3] = 0.0f; |
| 33 out[4] = 0.0f; |
| 34 out[5] = yScale; |
| 35 out[6] = 0.0f; |
| 36 out[7] = 0.0f; |
| 37 out[8] = -((leftTan - rightTan) * xScale * 0.5); |
| 38 out[9] = ((upTan - downTan) * yScale * 0.5); |
| 39 out[10] = depthFar / (depthNear - depthFar); |
| 40 out[11] = -1.0f; |
| 41 out[12] = 0.0f; |
| 42 out[13] = 0.0f; |
| 43 out[14] = (depthFar * depthNear) / (depthNear - depthFar); |
| 44 out[15] = 0.0f; |
| 45 } |
| 46 |
| 47 // Create a matrix from a rotation and translation. |
| 48 void matrixfromRotationTranslation(DOMFloat32Array* outArray, const mojo::WTFArr
ay<float>& rotation, const mojo::WTFArray<float>& translation) |
| 49 { |
| 50 // Quaternion math |
| 51 float x = rotation.is_null() ? 0.0f : rotation[0]; |
| 52 float y = rotation.is_null() ? 0.0f : rotation[1]; |
| 53 float z = rotation.is_null() ? 0.0f : rotation[2]; |
| 54 float w = rotation.is_null() ? 1.0f : rotation[3]; |
| 55 float x2 = x + x; |
| 56 float y2 = y + y; |
| 57 float z2 = z + z; |
| 58 |
| 59 float xx = x * x2; |
| 60 float xy = x * y2; |
| 61 float xz = x * z2; |
| 62 float yy = y * y2; |
| 63 float yz = y * z2; |
| 64 float zz = z * z2; |
| 65 float wx = w * x2; |
| 66 float wy = w * y2; |
| 67 float wz = w * z2; |
| 68 |
| 69 float* out = outArray->data(); |
| 70 out[0] = 1 - (yy + zz); |
| 71 out[1] = xy + wz; |
| 72 out[2] = xz - wy; |
| 73 out[3] = 0; |
| 74 out[4] = xy - wz; |
| 75 out[5] = 1 - (xx + zz); |
| 76 out[6] = yz + wx; |
| 77 out[7] = 0; |
| 78 out[8] = xz + wy; |
| 79 out[9] = yz - wx; |
| 80 out[10] = 1 - (xx + yy); |
| 81 out[11] = 0; |
| 82 out[12] = translation.is_null() ? 0.0f : translation[0]; |
| 83 out[13] = translation.is_null() ? 0.0f : translation[1]; |
| 84 out[14] = translation.is_null() ? 0.0f : translation[2]; |
| 85 out[15] = 1; |
| 86 } |
| 87 |
| 88 // Translate a matrix |
| 89 void matrixTranslate(DOMFloat32Array* outArray, const DOMFloat32Array* translati
on) |
| 90 { |
| 91 if (!translation) |
| 92 return; |
| 93 |
| 94 float x = translation->data()[0]; |
| 95 float y = translation->data()[1]; |
| 96 float z = translation->data()[2]; |
| 97 |
| 98 float* out = outArray->data(); |
| 99 out[12] = out[0] * x + out[4] * y + out[8] * z + out[12]; |
| 100 out[13] = out[1] * x + out[5] * y + out[9] * z + out[13]; |
| 101 out[14] = out[2] * x + out[6] * y + out[10] * z + out[14]; |
| 102 out[15] = out[3] * x + out[7] * y + out[11] * z + out[15]; |
| 103 } |
| 104 |
| 105 bool matrixInvert(DOMFloat32Array* outArray) |
| 106 { |
| 107 float* out = outArray->data(); |
| 108 float a00 = out[0]; |
| 109 float a01 = out[1]; |
| 110 float a02 = out[2]; |
| 111 float a03 = out[3]; |
| 112 float a10 = out[4]; |
| 113 float a11 = out[5]; |
| 114 float a12 = out[6]; |
| 115 float a13 = out[7]; |
| 116 float a20 = out[8]; |
| 117 float a21 = out[9]; |
| 118 float a22 = out[10]; |
| 119 float a23 = out[11]; |
| 120 float a30 = out[12]; |
| 121 float a31 = out[13]; |
| 122 float a32 = out[14]; |
| 123 float a33 = out[15]; |
| 124 |
| 125 float b00 = a00 * a11 - a01 * a10; |
| 126 float b01 = a00 * a12 - a02 * a10; |
| 127 float b02 = a00 * a13 - a03 * a10; |
| 128 float b03 = a01 * a12 - a02 * a11; |
| 129 float b04 = a01 * a13 - a03 * a11; |
| 130 float b05 = a02 * a13 - a03 * a12; |
| 131 float b06 = a20 * a31 - a21 * a30; |
| 132 float b07 = a20 * a32 - a22 * a30; |
| 133 float b08 = a20 * a33 - a23 * a30; |
| 134 float b09 = a21 * a32 - a22 * a31; |
| 135 float b10 = a21 * a33 - a23 * a31; |
| 136 float b11 = a22 * a33 - a23 * a32; |
| 137 |
| 138 // Calculate the determinant |
| 139 float det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05
* b06; |
| 140 |
| 141 if (!det) |
| 142 return false; |
| 143 |
| 144 det = 1.0 / det; |
| 145 |
| 146 out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; |
| 147 out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det; |
| 148 out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det; |
| 149 out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det; |
| 150 out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det; |
| 151 out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det; |
| 152 out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det; |
| 153 out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det; |
| 154 out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det; |
| 155 out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det; |
| 156 out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det; |
| 157 out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det; |
| 158 out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det; |
| 159 out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det; |
| 160 out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det; |
| 161 out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det; |
| 162 |
| 163 return true; |
| 164 }; |
| 165 |
| 166 VRFrameData::VRFrameData() |
| 167 : m_timestamp(0.0) |
| 168 { |
| 169 m_leftProjectionMatrix = DOMFloat32Array::create(16); |
| 170 m_leftViewMatrix = DOMFloat32Array::create(16); |
| 171 m_rightProjectionMatrix = DOMFloat32Array::create(16); |
| 172 m_rightViewMatrix = DOMFloat32Array::create(16); |
| 173 m_pose = VRPose::create(); |
| 174 } |
| 175 |
| 176 bool VRFrameData::update(const device::blink::VRPosePtr& pose, VREyeParameters*
leftEye, VREyeParameters* rightEye, float depthNear, float depthFar) |
| 177 { |
| 178 if (!pose) |
| 179 return false; |
| 180 |
| 181 m_timestamp = pose->timestamp; |
| 182 |
| 183 // Build the projection matrices |
| 184 projectionFromFieldOfView(m_leftProjectionMatrix, leftEye->fieldOfView(), de
pthNear, depthFar); |
| 185 projectionFromFieldOfView(m_rightProjectionMatrix, rightEye->fieldOfView(),
depthNear, depthFar); |
| 186 |
| 187 // Build the view matrices |
| 188 matrixfromRotationTranslation(m_leftViewMatrix, pose->orientation, pose->pos
ition); |
| 189 matrixTranslate(m_leftViewMatrix, leftEye->offset()); |
| 190 if (!matrixInvert(m_leftViewMatrix)) |
| 191 return false; |
| 192 |
| 193 matrixfromRotationTranslation(m_rightViewMatrix, pose->orientation, pose->po
sition); |
| 194 matrixTranslate(m_rightViewMatrix, rightEye->offset()); |
| 195 if (!matrixInvert(m_rightViewMatrix)) |
| 196 return false; |
| 197 |
| 198 // Set the pose |
| 199 m_pose->setPose(pose); |
| 200 |
| 201 return true; |
| 202 } |
| 203 |
| 204 DEFINE_TRACE(VRFrameData) |
| 205 { |
| 206 visitor->trace(m_leftProjectionMatrix); |
| 207 visitor->trace(m_leftViewMatrix); |
| 208 visitor->trace(m_rightProjectionMatrix); |
| 209 visitor->trace(m_rightViewMatrix); |
| 210 visitor->trace(m_pose); |
| 211 } |
| 212 |
| 213 } // namespace blink |
OLD | NEW |