#include "dl_graphics.h" #ifndef DL_DISABLE_RENDER #include "vk/dl_vk.h" #include "math/dl_coor.h" namespace dl { void Graphics::Dot(const Position3& p) { std::array v; v[0]._color = _get_color(0); v[0]._pos = p; g_vk->GfxPushDot(v[0]); } void Graphics::Line(const Position3& p0, const Position3& p1) { std::array v; v[0]._color = _get_color(0); v[1]._color = _get_color(1); v[0]._pos = p0; v[1]._pos = p1; g_vk->GfxPushLine(v[0], v[1]); } void Graphics::TriangleSolid(const Position3& p0, const Position3& p1, const Position3& p2) { std::array v; v[0]._color = _get_color(0); v[1]._color = _get_color(1); v[2]._color = _get_color(2); v[0]._pos = p0; v[1]._pos = p1; v[2]._pos = p2; g_vk->GfxPushTriangle(v[0], v[1], v[2]); } void Graphics::Triangle(const Position3& p0, const Position3& p1, const Position3& p2) { _colIndex[0] = 0; _colIndex[1] = 1; Line(p0, p1); _colIndex[0] = 1; _colIndex[1] = 2; Line(p1, p2); _colIndex[0] = 2; _colIndex[1] = 0; Line(p2, p0); _reset_col_index(); } void Graphics::Quad(const Position3& p0, const Position3& p1, const Position3& p2, const Position3& p3) { _colIndex[0] = 0; _colIndex[1] = 1; Line(p0, p1); _colIndex[0] = 1; _colIndex[1] = 2; Line(p1, p2); _colIndex[0] = 2; _colIndex[1] = 3; Line(p2, p3); _colIndex[0] = 3; _colIndex[1] = 0; Line(p3, p0); _reset_col_index(); } void Graphics::QuadSolid(const Position3& p0, const Position3& p1, const Position3& p2, const Position3& p3) { _colIndex[0] = 0; _colIndex[1] = 1; _colIndex[2] = 2; TriangleSolid(p0, p1, p2); _colIndex[0] = 0; _colIndex[1] = 2; _colIndex[2] = 3; TriangleSolid(p0, p2, p3); _reset_col_index(); } void Graphics::Rectangle(const Position3& p0, const Position3& p1) { Quad(p0, { p1[0], p0[1] }, p1, { p0[0], p1[1] }); } void Graphics::RectangleSolid(const Position3& p0, const Position3& p1) { QuadSolid(p0, { p1[0], p0[1] }, p1, { p0[0], p1[1] }); } void Graphics::Polygon(const std::vector& vec_pos) { for_couple(vec_pos, [&](const Position3& p0, const Position3& p1) { Line(p0, p1); }, true); } void Graphics::PolygonSolid(const std::vector& vec_pos) { Position3 center = Accumulate::Add(vec_pos) / (Float)vec_pos.size(); for_couple(vec_pos, [&](const Position3& p0, const Position3& p1) { TriangleSolid(center, p0, p1); }, true); } void Graphics::Love() { //f(x)=(0.64*sqrt(abs(x))-0.8+1.2^abs(x)*cos(200*x))*sqrt(cos(x)) std::vector vec; constexpr float pi_2 = (float)Math::PI(1, 2); for (float x = -pi_2; x < pi_2; x += 0.001f) { float y = (0.64f * sqrtf(std::abs(x)) - 0.8f + std::pow(1.2f, std::abs(x)) * cosf(64 * x)) * sqrtf(cosf(x)); vec.push_back({ x * 100.0f, (1.0f - y) * 100.0f }); //Dot(vec.back()); } // 色带分为6步 size_t i = 0; for_couple(vec, [&](const Position3& p0, const Position3& p1) { float cur = (float)i / vec.size() * 6.0f; Color col; if (cur < 1.0f) col = { 1.0f, 0.0f, cur, 1.0f }; else if (cur < 2.0f) col = { 2.0f - cur, 0.0f, 1.0f, 1.0f }; else if (cur < 3.0f) col = { 0.0f, cur - 2.0f, 1.0f, 1.0f }; else if (cur < 4.0f) col = { 0.0f, 1.0f, 4.0f - cur, 1.0f }; else if (cur < 5.0f) col = { cur - 4.0f, 1.0f, 0.0f, 1.0f }; else col = { 1.0f, 6.0f - cur, 0.0f, 1.0f }; SetColor(col); ++i; Line(p0, p1); }); } void Graphics::Dot2D(const Position2& p) { std::array v; v[0]._color = _get_color(0); v[0]._pos = _get_pos_2d(p); g_vk->GfxPushDot2D(v[0]); } void Graphics::Line2D(const Position2& p0, const Position2& p1) { std::array v; v[0]._color = _get_color(0); v[1]._color = _get_color(1); v[0]._pos = _get_pos_2d(p0); v[1]._pos = _get_pos_2d(p1); g_vk->GfxPushLine2D(v[0], v[1]); } void Graphics::Line2DEx(const Position2& p0, const Position2& p1) { std::array v; v[0]._color = _get_color(0); v[1]._color = _get_color(1); v[0]._pos = _get_pos_2d(p0); v[1]._pos = _get_pos_2d(p1); Position dir = Math::Normalize(p1 - p0); v[1]._pos.x += dir.x * 0.5f; v[1]._pos.y += dir.y * 0.5f; g_vk->GfxPushLine2D(v[0], v[1]); } void Graphics::TriangleSolid2D(const Position2& p0, const Position2& p1, const Position2& p2) { std::array v; v[0]._color =_get_color(0); v[1]._color =_get_color(1); v[2]._color =_get_color(2); v[0]._pos = _get_pos_2d(p0); v[1]._pos = _get_pos_2d(p1); v[2]._pos = _get_pos_2d(p2); g_vk->GfxPushTriangle2D(v[0], v[1], v[2]); } void Graphics::Triangle2D(const Position2& p0, const Position2& p1, const Position2& p2) { _colIndex[0] = 0; _colIndex[1] = 1; Line2D(p0, p1); _colIndex[0] = 1; _colIndex[1] = 2; Line2D(p1, p2); _colIndex[0] = 2; _colIndex[1] = 0; _bPolygonEx ? Line2DEx(p2, p0) : Line2D(p2, p0); _reset_col_index(); } void Graphics::Quad2D(const Position2& p0, const Position2& p1, const Position2& p2, const Position2& p3) { _colIndex[0] = 0; _colIndex[1] = 1; Line2D(p0, p1); _colIndex[0] = 1; _colIndex[1] = 2; Line2D(p1, p2); _colIndex[0] = 2; _colIndex[1] = 3; Line2D(p2, p3); _colIndex[0] = 3; _colIndex[1] = 0; _bPolygonEx ? Line2DEx(p3, p0) : Line2D(p3, p0); _reset_col_index(); } void Graphics::QuadSolid2D(const Position2& p0, const Position2& p1, const Position2& p2, const Position2& p3) { _colIndex[0] = 0; _colIndex[1] = 1; _colIndex[2] = 2; TriangleSolid2D(p0, p1, p2); _colIndex[0] = 0; _colIndex[1] = 2; _colIndex[2] = 3; TriangleSolid2D(p0, p2, p3); _reset_col_index(); } void Graphics::Rectangle2D(const Position2& p0, const Position2& p1) { Quad2D(p0, { p1[0], p0[1] }, p1, { p0[0], p1[1] }); } void Graphics::RectangleSolid2D(const Position2& p0, const Position2& p1) { QuadSolid2D(p0, { p1[0], p0[1] }, p1, { p0[0], p1[1] }); } void Graphics::Polygon2D(const std::vector& vec_pos) { if (vec_pos.size() < 3) return; for_couple(vec_pos, [&](const Position2& p0, const Position2& p1) { Line2D(p0, p1); }, !_bPolygonEx); if (_bPolygonEx) { Line2DEx(vec_pos[vec_pos.size() - 1], vec_pos[0]); } } void Graphics::PolygonSolid2D(const std::vector& vec_pos) { Position2 center = Accumulate::Add(vec_pos) / (Float)vec_pos.size(); for_couple(vec_pos, [&](const Position2& p0, const Position2& p1) { TriangleSolid2D(center, p0, p1); }, true); } void Graphics::PolygonRegular2D(const Position2& pos, Float r, unsigned n) { if (n < 3) return; std::vector mul_vertex; Float a = (Float)Math::PI(2u, n); for (unsigned i = 0; i < n; ++i) { Float x = r * std::cos(i * a); Float y = r * std::sin(i * a); mul_vertex.push_back({ pos[0] + x, pos[1] + y }); } size_t i = 0; for_couple(mul_vertex, [&](const Position2& p0, const Position2& p1) { _colIndex[0] = i + 0; _colIndex[1] = i + 1; Line2D(p0, p1); ++i; }, !_bPolygonEx); if (_bPolygonEx) { Line2DEx(mul_vertex[mul_vertex.size() - 1], mul_vertex[0]); } } void Graphics::PolygonRegularSolid2D(const Position2& pos, Float r, unsigned n) { std::vector mul_vertex; Float a = (Float)Math::PI(2u, n); for (unsigned i = 0; i < n; ++i) { Float x = r * std::cos(i * a); Float y = r * std::sin(i * a); mul_vertex.push_back({ pos[0] + x, pos[1] + y }); } size_t i = 0; for_couple(mul_vertex, [&](const Position2& p0, const Position2& p1) { _colIndex[0] = -1; _colIndex[1] = i + 0; _colIndex[2] = i + 1; TriangleSolid2D(pos, p0, p1); ++i; }, true); } void Graphics::RenderCoor(Coor* coor, bool to_parent) { SetCoor(coor); SetColor(ColorDef::RED); Line2D({}, { 32, 0}); SetColor(ColorDef::GREEN); Line2D({}, { 0, 32}); // 指向父节点 if (to_parent) { Coor* parent = coor->GetParent(); if (parent) { SetCoor(parent); SetColor(ColorDef::GREEN); Line2D(coor->GetPosition(), {}); } } } void Graphics::StrokeLine(const Position2& p0, const Position2& p1, Float width /*= 1.0_f*/, bool round /*= false*/) { dl::Quad q = createQuad(p0, p1, width); QuadSolid2D(q); if (round) { } } //void Graphics::_set_color_index(size_t a) //{ // _colIndex[0] = a < _col.size() ? a : -1; //} // //void Graphics::_set_color_index(size_t a, size_t b) //{ // _colIndex[0] = a < _col.size() ? a : -1; // _colIndex[1] = b < _col.size() ? b : -1; //} // //void Graphics::_set_color_index(size_t a, size_t b, size_t c) //{ // _colIndex[0] = a < _col.size() ? a : -1; // _colIndex[1] = b < _col.size() ? b : -1; // _colIndex[2] = c < _col.size() ? c : -1; //} //void Graphics::SetMatrix(Coor* coor) //{ // Matrix4 m_t = Mat::CreateTranslation(toLonger(coor->GetPosition())); // Matrix4 m_r = Mat::CreateRotation({ 0, 0, 1 }, coor->GetRotate()); // Matrix4 m_s = Mat::CreateScaling(toLonger(coor->GetPosition(), 1.0_f)); // _mat = m_t * m_r * m_s; //} } #endif