#include #include #include #include #include #include "glad/glad.h" #include "glm/gtc/matrix_transform.hpp" #include "scenes/sphere.hpp" namespace cbt::scenes { sphere::sphere() { m_start = std::chrono::steady_clock::now(); } auto sphere::init() -> bool { if (!build_pipeline() || !build_post_pipeline()) { return false; } return true; } auto sphere::update(float) -> void {} auto sphere::render(int width, int height) -> void { auto now = std::chrono::steady_clock::now(); auto elapsed = std::chrono::duration(now - m_start).count(); m_rt.resize(width, height); glViewport(0, 0, width, height); // Step 1: Render scene to texture (offscreen pass) m_rt.bind(); glClearColor(0.15f, 0.15f, 0.2f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); auto aspect = float(width) / float(height); 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_scene_pipeline.draw(model, view, proj); m_rt.unbind(); // Reset viewport for screen pass (RT bind changed it) glViewport(0, 0, width, height); // Step 2: Post-processing step (sample RT texture, apply effect, render to screen) glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); m_post_pipeline.bind_texture("u_texture", m_rt.color_id(), 0); m_post_pipeline.draw(glm::mat4{1.0f}, glm::mat4{1.0f}, glm::mat4{1.0f}); } auto sphere::build_pipeline() -> 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"; struct vertex { glm::vec3 position; glm::vec3 normal; glm::vec2 uv; glm::vec3 color; }; std::uint32_t const div = 32; std::vector vertices; std::vector 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; } gfx::pipeline_desc desc{ .vertex_data = std::as_bytes(std::span{vertices}), .index_data = std::as_bytes(std::span{indices}), .attributes = { {.location = 0, .num_components = 3, .offset = 0}, {.location = 1, .num_components = 3, .offset = 12}, {.location = 2, .num_components = 2, .offset = 24}, {.location = 3, .num_components = 3, .offset = 32}, }, .vertex_stride = sizeof(vertex), .vertex_shader_src = vert_src, .fragment_shader_src = frag_src, .depth_test = true, .primitive = gfx::primitive_type::triangles, .index_type_ = gfx::index_type::uint32 }; m_scene_pipeline = gfx::pipeline{desc}; return m_scene_pipeline.valid(); } auto sphere::build_post_pipeline() -> bool { char const* post_vert = R"glsl( #version 410 core layout(location = 0) in vec2 a_pos; layout(location = 1) in vec2 a_uv; out vec2 v_uv; void main() { gl_Position = vec4(a_pos, 0.0, 1.0); v_uv = a_uv; } )glsl"; char const* post_frag = R"glsl( #version 410 core in vec2 v_uv; uniform sampler2D u_texture; out vec4 frag_color; void main() { vec3 col = texture(u_texture, v_uv).rgb; // simple processing: vignette only (preserves original colors from sphere faces) float vig = 1.0 - length(v_uv * 2.0 - 1.0) * 0.5; frag_color = vec4(col * vig, 1.0); } )glsl"; struct fs_vertex { glm::vec2 pos; glm::vec2 uv; }; std::array qverts = {{ {{-1.0f, -1.0f}, {0.0f, 0.0f}}, {{ 1.0f, -1.0f}, {1.0f, 0.0f}}, {{ 1.0f, 1.0f}, {1.0f, 1.0f}}, {{-1.0f, 1.0f}, {0.0f, 1.0f}}, }}; std::array qinds = {0, 1, 2, 0, 2, 3}; gfx::pipeline_desc desc{ .vertex_data = std::as_bytes(std::span{qverts}), .index_data = std::as_bytes(std::span{qinds}), .attributes = { {.location = 0, .num_components = 2, .offset = 0}, {.location = 1, .num_components = 2, .offset = 8}, }, .vertex_stride = sizeof(fs_vertex), .vertex_shader_src = post_vert, .fragment_shader_src = post_frag, .depth_test = false, .primitive = gfx::primitive_type::triangles, .index_type_ = gfx::index_type::uint32 }; m_post_pipeline = gfx::pipeline{desc}; return m_post_pipeline.valid(); } }