| 1 | /**************************************************************************/ | 
|---|
| 2 | /*  static_raycaster_embree.cpp                                           */ | 
|---|
| 3 | /**************************************************************************/ | 
|---|
| 4 | /*                         This file is part of:                          */ | 
|---|
| 5 | /*                             GODOT ENGINE                               */ | 
|---|
| 6 | /*                        https://godotengine.org                         */ | 
|---|
| 7 | /**************************************************************************/ | 
|---|
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
|---|
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
|---|
| 10 | /*                                                                        */ | 
|---|
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
|---|
| 12 | /* a copy of this software and associated documentation files (the        */ | 
|---|
| 13 | /* "Software"), to deal in the Software without restriction, including    */ | 
|---|
| 14 | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
|---|
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
|---|
| 16 | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
|---|
| 17 | /* the following conditions:                                              */ | 
|---|
| 18 | /*                                                                        */ | 
|---|
| 19 | /* The above copyright notice and this permission notice shall be         */ | 
|---|
| 20 | /* included in all copies or substantial portions of the Software.        */ | 
|---|
| 21 | /*                                                                        */ | 
|---|
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
|---|
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
|---|
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
|---|
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
|---|
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
|---|
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
|---|
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
|---|
| 29 | /**************************************************************************/ | 
|---|
| 30 |  | 
|---|
| 31 | #include "static_raycaster_embree.h" | 
|---|
| 32 |  | 
|---|
| 33 | #ifdef TOOLS_ENABLED | 
|---|
| 34 |  | 
|---|
| 35 | #ifdef __SSE2__ | 
|---|
| 36 | #include <pmmintrin.h> | 
|---|
| 37 | #endif | 
|---|
| 38 |  | 
|---|
| 39 | RTCDevice StaticRaycasterEmbree::embree_device; | 
|---|
| 40 |  | 
|---|
| 41 | StaticRaycaster *StaticRaycasterEmbree::create_embree_raycaster() { | 
|---|
| 42 | return memnew(StaticRaycasterEmbree); | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 | void StaticRaycasterEmbree::make_default_raycaster() { | 
|---|
| 46 | create_function = create_embree_raycaster; | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | void StaticRaycasterEmbree::free() { | 
|---|
| 50 | if (embree_device) { | 
|---|
| 51 | rtcReleaseDevice(embree_device); | 
|---|
| 52 | } | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | bool StaticRaycasterEmbree::intersect(Ray &r_ray) { | 
|---|
| 56 | RTCIntersectContext context; | 
|---|
| 57 | rtcInitIntersectContext(&context); | 
|---|
| 58 | rtcIntersect1(embree_scene, &context, (RTCRayHit *)&r_ray); | 
|---|
| 59 | return r_ray.geomID != RTC_INVALID_GEOMETRY_ID; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | void StaticRaycasterEmbree::intersect(Vector<Ray> &r_rays) { | 
|---|
| 63 | Ray *rays = r_rays.ptrw(); | 
|---|
| 64 | for (int i = 0; i < r_rays.size(); ++i) { | 
|---|
| 65 | intersect(rays[i]); | 
|---|
| 66 | } | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | void StaticRaycasterEmbree::add_mesh(const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices, unsigned int p_id) { | 
|---|
| 70 | RTCGeometry embree_mesh = rtcNewGeometry(embree_device, RTC_GEOMETRY_TYPE_TRIANGLE); | 
|---|
| 71 |  | 
|---|
| 72 | int vertex_count = p_vertices.size(); | 
|---|
| 73 |  | 
|---|
| 74 | Vector3 *embree_vertices = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count); | 
|---|
| 75 | memcpy(embree_vertices, p_vertices.ptr(), sizeof(Vector3) * vertex_count); | 
|---|
| 76 |  | 
|---|
| 77 | if (p_indices.is_empty()) { | 
|---|
| 78 | ERR_FAIL_COND(vertex_count % 3 != 0); | 
|---|
| 79 | uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, vertex_count / 3); | 
|---|
| 80 | for (int i = 0; i < vertex_count; i++) { | 
|---|
| 81 | embree_triangles[i] = i; | 
|---|
| 82 | } | 
|---|
| 83 | } else { | 
|---|
| 84 | uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, p_indices.size() / 3); | 
|---|
| 85 | memcpy(embree_triangles, p_indices.ptr(), sizeof(uint32_t) * p_indices.size()); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | rtcCommitGeometry(embree_mesh); | 
|---|
| 89 | rtcAttachGeometryByID(embree_scene, embree_mesh, p_id); | 
|---|
| 90 | rtcReleaseGeometry(embree_mesh); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | void StaticRaycasterEmbree::commit() { | 
|---|
| 94 | rtcCommitScene(embree_scene); | 
|---|
| 95 | } | 
|---|
| 96 |  | 
|---|
| 97 | void StaticRaycasterEmbree::set_mesh_filter(const HashSet<int> &p_mesh_ids) { | 
|---|
| 98 | for (const int &E : p_mesh_ids) { | 
|---|
| 99 | rtcDisableGeometry(rtcGetGeometry(embree_scene, E)); | 
|---|
| 100 | } | 
|---|
| 101 | rtcCommitScene(embree_scene); | 
|---|
| 102 | filter_meshes = p_mesh_ids; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | void StaticRaycasterEmbree::clear_mesh_filter() { | 
|---|
| 106 | for (const int &E : filter_meshes) { | 
|---|
| 107 | rtcEnableGeometry(rtcGetGeometry(embree_scene, E)); | 
|---|
| 108 | } | 
|---|
| 109 | rtcCommitScene(embree_scene); | 
|---|
| 110 | filter_meshes.clear(); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | void embree_error_handler(void *p_user_data, RTCError p_code, const char *p_str) { | 
|---|
| 114 | print_error( "Embree error: "+ String(p_str)); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | StaticRaycasterEmbree::StaticRaycasterEmbree() { | 
|---|
| 118 | #ifdef __SSE2__ | 
|---|
| 119 | _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); | 
|---|
| 120 | _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); | 
|---|
| 121 | #endif | 
|---|
| 122 |  | 
|---|
| 123 | if (!embree_device) { | 
|---|
| 124 | embree_device = rtcNewDevice(nullptr); | 
|---|
| 125 | rtcSetDeviceErrorFunction(embree_device, &embree_error_handler, nullptr); | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | embree_scene = rtcNewScene(embree_device); | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | StaticRaycasterEmbree::~StaticRaycasterEmbree() { | 
|---|
| 132 | if (embree_scene != nullptr) { | 
|---|
| 133 | rtcReleaseScene(embree_scene); | 
|---|
| 134 | } | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | #endif // TOOLS_ENABLED | 
|---|
| 138 |  | 
|---|