/* Copyright (c) 2013 Daniele Bartolini, Michele Rossi Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto 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 "DebugRenderer.h" #include "Renderer.h" #include "MathUtils.h" namespace crown { //----------------------------------------------------------------------------- DebugRenderer::DebugRenderer(Renderer& renderer) : m_renderer(renderer), m_lines_count(0) { } //----------------------------------------------------------------------------- DebugRenderer::~DebugRenderer() { } //----------------------------------------------------------------------------- void DebugRenderer::add_line(const Vector3& start, const Vector3& end, const Color4& color, bool depth_write) { if (m_lines_count >= MAX_DEBUG_LINES) { return; } m_lines[m_lines_count * 2 + 0] = start; m_lines[m_lines_count * 2 + 1] = end; m_colors[m_lines_count * 2 + 0] = color; m_colors[m_lines_count * 2 + 1] = color; m_depth_writes[m_lines_count * 2 + 0] = depth_write; m_depth_writes[m_lines_count * 2 + 1] = depth_write; m_lines_count++; } //----------------------------------------------------------------------------- void DebugRenderer::add_sphere(const Vector3& center, const float radius, const Color4& color, bool depth_write) { const uint32_t deg_step = 15; // XZ plane for (uint32_t deg = 0; deg < 360; deg += deg_step) { float rad_0 = math::deg_to_rad(deg); float rad_1 = math::deg_to_rad(deg + deg_step); Vector3 start(math::cos(rad_0) * radius, 0, -math::sin(rad_0) * radius); Vector3 end (math::cos(rad_1) * radius, 0, -math::sin(rad_1) * radius); add_line(center + start, center + end, color, depth_write); } // XY plane for (uint32_t deg = 0; deg < 360; deg += deg_step) { float rad_0 = math::deg_to_rad(deg); float rad_1 = math::deg_to_rad(deg + deg_step); Vector3 start(math::cos(rad_0) * radius, math::sin(rad_0) * radius, 0); Vector3 end (math::cos(rad_1) * radius, math::sin(rad_1) * radius, 0); add_line(center + start, center + end, color, depth_write); } // YZ plane for (uint32_t deg = 0; deg < 360; deg += deg_step) { float rad_0 = math::deg_to_rad(deg); float rad_1 = math::deg_to_rad(deg + deg_step); Vector3 start(0, math::sin(rad_0) * radius, -math::cos(rad_0) * radius); Vector3 end (0, math::sin(rad_1) * radius, -math::cos(rad_1) * radius); add_line(center + start, center + end, color, depth_write); } } //----------------------------------------------------------------------------- void DebugRenderer::add_box(const Vector3& min, const Vector3& max, const Color4& color, bool depth_write) { // Back lines add_line(min , Vector3(max.x, min.y, min.z), color, depth_write); add_line(Vector3(max.x, min.y, min.z), Vector3(max.x, max.y, min.z), color, depth_write); add_line(Vector3(max.x, max.y, min.z), Vector3(min.x, max.y, min.z), color, depth_write); add_line(Vector3(min.x, max.y, min.z), min , color, depth_write); // Front lines add_line(Vector3(min.x, min.y, max.z), Vector3(max.x, min.y, max.z), color, depth_write); add_line(Vector3(max.x, min.y, max.z), Vector3(max.x, max.y, max.z), color, depth_write); add_line(Vector3(max.x, max.y, max.z), Vector3(min.x, max.y, max.z), color, depth_write); add_line(Vector3(min.x, max.y, max.z), Vector3(min.x, min.y, max.z), color, depth_write); // Connect back and front vertices add_line(min , Vector3(min.x, min.y, max.z), color, depth_write); add_line(Vector3(max.x, min.y, min.z), Vector3(max.x, min.y, max.z), color, depth_write); add_line(Vector3(max.x, max.y, min.z), Vector3(max.x, max.y, max.z), color, depth_write); add_line(Vector3(min.x, max.y, min.z), Vector3(min.x, max.y, max.z), color, depth_write); } //----------------------------------------------------------------------------- void DebugRenderer::add_pose(const Matrix4x4& pose, bool depth_write) { add_line(pose.translation(), pose.translation() + pose.x(), Color4::RED, depth_write); add_line(pose.translation(), pose.translation() + pose.y(), Color4::GREEN, depth_write); add_line(pose.translation(), pose.translation() + pose.z(), Color4::BLUE, depth_write); } //----------------------------------------------------------------------------- void DebugRenderer::draw_all() { if (m_lines_count > 0) { // m_renderer.draw_lines(m_lines[0].to_float_ptr(), m_colors[0].to_float_ptr(), m_lines_count * 2); } m_lines_count = 0; } } // namespace crown