Files
cuber/cuber.cpp
T
portersky 30ddaf7d39 feat: render spinning 3D cube with glm
- Add glm dependency for matrix transformations
- Replace triangle with 6-face colored cube
- Add MVP shader uniforms (model, view, projection)
- Enable depth testing for proper 3D rendering
- Spin cube around (1, 0.5, 0.3) axis

style: fix trailing return type for move assignment operators

- buffer/texture/shader/vao: use auto fn() -> T& style
- Document trailing return type convention in AGENTS.md
2026-05-05 22:19:33 +02:00

177 lines
5.5 KiB
C++

#define GLFW_INCLUDE_NONE
#include "GLFW/glfw3.h"
#include "cbt/opengl/context.hpp"
#include "cbt/opengl/shader.hpp"
#include "cbt/opengl/buffer.hpp"
#include "cbt/opengl/vao.hpp"
#include <chrono>
#include <asio.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "glad/glad.h"
auto main(int, char const*[]) -> int {
auto ctx = cbt::opengl::context("cuber", 1280, 720);
if (!ctx.valid()) {
return 1;
}
// compile shader
auto prog = cbt::opengl::shader();
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";
if (!prog.compile_vertex(vert_src) || !prog.compile_fragment(frag_src) || !prog.link()) {
return 1;
}
// cube vertices: 6 faces, 2 triangles per face, 3 verts each = 36 vertices
// each vertex: x, y, z, r, g, b
std::array<float, 36 * 6> 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.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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,
};
auto vbo = cbt::opengl::buffer(cbt::opengl::buffer_type::vertex);
vbo.upload(vertices.data(), vertices.size() * sizeof(float));
// bind VAO and configure vertex attributes
auto vao = cbt::opengl::vao();
vao.bind();
vbo.bind();
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), nullptr);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
reinterpret_cast<void*>(3 * sizeof(float)));
vbo.unbind();
vao.unbind();
// enable depth testing
glEnable(GL_DEPTH_TEST);
// signal handling
asio::io_context io;
asio::signal_set signals(io, SIGINT, SIGTERM);
bool quit = false;
signals.async_wait([&](auto, auto) {
quit = true;
io.stop();
});
auto process_signals = [&]() -> void {
while (io.poll()) {}
};
// render loop
auto start = std::chrono::steady_clock::now();
while (!ctx.should_close()) {
process_signals();
if (quit || glfwGetKey(ctx.raw(), GLFW_KEY_Q) == GLFW_PRESS) {
break;
}
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration<float>(now - start).count();
glClearColor(0.15f, 0.15f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// compute MVP matrices
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, -3.0f});
auto model = glm::rotate(glm::mat4{1.0f}, elapsed, glm::vec3{1.0f, 0.5f, 0.3f});
prog.use();
glUniformMatrix4fv(glGetUniformLocation(prog.id(), "u_proj"), 1, GL_FALSE, glm::value_ptr(proj));
glUniformMatrix4fv(glGetUniformLocation(prog.id(), "u_view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(prog.id(), "u_model"), 1, GL_FALSE, glm::value_ptr(model));
vao.bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
vao.unbind();
prog.unuse();
ctx.swap_buffers();
ctx.poll_events();
}
return 0;
}