#include #include #include "glad/glad.h" #include "glm/gtc/matrix_transform.hpp" #include "scenes/cube.hpp" namespace cbt::scenes { cube::cube() { m_start = std::chrono::steady_clock::now(); } auto cube::init() -> bool { if (!build_pipeline()) { return false; } return true; } auto cube::update(float) -> void {} auto cube::render(int width, int height) -> void { glViewport(0, 0, width, height); auto now = std::chrono::steady_clock::now(); auto elapsed = std::chrono::duration(now - m_start).count(); 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, -3.0f}); auto model = glm::rotate(glm::mat4{1.0f}, elapsed, glm::vec3{1.0f, 0.5f, 0.3f}); m_pipeline.draw(model, view, proj); } auto cube::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_color; uniform mat4 u_model; uniform mat4 u_view; uniform mat4 u_proj; out vec3 v_color; void main() { gl_Position = u_proj * u_view * u_model * vec4(a_pos, 1.0); v_color = a_color; } )glsl"; char const* frag_src = R"glsl( #version 410 core in vec3 v_color; out vec4 frag_color; void main() { frag_color = vec4(v_color, 1.0); } )glsl"; std::array vertices = { // front face (cyan) 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, // back face (magenta) 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 1.0f, -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 1.0f, -0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 1.0f, -0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, // top face (yellow) 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, -0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f, -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, // bottom face (white) 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, -0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f, 1.0f, 1.0f, 1.0f, // right face (lime) 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // left face (orange) -0.5f, -0.5f, -0.5f, 1.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.5f, 1.0f, 0.5f, 0.0f, -0.5f, 0.5f, 0.5f, 1.0f, 0.5f, 0.0f, -0.5f, -0.5f, -0.5f, 1.0f, 0.5f, 0.0f, -0.5f, 0.5f, 0.5f, 1.0f, 0.5f, 0.0f, -0.5f, 0.5f, -0.5f, 1.0f, 0.5f, 0.0f, }; gfx::pipeline_desc desc{ .vertex_data = std::as_bytes(std::span{vertices}), .attributes = { {.location = 0, .num_components = 3, .offset = 0}, {.location = 1, .num_components = 3, .offset = 12}, }, .vertex_stride = 24, .vertex_shader_src = vert_src, .fragment_shader_src = frag_src, .depth_test = true, .primitive = gfx::primitive_type::triangles // no index data }; m_pipeline = gfx::pipeline{desc}; return m_pipeline.valid(); } }