1 /*************************************************************************/
2 /*  shape.cpp                                                            */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
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 #include "shape.h"
31 
32 #include "os/os.h"
33 #include "scene/main/scene_main_loop.h"
34 #include "scene/resources/mesh.h"
35 #include "servers/physics_server.h"
36 
add_vertices_to_array(DVector<Vector3> & array,const Transform & p_xform)37 void Shape::add_vertices_to_array(DVector<Vector3> &array, const Transform &p_xform) {
38 
39 	Vector<Vector3> toadd = _gen_debug_mesh_lines();
40 
41 	if (toadd.size()) {
42 
43 		int base = array.size();
44 		array.resize(base + toadd.size());
45 		DVector<Vector3>::Write w = array.write();
46 		for (int i = 0; i < toadd.size(); i++) {
47 			w[i + base] = p_xform.xform(toadd[i]);
48 		}
49 	}
50 }
51 
get_debug_mesh()52 Ref<Mesh> Shape::get_debug_mesh() {
53 
54 	if (debug_mesh_cache.is_valid())
55 		return debug_mesh_cache;
56 
57 	Vector<Vector3> lines = _gen_debug_mesh_lines();
58 
59 	debug_mesh_cache = Ref<Mesh>(memnew(Mesh));
60 
61 	if (!lines.empty()) {
62 		//make mesh
63 		DVector<Vector3> array;
64 		array.resize(lines.size());
65 		{
66 
67 			DVector<Vector3>::Write w = array.write();
68 			for (int i = 0; i < lines.size(); i++) {
69 				w[i] = lines[i];
70 			}
71 		}
72 
73 		Array arr;
74 		arr.resize(Mesh::ARRAY_MAX);
75 		arr[Mesh::ARRAY_VERTEX] = array;
76 
77 		SceneTree *st = OS::get_singleton()->get_main_loop()->cast_to<SceneTree>();
78 
79 		debug_mesh_cache->add_surface(Mesh::PRIMITIVE_LINES, arr);
80 
81 		if (st) {
82 			debug_mesh_cache->surface_set_material(0, st->get_debug_collision_material());
83 		}
84 	}
85 
86 	return debug_mesh_cache;
87 }
88 
Shape()89 Shape::Shape() {
90 
91 	ERR_PRINT("Constructor must not be called!");
92 }
93 
Shape(RID p_shape)94 Shape::Shape(RID p_shape) {
95 
96 	shape = p_shape;
97 }
98 
~Shape()99 Shape::~Shape() {
100 
101 	PhysicsServer::get_singleton()->free(shape);
102 }
103