#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 = _col[_colIndex[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 = _col[_colIndex[0]]; v[1]._color = _col[_colIndex[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 = _col[_colIndex[0]]; v[1]._color = _col[_colIndex[1]]; v[2]._color = _col[_colIndex[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) { if (!_coor) Dot(toLonger(p, _z)); else { Position2 pt = _coor->ThisToWorld(p); Dot(toLonger(pt, _z)); } } void Graphics::Line2D(const Position2& p0, const Position2& p1) { if (!_coor) Line(toLonger(p0, _z), toLonger(p1, _z)); else { Position2 pt0 = _coor->ThisToWorld(p0); Position2 pt1 = _coor->ThisToWorld(p1); Line(toLonger(pt0, _z), toLonger(pt1, _z)); } } void Graphics::TriangleSolid2D(const Position2& p0, const Position2& p1, const Position2& p2) { if (!_coor) TriangleSolid(toLonger(p0, _z), toLonger(p1, _z), toLonger(p2, _z)); else { Position2 pt0 = _coor->ThisToWorld(p0); Position2 pt1 = _coor->ThisToWorld(p1); Position2 pt2 = _coor->ThisToWorld(p2); TriangleSolid(toLonger(pt0, _z), toLonger(pt1, _z), toLonger(pt2, _z)); } } 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; 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; 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::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::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