feat: add sphere scene with fixed cube-to-sphere mapping

Added scenes/sphere.{hpp,cpp} using the cube-to-sphere
approach from nrz.cpp, but with corrected math: vertices
are simply normalized to project onto the unit sphere
(the original used a broken formula with p=50.0 as an
exponent).

The sphere uses indexed geometry with position, normal,
and UV attributes, plus a simple diffuse lighting shader.

Press 1/2 to switch between cube and sphere scenes.
Updated .gitignore to exclude generated PNG screenshots.
This commit is contained in:
2026-05-05 23:54:48 +02:00
parent 4a88c8cc06
commit 78d0515e8b
5 changed files with 264 additions and 5 deletions
+16 -4
View File
@@ -13,6 +13,7 @@
#include "cbt/window.hpp"
#include "cbt/opengl/context.hpp"
#include "scenes/cube.hpp"
#include "scenes/sphere.hpp"
auto main(int argc, char const* argv[]) -> int {
float max_duration_seconds = 0.0f;
@@ -23,6 +24,7 @@ auto main(int argc, char const* argv[]) -> int {
fmt::print("Usage: {} [--duration <seconds>] [--help|-h]\n", argv[0]);
fmt::print(" --duration <seconds> Auto-terminate after N seconds (for testing/CI)\n");
fmt::print(" S key Take screenshot (saved as screenshot.png)\n");
fmt::print(" 1/2 key Switch between cube/sphere scene\n");
return 0;
}
if (arg == "--duration" && i + 1 < argc) {
@@ -43,10 +45,14 @@ auto main(int argc, char const* argv[]) -> int {
return 1;
}
auto scn = cbt::scenes::cube();
if (!scn.init()) {
auto cube_scn = cbt::scenes::cube();
auto sphere_scn = cbt::scenes::sphere();
cbt::scene* active_scene = nullptr;
if (!cube_scn.init() || !sphere_scn.init()) {
return 1;
}
active_scene = &cube_scn;
// signal handling + optional duration timer (via ASIO)
asio::io_context io;
@@ -84,13 +90,19 @@ auto main(int argc, char const* argv[]) -> int {
if (glfwGetKey(win.raw(), GLFW_KEY_S) == GLFW_PRESS) {
win.screenshot();
}
if (glfwGetKey(win.raw(), GLFW_KEY_1) == GLFW_PRESS) {
active_scene = &cube_scn;
}
if (glfwGetKey(win.raw(), GLFW_KEY_2) == GLFW_PRESS) {
active_scene = &sphere_scn;
}
auto now = std::chrono::steady_clock::now();
auto dt = std::chrono::duration<float>(now - prev).count();
prev = now;
scn.update(dt);
scn.render();
active_scene->update(dt);
active_scene->render();
win.swap_buffers();
win.poll_events();