184 ar_world_tracking_configuration_t worldTrackingConfiguration = ar_world_tracking_configuration_create();
185 m_worldTrackingProvider = ar_world_tracking_provider_create(worldTrackingConfiguration);
187 ar_data_providers_t dataProviders = ar_data_providers_create();
188 ar_data_providers_add_data_provider(dataProviders, m_worldTrackingProvider);
190 m_isHandTrackingSupported = ar_hand_tracking_provider_is_supported();
191 if (m_isHandTrackingSupported) {
192 ar_hand_tracking_configuration_t handTrackingConfiguration = ar_hand_tracking_configuration_create();
193 m_handTrackingProvider = ar_hand_tracking_provider_create(handTrackingConfiguration);
194 ar_data_providers_add_data_provider(dataProviders, m_handTrackingProvider);
196 qWarning(
"Hand tracking is not supported on this device.");
200 m_arSession = ar_session_create();
201 ar_session_run(m_arSession, dataProviders);
204 if (m_isHandTrackingSupported) {
205 m_leftHandAnchor = ar_hand_anchor_create();
206 m_rightHandAnchor = ar_hand_anchor_create();
214 if (
frame ==
nullptr) {
218 cp_frame_timing_t timing = cp_frame_predict_timing(
frame);
219 if (timing ==
nullptr)
222 cp_frame_start_update(
frame);
226 cp_frame_end_update(
frame);
228 cp_time_wait_until(cp_frame_timing_get_optimal_input_time(timing));
230 cp_frame_start_submission(
frame);
231 cp_drawable_t drawable = cp_frame_query_drawable(
frame);
232 if (drawable ==
nullptr)
235 cp_frame_timing_t actualTiming = cp_drawable_get_frame_timing(drawable);
237 cp_drawable_set_device_anchor(drawable, anchor);
240 simd_float4x4 headTransform = ar_anchor_get_origin_from_anchor_transform(anchor);
243 if (m_isHandTrackingSupported) {
244 ar_hand_tracking_provider_get_latest_anchors(m_handTrackingProvider, m_leftHandAnchor, m_rightHandAnchor);
246 if (ar_trackable_anchor_is_tracked(m_leftHandAnchor)) {
250 if (ar_trackable_anchor_is_tracked(m_rightHandAnchor)) {
255 QRhi *rhi = renderControl->
rhi();
257 for (
size_t i = 0,
end = cp_drawable_get_view_count(drawable);
i !=
end ; ++
i) {
259 id<MTLTexture> colorMetalTexture = cp_drawable_get_color_texture(drawable,
i);
260 auto textureSize =
QSize([colorMetalTexture
width], [colorMetalTexture
height]);
261 auto renderTarget = QQuickRenderTarget::fromMetalTexture(
static_cast<MTLTexture*
>(colorMetalTexture), [colorMetalTexture pixelFormat], textureSize);
263 auto depthMetalTexture = cp_drawable_get_depth_texture(drawable,
i);
264 auto depthTextureSize =
QSize([depthMetalTexture
width], [depthMetalTexture
height]);
265 MTLPixelFormat depthTextureFormat = [depthMetalTexture pixelFormat];
268 case MTLPixelFormatDepth16Unorm:
270 case MTLPixelFormatDepth32Float:
273 qWarning(
"Unsupported depth texture format");
277 auto depthFormat = convertFormat(depthTextureFormat);
279 if (m_rhiDepthTexture && (m_rhiDepthTexture->
format() != depthFormat || m_rhiDepthTexture->
pixelSize() != depthTextureSize)) {
280 delete m_rhiDepthTexture;
281 m_rhiDepthTexture =
nullptr;
284 if (!m_rhiDepthTexture)
288 m_rhiDepthTexture->
createFrom({
quint64(
static_cast<MTLTexture*
>(depthMetalTexture)), 0});
289 renderTarget.setDepthTexture(m_rhiDepthTexture);
292 quickWindow->setRenderTarget(renderTarget);
298 textureSize.height());
300 textureSize.height()));
304 cp_view_t
view = cp_drawable_get_view(drawable,
i);
305 simd_float4 tangents = cp_view_get_tangents(
view);
306 const float tangentLeft = tangents[0];
307 const float tangentRight = tangents[1];
308 const float tangentUp = tangents[2];
309 const float tangentDown = tangents[3];
311 simd_float2 depth_range = cp_drawable_get_depth_range(drawable);
312 const float clipNear = depth_range[1];
313 const float clipFar = depth_range[0];
322 simd_float4x4 localEyeTransform = cp_view_get_transform(
view);
323 simd_float4x4 eyeCameraTransform = simd_mul(headTransform, localEyeTransform);
325 QMatrix4x4 transform{eyeCameraTransform.columns[0].x, eyeCameraTransform.columns[1].x, eyeCameraTransform.columns[2].x, eyeCameraTransform.columns[3].x * 100,
326 eyeCameraTransform.columns[0].y, eyeCameraTransform.columns[1].y, eyeCameraTransform.columns[2].y, eyeCameraTransform.columns[3].y * 100,
327 eyeCameraTransform.columns[0].z, eyeCameraTransform.columns[1].z, eyeCameraTransform.columns[2].z, eyeCameraTransform.columns[3].z * 100,
328 0.0f, 0.0f, 0.0f, 1.0f};
330 xrViewport->setCamera(xrOrigin->eyeCamera(
i));
335 renderControl->
sync();
342 cp_drawable_encode_present(drawable, commandBuffer);
345 cp_frame_end_submission(
frame);
Format
Specifies the texture format.
virtual bool createFrom(NativeTexture src)
Similar to create(), except that no new native textures are created.