StapleGL
Header-only C++20 OpenGL wrapper
Loading...
Searching...
No Matches
sandbox.cpp
#include "glad.h"
#include "staplegl.hpp"
#include <GLFW/glfw3.h>
#include <array>
#include <iostream>
#include <span>
#include <utility>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
// initial window size
const int32_t SCR_WIDTH = 1600;
const int32_t SCR_HEIGHT = 900;
auto main() -> int
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_SAMPLES, 4); // MSAA
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL example in StapleGL", nullptr, nullptr);
if (window == nullptr) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress)) == 0) { // NOLINT (reinterpret-cast)
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glEnable(GL_MULTISAMPLE); // MSAA
staplegl::shader_program const basic { "basic_shader", "./shaders/basic_shader.glsl" };
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
std::array<float, 12> vertices = {
0.5F, 0.5F, 0.0F, // top right
0.5F, -0.5F, 0.0F, // bottom right
-0.5F, -0.5F, 0.0F, // bottom left
-0.5F, 0.5F, 0.0F // top left
};
std::array<unsigned int, 6> indices = {
0, 1, 3, // first Triangle
1, 2, 3 // second Triangle
};
staplegl::index_buffer EBO { indices };
using namespace staplegl::shader_data_type;
staplegl::vertex_buffer_layout const layout { { u_type::vec3, "aPos" } };
VBO.set_layout(layout);
VAO.add_vertex_buffer(std::move(VBO));
VAO.set_index_buffer(std::move(EBO));
basic.bind();
while (glfwWindowShouldClose(window) == 0) {
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2F, 0.3F, 0.3F, 1.0F);
glClear(GL_COLOR_BUFFER_BIT);
// draw our first triangle
VAO.bind(); // seeing as we only have a single VAO there's
// no need to bind it every time, but we'll do
// so to keep things a bit more organized
// glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved
// etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this
// frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
glfwSetWindowShouldClose(window, 1);
}
}
// glfw: whenever the window size changed (by OS or user resize) this callback
// function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* /*window*/, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width
// and height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
const int32_t SCR_WIDTH
Definition batches.cpp:67
void processInput(GLFWwindow *window)
Definition batches.cpp:239
void framebuffer_size_callback(GLFWwindow *window, int width, int height)
Definition batches.cpp:246
auto main() -> int
Definition batches.cpp:70
const int32_t SCR_HEIGHT
Definition batches.cpp:68
Element Buffer Object (EBO) wrapper.
Shader program class.
Definition shader.hpp:77
Vertex Array Object (VAO) wrapper.
void bind() const
Bind the vertex array object.
void set_index_buffer(index_buffer &&ibo)
Set the index buffer object.
auto add_vertex_buffer(vertex_buffer &&vbo) -> vertex_array::iterator_t
Add a vertex buffer to the vertex array object.
Vertex Buffer Object (VBO) wrapper.
StapleGL, a C++20 wrapper for OpenGL.