9114eaabc6
- const T* -> T const* in all headers and implementations - const T& -> T const& for copy constructor/operator= deletes - update AGENTS.md to document east const convention
106 lines
2.6 KiB
C++
106 lines
2.6 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 "asio.hpp"
|
|
#include "glad/glad.h"
|
|
|
|
auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> 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;
|
|
out vec3 v_color;
|
|
void main() {
|
|
gl_Position = 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;
|
|
}
|
|
|
|
// vertex data: triangle with per-vertex colors
|
|
// red, green, blue corners
|
|
std::array<float, 18> vertices = {
|
|
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
|
|
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
|
|
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.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();
|
|
|
|
// 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
|
|
while (!ctx.should_close()) {
|
|
process_signals();
|
|
|
|
if (quit || glfwGetKey(ctx.raw(), GLFW_KEY_Q) == GLFW_PRESS) {
|
|
break;
|
|
}
|
|
|
|
glClearColor(0.6f, 0.8f, 1.0f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
prog.use();
|
|
vao.bind();
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
vao.unbind();
|
|
prog.unuse();
|
|
|
|
ctx.swap_buffers();
|
|
ctx.poll_events();
|
|
}
|
|
|
|
return 0;
|
|
}
|