2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2020-03-26 21:49:16 +00:00
|
|
|
/* immediate_geometry_3d.cpp */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 12:46:12 +00: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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
#include "immediate_geometry_3d.h"
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void ImmediateGeometry3D::begin(Mesh::PrimitiveType p_primitive, const Ref<Texture2D> &p_texture) {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_begin(im, (RS::PrimitiveType)p_primitive, p_texture.is_valid() ? p_texture->get_rid() : RID());
|
2014-05-29 13:56:39 +00:00
|
|
|
if (p_texture.is_valid())
|
|
|
|
cached_textures.push_back(p_texture);
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void ImmediateGeometry3D::set_normal(const Vector3 &p_normal) {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_normal(im, p_normal);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void ImmediateGeometry3D::set_tangent(const Plane &p_tangent) {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_tangent(im, p_tangent);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void ImmediateGeometry3D::set_color(const Color &p_color) {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_color(im, p_color);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void ImmediateGeometry3D::set_uv(const Vector2 &p_uv) {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_uv(im, p_uv);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void ImmediateGeometry3D::set_uv2(const Vector2 &p_uv2) {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_uv2(im, p_uv2);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void ImmediateGeometry3D::add_vertex(const Vector3 &p_vertex) {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_vertex(im, p_vertex);
|
2014-05-29 13:56:39 +00:00
|
|
|
if (empty) {
|
2017-06-06 18:33:51 +00:00
|
|
|
aabb.position = p_vertex;
|
2017-03-05 15:44:50 +00:00
|
|
|
aabb.size = Vector3();
|
|
|
|
empty = false;
|
2014-05-29 13:56:39 +00:00
|
|
|
} else {
|
|
|
|
aabb.expand_to(p_vertex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void ImmediateGeometry3D::end() {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_end(im);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void ImmediateGeometry3D::clear() {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RS::get_singleton()->immediate_clear(im);
|
2017-03-05 15:44:50 +00:00
|
|
|
empty = true;
|
2014-05-29 13:56:39 +00:00
|
|
|
cached_textures.clear();
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
AABB ImmediateGeometry3D::get_aabb() const {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
|
|
|
return aabb;
|
|
|
|
}
|
2020-03-26 21:49:16 +00:00
|
|
|
Vector<Face3> ImmediateGeometry3D::get_faces(uint32_t p_usage_flags) const {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
return Vector<Face3>();
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void ImmediateGeometry3D::add_sphere(int p_lats, int p_lons, float p_radius, bool p_add_uv) {
|
2014-10-12 05:13:22 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 1; i <= p_lats; i++) {
|
|
|
|
double lat0 = Math_PI * (-0.5 + (double)(i - 1) / p_lats);
|
|
|
|
double z0 = Math::sin(lat0);
|
|
|
|
double zr0 = Math::cos(lat0);
|
2014-10-12 05:13:22 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double lat1 = Math_PI * (-0.5 + (double)i / p_lats);
|
2014-10-12 05:13:22 +00:00
|
|
|
double z1 = Math::sin(lat1);
|
|
|
|
double zr1 = Math::cos(lat1);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = p_lons; j >= 1; j--) {
|
2014-10-12 05:13:22 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double lng0 = 2 * Math_PI * (double)(j - 1) / p_lons;
|
2014-10-12 05:13:22 +00:00
|
|
|
double x0 = Math::cos(lng0);
|
|
|
|
double y0 = Math::sin(lng0);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
double lng1 = 2 * Math_PI * (double)(j) / p_lons;
|
2014-10-12 05:13:22 +00:00
|
|
|
double x1 = Math::cos(lng1);
|
|
|
|
double y1 = Math::sin(lng1);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 v[4] = {
|
|
|
|
Vector3(x1 * zr0, z0, y1 * zr0),
|
|
|
|
Vector3(x1 * zr1, z1, y1 * zr1),
|
|
|
|
Vector3(x0 * zr1, z1, y0 * zr1),
|
|
|
|
Vector3(x0 * zr0, z0, y0 * zr0)
|
2014-10-12 05:13:22 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#define ADD_POINT(m_idx) \
|
|
|
|
if (p_add_uv) { \
|
|
|
|
set_uv(Vector2(Math::atan2(v[m_idx].x, v[m_idx].z) / Math_PI * 0.5 + 0.5, v[m_idx].y * 0.5 + 0.5)); \
|
|
|
|
set_tangent(Plane(Vector3(-v[m_idx].z, v[m_idx].y, v[m_idx].x), 1)); \
|
|
|
|
} \
|
|
|
|
set_normal(v[m_idx]); \
|
|
|
|
add_vertex(v[m_idx] * p_radius);
|
2014-10-12 05:13:22 +00:00
|
|
|
|
|
|
|
ADD_POINT(0);
|
|
|
|
ADD_POINT(1);
|
|
|
|
ADD_POINT(2);
|
|
|
|
|
|
|
|
ADD_POINT(2);
|
|
|
|
ADD_POINT(3);
|
|
|
|
ADD_POINT(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
void ImmediateGeometry3D::_bind_methods() {
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("begin", "primitive", "texture"), &ImmediateGeometry3D::begin, DEFVAL(Ref<Texture2D>()));
|
|
|
|
ClassDB::bind_method(D_METHOD("set_normal", "normal"), &ImmediateGeometry3D::set_normal);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_tangent", "tangent"), &ImmediateGeometry3D::set_tangent);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_color", "color"), &ImmediateGeometry3D::set_color);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_uv", "uv"), &ImmediateGeometry3D::set_uv);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_uv2", "uv"), &ImmediateGeometry3D::set_uv2);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_vertex", "position"), &ImmediateGeometry3D::add_vertex);
|
|
|
|
ClassDB::bind_method(D_METHOD("add_sphere", "lats", "lons", "radius", "add_uv"), &ImmediateGeometry3D::add_sphere, DEFVAL(true));
|
|
|
|
ClassDB::bind_method(D_METHOD("end"), &ImmediateGeometry3D::end);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear"), &ImmediateGeometry3D::clear);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
ImmediateGeometry3D::ImmediateGeometry3D() {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
im = RenderingServer::get_singleton()->immediate_create();
|
2014-05-29 13:56:39 +00:00
|
|
|
set_base(im);
|
2017-03-05 15:44:50 +00:00
|
|
|
empty = true;
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 21:49:16 +00:00
|
|
|
ImmediateGeometry3D::~ImmediateGeometry3D() {
|
2014-05-29 13:56:39 +00:00
|
|
|
|
2020-03-27 18:21:27 +00:00
|
|
|
RenderingServer::get_singleton()->free(im);
|
2014-05-29 13:56:39 +00:00
|
|
|
}
|