virtualx-engine/scene/3d/quad.cpp

227 lines
6.2 KiB
C++
Raw Normal View History

2014-02-10 02:10:30 +01:00
/*************************************************************************/
/* quad.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2014-02-10 02:10:30 +01:00
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "quad.h"
#include "servers/visual_server.h"
void Quad::_update() {
if (!is_inside_tree())
2014-02-10 02:10:30 +01:00
return;
Vector3 normal;
normal[axis] = 1.0;
2014-02-10 02:10:30 +01:00
const int axis_order_1[3] = { 1, 2, 0 };
const int axis_order_2[3] = { 2, 0, 1 };
const int a1 = axis_order_1[axis];
const int a2 = axis_order_2[axis];
2014-02-10 02:10:30 +01:00
PoolVector<Vector3> points;
2014-02-10 02:10:30 +01:00
points.resize(4);
PoolVector<Vector3>::Write pointsw = points.write();
2014-02-10 02:10:30 +01:00
Vector2 s2 = size * 0.5;
2014-02-10 02:10:30 +01:00
Vector2 o = offset;
if (!centered)
o += s2;
2014-02-10 02:10:30 +01:00
pointsw[0][a1] = -s2.x + offset.x;
pointsw[0][a2] = s2.y + offset.y;
2014-02-10 02:10:30 +01:00
pointsw[1][a1] = s2.x + offset.x;
pointsw[1][a2] = s2.y + offset.y;
2014-02-10 02:10:30 +01:00
pointsw[2][a1] = s2.x + offset.x;
pointsw[2][a2] = -s2.y + offset.y;
2014-02-10 02:10:30 +01:00
pointsw[3][a1] = -s2.x + offset.x;
pointsw[3][a2] = -s2.y + offset.y;
2014-02-10 02:10:30 +01:00
aabb = Rect3(pointsw[0], Vector3());
for (int i = 1; i < 4; i++)
2014-02-10 02:10:30 +01:00
aabb.expand_to(pointsw[i]);
pointsw = PoolVector<Vector3>::Write();
2014-02-10 02:10:30 +01:00
PoolVector<Vector3> normals;
2014-02-10 02:10:30 +01:00
normals.resize(4);
PoolVector<Vector3>::Write normalsw = normals.write();
2014-02-10 02:10:30 +01:00
for (int i = 0; i < 4; i++)
normalsw[i] = normal;
2014-02-10 02:10:30 +01:00
normalsw = PoolVector<Vector3>::Write();
2014-02-10 02:10:30 +01:00
PoolVector<Vector2> uvs;
2014-02-10 02:10:30 +01:00
uvs.resize(4);
PoolVector<Vector2>::Write uvsw = uvs.write();
2014-02-10 02:10:30 +01:00
uvsw[0] = Vector2(0, 0);
uvsw[1] = Vector2(1, 0);
uvsw[2] = Vector2(1, 1);
uvsw[3] = Vector2(0, 1);
2014-02-10 02:10:30 +01:00
uvsw = PoolVector<Vector2>::Write();
2014-02-10 02:10:30 +01:00
PoolVector<int> indices;
2014-02-10 02:10:30 +01:00
indices.resize(6);
PoolVector<int>::Write indicesw = indices.write();
indicesw[0] = 0;
indicesw[1] = 1;
indicesw[2] = 2;
indicesw[3] = 2;
indicesw[4] = 3;
indicesw[5] = 0;
2014-02-10 02:10:30 +01:00
indicesw = PoolVector<int>::Write();
2014-02-10 02:10:30 +01:00
Array arr;
arr.resize(VS::ARRAY_MAX);
arr[VS::ARRAY_VERTEX] = points;
arr[VS::ARRAY_NORMAL] = normals;
arr[VS::ARRAY_TEX_UV] = uvs;
arr[VS::ARRAY_INDEX] = indices;
2014-02-10 02:10:30 +01:00
if (configured) {
VS::get_singleton()->mesh_remove_surface(mesh, 0);
2014-02-10 02:10:30 +01:00
} else {
configured = true;
2014-02-10 02:10:30 +01:00
}
VS::get_singleton()->mesh_add_surface_from_arrays(mesh, VS::PRIMITIVE_TRIANGLES, arr);
2014-02-10 02:10:30 +01:00
pending_update = false;
2014-02-10 02:10:30 +01:00
}
void Quad::set_axis(Vector3::Axis p_axis) {
axis = p_axis;
2014-02-10 02:10:30 +01:00
_update();
}
Vector3::Axis Quad::get_axis() const {
2014-02-10 02:10:30 +01:00
return axis;
}
void Quad::set_size(const Vector2 &p_size) {
2014-02-10 02:10:30 +01:00
size = p_size;
2014-02-10 02:10:30 +01:00
_update();
}
Vector2 Quad::get_size() const {
2014-02-10 02:10:30 +01:00
return size;
}
void Quad::set_offset(const Vector2 &p_offset) {
2014-02-10 02:10:30 +01:00
offset = p_offset;
2014-02-10 02:10:30 +01:00
_update();
}
Vector2 Quad::get_offset() const {
2014-02-10 02:10:30 +01:00
return offset;
}
void Quad::set_centered(bool p_enabled) {
2014-02-10 02:10:30 +01:00
centered = p_enabled;
2014-02-10 02:10:30 +01:00
_update();
}
bool Quad::is_centered() const {
2014-02-10 02:10:30 +01:00
return centered;
}
void Quad::_notification(int p_what) {
switch (p_what) {
2014-02-10 02:10:30 +01:00
case NOTIFICATION_ENTER_TREE: {
2014-02-10 02:10:30 +01:00
if (pending_update)
_update();
} break;
case NOTIFICATION_EXIT_TREE: {
2014-02-10 02:10:30 +01:00
pending_update = true;
2014-02-10 02:10:30 +01:00
} break;
}
}
PoolVector<Face3> Quad::get_faces(uint32_t p_usage_flags) const {
2014-02-10 02:10:30 +01:00
return PoolVector<Face3>();
2014-02-10 02:10:30 +01:00
}
Rect3 Quad::get_aabb() const {
2014-02-10 02:10:30 +01:00
return aabb;
}
void Quad::_bind_methods() {
2014-02-10 02:10:30 +01:00
ClassDB::bind_method(D_METHOD("set_axis", "axis"), &Quad::set_axis);
ClassDB::bind_method(D_METHOD("get_axis"), &Quad::get_axis);
2014-02-10 02:10:30 +01:00
ClassDB::bind_method(D_METHOD("set_size", "size"), &Quad::set_size);
ClassDB::bind_method(D_METHOD("get_size"), &Quad::get_size);
2014-02-10 02:10:30 +01:00
ClassDB::bind_method(D_METHOD("set_centered", "centered"), &Quad::set_centered);
ClassDB::bind_method(D_METHOD("is_centered"), &Quad::is_centered);
2014-02-10 02:10:30 +01:00
ClassDB::bind_method(D_METHOD("set_offset", "offset"), &Quad::set_offset);
ClassDB::bind_method(D_METHOD("get_offset"), &Quad::get_offset);
2014-02-10 02:10:30 +01:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "axis", PROPERTY_HINT_ENUM, "X,Y,Z"), "set_axis", "get_axis");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered");
2014-02-10 02:10:30 +01:00
}
Quad::Quad() {
pending_update = true;
centered = true;
2014-02-10 02:10:30 +01:00
//offset=0;
size = Vector2(1, 1);
axis = Vector3::AXIS_Z;
mesh = VisualServer::get_singleton()->mesh_create();
2014-02-10 02:10:30 +01:00
set_base(mesh);
configured = false;
2014-02-10 02:10:30 +01:00
}
2016-12-16 13:50:46 +01:00
Quad::~Quad() {
VisualServer::get_singleton()->free(mesh);
}