A thin wrapper around OpenGL for C++17 with no dependencies. This library was inspired by Vulkan.hpp, the C++ wrapper for Vulkan.
To add this library to your project, simply add these two lines to your CMakeLists.txt:
add_subdirectory(path/to/glpp)
target_link_libraries(${PROJECT_NAME} PRIVATE glpp::glpp)
Then include it as:
#include <glpp/glpp.hpp>
All OpenGL object handles (or ids) are wrapped in a UniqueXxx
type that handles automatic destruction. These types are similar to a std::unique_ptr
: they can't be copied but can be moved.
We define an enum class
for each group of OpenGL constants that belong together:
namespace glpp {
enum class Interpolation : GLint {
NearestNeighbour = GL_NEAREST,
Linear = GL_LINEAR,
};
}
We also provide a wrapper for each OpenGL function with stronger typing and error checking:
namespace glpp {
void set_minification_filter(const UniqueTexture& texture, Interpolation interpolation) const
{
internal::assert_is_bound(GL_TEXTURE_BINDING_2D, *texture,
"You must bind the texture before setting its minification filter");
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, raw(interpolation));
glpp_check_errors();
}
}
glpp_check_errors()
will detect errors generated by OpenGL (even if you don't have access to the modern debugging feature added in OpenGL 4.3):
glUseProgram(0);
glpp_check_errors(); // Will detect that we just tried to use an invalid program id and will call the error callback with an error message of "GL_INVALID_OPERATION"
You can provide you own callback that will be called when an error is detected:
glpp::set_error_callback([](std::string&& error_message) {
throw std::runtime_error{error_message};
});
If you do not provide a callback, by default glpp logs to std::cerr
.
To enable error checking you need to define GLPP_SHOULD_CHECK_ERRORS
from your CMake.
If you wanted to enable error checking only in debug mode you would do:
target_compile_definitions(glpp PRIVATE $<$<CONFIG:Debug>:GLPP_SHOULD_CHECK_ERRORS>)
and if you wanted error checking even in release:
target_compile_definitions(glpp PRIVATE GLPP_SHOULD_CHECK_ERRORS)
Here is a typical example of how you would add glpp to your projects:
# ---Add glpp---
add_subdirectory(third-party/glpp)
target_compile_definitions(glpp PRIVATE $<$<CONFIG:Debug>:GLPP_SHOULD_CHECK_ERRORS>) # Enable error checking only in debug mode
target_link_libraries(my_project PUBLIC glpp::glpp)
# ------