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_quat(const char* params) { Lumix::Quat q1, q2; Lumix::Matrix mtx; q1.set(1.0f, 0.0f, 0.0f, 0.0f); LUMIX_EXPECT_CLOSE_EQ(q1.x, 1.0f, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.y, 0.0f, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.z, 0.0f, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.w, 0.0f, 0.001f); q1.toMatrix(mtx); mtx.getRotation(q2); LUMIX_EXPECT_CLOSE_EQ(q1.x, q2.x, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.y, q2.y, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.z, q2.z, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.w, q2.w, 0.001f); q1.set(0, 0, 1, 0); q1.toMatrix(mtx); mtx.getRotation(q2); LUMIX_EXPECT_CLOSE_EQ(q1.x, q2.x, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.y, q2.y, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.z, q2.z, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.w, q2.w, 0.001f); q1 = Lumix::Quat(Lumix::Vec3(1, 0, 0), Lumix::Math::PI / 4); q1.toMatrix(mtx); mtx.getRotation(q2); LUMIX_EXPECT_CLOSE_EQ(q1.x, q2.x, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.y, q2.y, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.z, q2.z, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.w, q2.w, 0.001f); q1 = Lumix::Quat(Lumix::Vec3(1, 1, 0), Lumix::Math::PI / 6); q1.normalize(); q1.toMatrix(mtx); mtx.getRotation(q2); LUMIX_EXPECT_CLOSE_EQ(q1.x, q2.x, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.y, q2.y, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.z, q2.z, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.w, q2.w, 0.001f); q1.set(0.923879862f, 0, 0, -0.382683545f); q1.toMatrix(mtx); mtx.getRotation(q2); LUMIX_EXPECT_CLOSE_EQ(q1.x, q2.x, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.y, q2.y, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.z, q2.z, 0.001f); LUMIX_EXPECT_CLOSE_EQ(q1.w, q2.w, 0.001f); Lumix::Quat qeuler; for (float x = -Lumix::Math::PI + 0.01f; x < Lumix::Math::PI; x += 0.1f) { for (float y = -Lumix::Math::PI + 0.01f; y < Lumix::Math::PI; y += 0.1f) { float x = 0; for (float z = -Lumix::Math::PI + 0.01f; z < Lumix::Math::PI; z += 0.1f) { qeuler.fromEuler(Lumix::Vec3(x, y, z)); Lumix::Vec3 u(0, 1, 0); u = qeuler * u; Lumix::Vec3 euler = qeuler.toEuler(); LUMIX_EXPECT_CLOSE_EQ(euler.x, x, 0.001f); LUMIX_EXPECT_CLOSE_EQ(euler.y, y, 0.001f); LUMIX_EXPECT_CLOSE_EQ(euler.z, z, 0.001f); } } } }
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); }
void UT_aabb(const char* params) { Lumix::AABB aabb1; Lumix::AABB aabb2(Lumix::Vec3(0, 0, 0), Lumix::Vec3(1, 1, 1)); LUMIX_EXPECT(aabb2.min.x == 0); LUMIX_EXPECT(aabb2.min.y == 0); LUMIX_EXPECT(aabb2.min.z == 0); LUMIX_EXPECT(aabb2.max.x == 1); LUMIX_EXPECT(aabb2.max.y == 1); LUMIX_EXPECT(aabb2.max.z == 1); aabb1 = aabb2; LUMIX_EXPECT(aabb1.min.x == aabb2.min.x); LUMIX_EXPECT(aabb1.min.y == aabb2.min.y); LUMIX_EXPECT(aabb1.min.z == aabb2.min.z); LUMIX_EXPECT(aabb1.max.x == aabb2.max.x); LUMIX_EXPECT(aabb1.max.y == aabb2.max.y); LUMIX_EXPECT(aabb1.max.z == aabb2.max.z); Lumix::Vec3 points[8]; aabb2.getCorners(Lumix::Matrix::IDENTITY, points); LUMIX_EXPECT(points[0].x == 0); LUMIX_EXPECT(points[0].y == 0); LUMIX_EXPECT(points[0].z == 0); LUMIX_EXPECT(points[1].x == 0); LUMIX_EXPECT(points[1].y == 0); LUMIX_EXPECT(points[1].z == 1); LUMIX_EXPECT(points[2].x == 0); LUMIX_EXPECT(points[2].y == 1); LUMIX_EXPECT(points[2].z == 0); LUMIX_EXPECT(points[3].x == 0); LUMIX_EXPECT(points[3].y == 1); LUMIX_EXPECT(points[3].z == 1); LUMIX_EXPECT(points[4].x == 1); LUMIX_EXPECT(points[4].y == 0); LUMIX_EXPECT(points[4].z == 0); LUMIX_EXPECT(points[5].x == 1); LUMIX_EXPECT(points[5].y == 0); LUMIX_EXPECT(points[5].z == 1); LUMIX_EXPECT(points[6].x == 1); LUMIX_EXPECT(points[6].y == 1); LUMIX_EXPECT(points[6].z == 0); LUMIX_EXPECT(points[7].x == 1); LUMIX_EXPECT(points[7].y == 1); LUMIX_EXPECT(points[7].z == 1); Lumix::AABB aabb3(Lumix::Vec3(0, 0, 0), Lumix::Vec3(1, 1, 1)); Lumix::AABB aabb4(Lumix::Vec3(1, 2, 3), Lumix::Vec3(2, 3, 4)); Lumix::Matrix mtx = Lumix::Matrix::IDENTITY; mtx.setTranslation(Lumix::Vec3(1, 2, 3)); aabb3.transform(mtx); LUMIX_EXPECT(aabb3.min.x == aabb4.min.x); LUMIX_EXPECT(aabb3.min.y == aabb4.min.y); LUMIX_EXPECT(aabb3.min.z == aabb4.min.z); LUMIX_EXPECT(aabb3.max.x == aabb4.max.x); LUMIX_EXPECT(aabb3.max.y == aabb4.max.y); LUMIX_EXPECT(aabb3.max.z == aabb4.max.z); }