feat: render per-vertex color triangle
- Add shader class for vertex/fragment program compilation - Add vao class for vertex array object management - Render RGB gradient triangle with interpolated vertex colors
This commit is contained in:
@@ -2,20 +2,71 @@
|
||||
#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 <csignal>
|
||||
|
||||
#include <asio.hpp>
|
||||
|
||||
#include "asio.hpp"
|
||||
#include "glad/glad.h"
|
||||
|
||||
auto main(int, char const*[]) -> int {
|
||||
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();
|
||||
|
||||
const char* 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";
|
||||
|
||||
const char* 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;
|
||||
@@ -29,6 +80,7 @@ auto main(int, char const*[]) -> int {
|
||||
while (io.poll()) {}
|
||||
};
|
||||
|
||||
// render loop
|
||||
while (!ctx.should_close()) {
|
||||
process_signals();
|
||||
|
||||
@@ -39,6 +91,12 @@ auto main(int, char const*[]) -> int {
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user