void EditorIcon::render(PipelineInstance& pipeline) { static const float MIN_SCALE_FACTOR = 10; static const float MAX_SCALE_FACTOR = 60; if (m_is_visible) { const Universe& universe = m_scene->getUniverse(); ComponentIndex camera = m_scene->getCameraInSlot("editor"); Lumix::Matrix mtx = universe.getMatrix(m_scene->getCameraEntity(camera)); float fov = m_scene->getCameraFOV(camera); Vec3 position = universe.getPosition(m_entity); float distance = (position - mtx.getTranslation()).length(); float scaleFactor = MIN_SCALE_FACTOR + distance; scaleFactor = Math::clamp(scaleFactor, MIN_SCALE_FACTOR, MAX_SCALE_FACTOR); float scale = tan(Math::degreesToRadians(fov) * 0.5f) * distance / scaleFactor; mtx.setTranslation(position); Matrix scale_mtx = Matrix::IDENTITY; m_matrix = mtx; scale_mtx.m11 = scale_mtx.m22 = scale_mtx.m33 = scale > 0 ? scale : 1; mtx = mtx * scale_mtx; m_scale = scale; if (m_model->isReady()) { pipeline.renderModel(*m_model, mtx); } } }
void UT_matrix(const char* params) { Lumix::DefaultAllocator allocator; for (int i = 0; i < 16; ++i) { LUMIX_EXPECT_CLOSE_EQ(*((&Lumix::Matrix::IDENTITY.m11) + i), i % 5 == 0 ? 1.0f : 0.0f, 0.001f); } Lumix::Matrix mtx = Lumix::Matrix::IDENTITY; expectSameMatrices(mtx, Lumix::Matrix::IDENTITY); LUMIX_EXPECT_CLOSE_EQ(mtx.determinant(), 1.0f, 0.001f); mtx.inverse(); expectSameMatrices(mtx, Lumix::Matrix::IDENTITY); mtx.fastInverse(); expectSameMatrices(mtx, Lumix::Matrix::IDENTITY); mtx.transpose(); expectSameMatrices(mtx, Lumix::Matrix::IDENTITY); LUMIX_EXPECT_CLOSE_EQ(mtx.getTranslation().x, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getTranslation().y, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getTranslation().z, 0, 0.001f); Lumix::Quat rot; mtx.getRotation(rot); LUMIX_EXPECT_CLOSE_EQ(rot.x, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(rot.y, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(rot.z, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(rot.w, 1, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getXVector().x, 1, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getXVector().y, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getXVector().z, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getYVector().x, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getYVector().y, 1, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getYVector().z, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getZVector().x, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getZVector().y, 0, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.getZVector().z, 1, 0.001f); mtx.multiply3x3(1.0f); expectSameMatrices(mtx, Lumix::Matrix::IDENTITY); Lumix::Vec3 v = mtx.multiplyPosition(Lumix::Vec3(1, 2, 3)); LUMIX_EXPECT_CLOSE_EQ(v.x, 1, 0.001f); LUMIX_EXPECT_CLOSE_EQ(v.y, 2, 0.001f); LUMIX_EXPECT_CLOSE_EQ(v.z, 3, 0.001f); mtx.setTranslation(Lumix::Vec3(1, 2, 3)); LUMIX_EXPECT_CLOSE_EQ(mtx.m41, 1, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m42, 2, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m43, 3, 0.001f); mtx.setIdentity(); expectSameMatrices(mtx, Lumix::Matrix::IDENTITY); mtx.translate(Lumix::Vec3(1, 2, 3)); LUMIX_EXPECT_CLOSE_EQ(mtx.m41, 1, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m42, 2, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m43, 3, 0.001f); mtx.setIdentity(); mtx.setXVector(Lumix::Vec3(3, 2, 1)); LUMIX_EXPECT_CLOSE_EQ(mtx.m11, 3, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m12, 2, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m13, 1, 0.001f); mtx.setYVector(Lumix::Vec3(6, 5, 4)); LUMIX_EXPECT_CLOSE_EQ(mtx.m21, 6, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m22, 5, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m23, 4, 0.001f); mtx.setZVector(Lumix::Vec3(9, 8, 7)); LUMIX_EXPECT_CLOSE_EQ(mtx.m31, 9, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m32, 8, 0.001f); LUMIX_EXPECT_CLOSE_EQ(mtx.m33, 7, 0.001f); Lumix::Matrix old = mtx; expectSameMatrices(mtx, old); mtx.transpose(); mtx.transpose(); expectSameMatrices(mtx, old); mtx.inverse(); mtx.inverse(); expectSameMatrices(mtx, old); mtx.setIdentity(); mtx.multiply3x3(2.0f); old = mtx; mtx.inverse(); mtx.inverse(); expectSameMatrices(mtx, old); for (int i = 0; i < 16; ++i) { *((&mtx.m11) + i) = (float)i; } old = mtx; mtx = mtx * Lumix::Matrix::IDENTITY; expectSameMatrices(mtx, old); mtx = Lumix::Matrix::IDENTITY * mtx; expectSameMatrices(mtx, old); Lumix::Matrix euler; euler.fromEuler(0, 0, 0); expectSameMatrices(euler, Lumix::Matrix::IDENTITY); Lumix::Matrix check_matrix; check_matrix = Lumix::Matrix::IDENTITY; check_matrix.m11 = -1; check_matrix.m33 = -1; euler.fromEuler(3.14159265f, 0, 0); expectSameMatrices(euler, check_matrix); check_matrix = Lumix::Matrix::IDENTITY; check_matrix.m11 = -1; check_matrix.m22 = -1; euler.fromEuler(0, 0, 3.14159265f); expectSameMatrices(euler, check_matrix); check_matrix = Lumix::Matrix::IDENTITY; check_matrix.m22 = -1; check_matrix.m33 = -1; euler.fromEuler(0, 3.14159265f, 0); expectSameMatrices(euler, check_matrix); }