CoordinateFrame GLight::frame() const { CoordinateFrame f; if (rightDirection == Vector3::zero()) { // No specified right direction; choose one automatically if (position.w == 0) { // Directional light f.lookAt(-position.xyz()); } else { // Spot light f.lookAt(spotDirection); } } else { const Vector3& Z = -spotDirection.direction(); Vector3 X = rightDirection.direction(); // Ensure the vectors are not too close together while (abs(X.dot(Z)) > 0.9f) { X = Vector3::random(); } // Ensure perpendicular X -= Z * Z.dot(X); const Vector3& Y = Z.cross(X); f.rotation.setColumn(Vector3::X_AXIS, X); f.rotation.setColumn(Vector3::Y_AXIS, Y); f.rotation.setColumn(Vector3::Z_AXIS, Z); } f.translation = position.xyz(); return f; }
void testCoordinateFrame() { printf("CoordinateFrame "); { // Easy case CoordinateFrame c; c.lookAt(Vector3(-1, 0, -1)); float h = c.getHeading(); debugAssert(fuzzyEq(h, G3D::pi() / 4)); } // Test getHeading at a variety of angles for (int i = -175; i <= 175; i += 5) { CoordinateFrame c; float h = c.getHeading(); debugAssert(h == 0); c.rotation = Matrix3::fromAxisAngle(Vector3::unitY(), toRadians(i)); h = c.getHeading(); debugAssert(fuzzyEq(h, toRadians(i))); } printf("passed\n"); }
/** Two slanted green ramps. */ void Demo::insertRamps() { { Box b(Vector3(-1, 0, -5), Vector3(1, .25f, 5.5f)); CoordinateFrame c; c.lookAt(Vector3(0, 1, 2)); c.translation = Vector3(-2.5f, 2.25f, 5.5f); scene.insertStatic(new BoxObject(c.toWorldSpace(b), (Color3::green() + Color3::white()) / 2)); } // Corner ramp { Box b(Vector3(-1, 0, -5), Vector3(1, .25f, 5.5f)); CoordinateFrame c; c.lookAt(Vector3(-2, 2, -2)); c.translation = Vector3(-11.2f, 2.85f, -7.2f); scene.insertStatic(new BoxObject(c.toWorldSpace(b), (Color3::green() + Color3::white()) / 2)); } }
void Demo::insertSpiralSlide() { int i; for (i = 0; i < 41; ++i) { double angle = pi() * i / 10.0; double angle2 = pi() * (i - 0.6) / 10.0; // Outer spiral CoordinateFrame c; Box b(Vector3(-1, -1, -.1f), Vector3(1, 1, .1f)); c.translation = Vector3(cos(angle) * 2.9f, i / 3.5f + 1.5f, sin(angle) * 2.9f); c.lookAt(Vector3(cos(angle2) * 1.5f, i / 3.5 + 2.2f, sin(angle2) * 1.5f)); scene.insertStatic(new BoxObject(c.toWorldSpace(b), (Color3::yellow() + Color3::white()) / 2)); // Inner inner spiral { Box b(Vector3(-.3f, -.3f, -.1f), Vector3(.25f, .25f, .1f)); c.translation = Vector3(cos(angle) * 1.2f, i / 3.5f + 1, sin(angle) * 1.2f); c.lookAt(Vector3(cos(angle2) * 3, i / 3.5f + 2, sin(angle2) * 3)); scene.insertStatic(new BoxObject(c.toWorldSpace(b), (Color3::yellow() + Color3::white()) / 2)); } } scene.insertDynamic(new SimSphere(Sphere(Vector3(1.9f, 13, -1), .75f), Vector3(-2,-.5f,-2), Color3::blue())); }