Files
cuber/scenes/sphere.cpp
T
portersky 78d0515e8b 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.
2026-05-05 23:54:48 +02:00

196 lines
6.5 KiB
C++

#include <cstddef>
#include <cmath>
#include <vector>
#include "glad/glad.h"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "scenes/sphere.hpp"
namespace cbt::scenes {
sphere::sphere() {
m_start = std::chrono::steady_clock::now();
}
auto sphere::init() -> bool {
if (!build_shader()) {
return false;
}
build_mesh();
glEnable(GL_DEPTH_TEST);
return true;
}
auto sphere::update(float) -> void {}
auto sphere::render() -> void {
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration<float>(now - m_start).count();
glClearColor(0.15f, 0.15f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
auto aspect = 1280.0f / 720.0f;
auto proj = glm::perspective(glm::radians(45.0f), aspect, 0.1f, 100.0f);
auto view = glm::translate(glm::mat4{1.0f}, glm::vec3{0.0f, 0.0f, -4.0f});
auto model = glm::rotate(glm::mat4{1.0f}, elapsed, glm::vec3{1.0f, 0.3f, 0.2f});
m_prog.use();
glUniformMatrix4fv(m_loc_proj, 1, GL_FALSE, glm::value_ptr(proj));
glUniformMatrix4fv(m_loc_view, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(m_loc_model, 1, GL_FALSE, glm::value_ptr(model));
m_vao.bind();
glDrawElements(GL_TRIANGLES, m_index_count, GL_UNSIGNED_INT, nullptr);
m_vao.unbind();
m_prog.unuse();
}
auto sphere::build_mesh() -> void {
struct vertex {
glm::vec3 position;
glm::vec3 normal;
glm::vec2 uv;
};
std::uint32_t const div = 32;
std::vector<vertex> vertices;
std::vector<std::uint32_t> indices;
// Generate 6 cube faces, each with div x div vertices
auto add_face = [&](glm::vec3 const& center, glm::vec3 const& u_axis,
glm::vec3 const& v_axis) -> void {
for (std::uint32_t i = 0; i < div; ++i) {
for (std::uint32_t j = 0; j < div; ++j) {
float const s = float(i) / float(div - 1) * 2.0f - 1.0f;
float const t = float(j) / float(div - 1) * 2.0f - 1.0f;
// Position on cube face
glm::vec3 pos = center + u_axis * s + v_axis * t;
// FIX: normalize to project onto unit sphere
// (the original nrz.cpp used a broken formula with p=50.0)
float const len = glm::length(pos);
glm::vec3 normal = pos / len;
vertices.push_back({normal, normal, {float(i) / float(div - 1), float(j) / float(div - 1)}});
}
}
};
// +X face (right)
add_face(glm::vec3{1.0f, 0.0f, 0.0f}, glm::vec3{0.0f, 1.0f, 0.0f}, glm::vec3{0.0f, 0.0f, 1.0f});
// -X face (left)
add_face(glm::vec3{-1.0f, 0.0f, 0.0f}, glm::vec3{0.0f, 1.0f, 0.0f}, glm::vec3{0.0f, 0.0f, -1.0f});
// +Y face (top)
add_face(glm::vec3{0.0f, 1.0f, 0.0f}, glm::vec3{1.0f, 0.0f, 0.0f}, glm::vec3{0.0f, 0.0f, -1.0f});
// -Y face (bottom)
add_face(glm::vec3{0.0f, -1.0f, 0.0f}, glm::vec3{1.0f, 0.0f, 0.0f}, glm::vec3{0.0f, 0.0f, 1.0f});
// +Z face (front)
add_face(glm::vec3{0.0f, 0.0f, 1.0f}, glm::vec3{1.0f, 0.0f, 0.0f}, glm::vec3{0.0f, 1.0f, 0.0f});
// -Z face (back)
add_face(glm::vec3{0.0f, 0.0f, -1.0f}, glm::vec3{-1.0f, 0.0f, 0.0f}, glm::vec3{0.0f, 1.0f, 0.0f});
// Generate indices for each face
std::uint32_t offset = 0;
for (std::uint32_t face = 0; face < 6; ++face) {
for (std::uint32_t i = 0; i < div - 1; ++i) {
for (std::uint32_t j = 0; j < div - 1; ++j) {
std::uint32_t const a = offset + i * div + j;
std::uint32_t const b = offset + (i + 1) * div + j;
std::uint32_t const c = offset + (i + 1) * div + j + 1;
std::uint32_t const d = offset + i * div + j + 1;
// Two triangles per quad (consistent winding)
indices.push_back(a);
indices.push_back(b);
indices.push_back(d);
indices.push_back(b);
indices.push_back(c);
indices.push_back(d);
}
}
offset += div * div;
}
m_index_count = static_cast<GLsizei>(indices.size());
m_vbo.upload(vertices.data(), vertices.size() * sizeof(vertex));
m_ebo.upload(indices.data(), indices.size() * sizeof(std::uint32_t));
m_vao.bind();
m_vbo.bind();
m_ebo.bind();
// location 0: position (vec3)
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), nullptr);
// location 1: normal (vec3)
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex),
reinterpret_cast<void*>(offsetof(vertex, normal)));
// location 2: uv (vec2)
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex),
reinterpret_cast<void*>(3 * sizeof(float) + 3 * sizeof(float)));
m_ebo.unbind();
m_vbo.unbind();
m_vao.unbind();
}
auto sphere::build_shader() -> bool {
char const* vert_src = R"glsl(
#version 410 core
layout(location = 0) in vec3 a_pos;
layout(location = 1) in vec3 a_normal;
layout(location = 2) in vec2 a_uv;
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_proj;
out vec3 v_normal;
out vec3 v_world_pos;
out vec2 v_uv;
void main() {
gl_Position = u_proj * u_view * u_model * vec4(a_pos, 1.0);
v_normal = mat3(u_model) * a_normal;
v_world_pos = (u_model * vec4(a_pos, 1.0)).xyz;
v_uv = a_uv;
}
)glsl";
char const* frag_src = R"glsl(
#version 410 core
in vec3 v_normal;
in vec3 v_world_pos;
in vec2 v_uv;
out vec4 frag_color;
void main() {
vec3 light_dir = normalize(vec3(1.0, 1.0, 2.0));
vec3 normal = normalize(v_normal);
float diff = max(dot(normal, light_dir), 0.0);
vec3 ambient = vec3(0.2);
vec3 color = vec3(0.4, 0.6, 1.0);
vec3 result = (ambient + diff * 0.8) * color;
frag_color = vec4(result, 1.0);
}
)glsl";
if (!m_prog.compile_vertex(vert_src) || !m_prog.compile_fragment(frag_src) || !m_prog.link()) {
return false;
}
m_loc_proj = glGetUniformLocation(m_prog.id(), "u_proj");
m_loc_view = glGetUniformLocation(m_prog.id(), "u_view");
m_loc_model = glGetUniformLocation(m_prog.id(), "u_model");
return true;
}
}