Files
cuber/scenes/sphere.cpp
T
portersky 91fe3c6e8c style: color-code sphere faces to visualize seams
Each of the 6 cube-to-sphere faces now has a distinct color
(red, green, blue, yellow, magenta, cyan) passed via a
vertex color attribute. The fragment shader multiplies
lighting by this color instead of a uniform blue.

This makes the cube-face seams clearly visible when
inspecting the mapped sphere.
2026-05-06 00:01:17 +02:00

215 lines
7.1 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;
glm::vec3 color;
};
std::uint32_t const div = 32;
std::vector<vertex> vertices;
std::vector<std::uint32_t> indices;
// Distinct colors for each cube face so seams are visible
glm::vec3 const face_colors[6] = {
{1.0f, 0.2f, 0.2f}, // +X red
{0.2f, 1.0f, 0.2f}, // -X green
{0.2f, 0.2f, 1.0f}, // +Y blue
{1.0f, 1.0f, 0.2f}, // -Y yellow
{1.0f, 0.2f, 1.0f}, // +Z magenta
{0.2f, 1.0f, 1.0f}, // -Z cyan
};
// 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, std::uint32_t face_idx) -> 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)}, face_colors[face_idx]});
}
}
};
// +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}, 0);
// -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}, 1);
// +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}, 2);
// -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}, 3);
// +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}, 4);
// -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}, 5);
// 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*>(3 * sizeof(float)));
// location 2: uv (vec2)
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex),
reinterpret_cast<void*>(6 * sizeof(float)));
// location 3: color (vec3)
glEnableVertexAttribArray(3);
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(vertex),
reinterpret_cast<void*>(8 * sizeof(float)));
m_vao.unbind();
m_vbo.unbind();
m_ebo.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;
layout(location = 3) in vec3 a_color;
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;
out vec3 v_color;
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;
v_color = a_color;
}
)glsl";
char const* frag_src = R"glsl(
#version 410 core
in vec3 v_normal;
in vec3 v_world_pos;
in vec2 v_uv;
in vec3 v_color;
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 result = (ambient + diff * 0.8) * v_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;
}
}