CG_Labs  2020.0
helpers.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <glad/glad.h>
4 #include <GLFW/glfw3.h>
5 #include <glm/glm.hpp>
6 
7 #include "core/FPSCamera.h" // As it includes OpenGL headers, import it after glad
8 
9 #include <functional>
10 #include <string>
11 #include <vector>
12 #include <unordered_map>
13 
15 namespace bonobo
16 {
19  enum class shader_bindings : unsigned int{
20  vertices = 0u,
21  normals,
22  texcoords,
23  tangents,
24  binormals
25  };
26 
29  using texture_bindings = std::unordered_map<std::string, GLuint>;
30 
32  struct mesh_data {
33  GLuint vao{0u};
34  GLuint bo{0u};
35  GLuint ibo{0u};
36  size_t vertices_nb{0u};
37  size_t indices_nb{0u};
39  GLenum drawing_mode{GL_TRIANGLES};
40  std::string name{};
41  };
42 
43  enum class polygon_mode_t : unsigned int {
44  fill = 0u,
45  line,
46  point
47  };
48 
50  void init();
51 
53  void deinit();
54 
60  std::vector<mesh_data> loadObjects(std::string const& filename);
61 
74  GLuint createTexture(uint32_t width, uint32_t height,
75  GLenum target = GL_TEXTURE_2D,
76  GLint internal_format = GL_RGBA,
77  GLenum format = GL_RGBA,
78  GLenum type = GL_UNSIGNED_BYTE,
79  GLvoid const* data = nullptr);
80 
86  GLuint loadTexture2D(std::string const& filename,
87  bool generate_mipmap = true);
88 
99  GLuint loadTextureCubeMap(std::string const& posx, std::string const& negx,
100  std::string const& posy, std::string const& negy,
101  std::string const& posz, std::string const& negz,
102  bool generate_mipmap = true);
103 
112  GLuint createProgram(std::string const& vert_shader_source_path,
113  std::string const& frag_shader_source_path);
114 
136  void displayTexture(glm::vec2 const& lower_left,
137  glm::vec2 const& upper_right, GLuint texture,
138  GLuint sampler, glm::ivec4 const& swizzle,
139  glm::ivec2 const& window_size, bool linearise = false,
140  float nearPlane = 0.0f, float farPlane = 0.0f);
141 
150  GLuint createFBO(std::vector<GLuint> const& color_attachments,
151  GLuint depth_attachment = 0u);
152 
157  GLuint createSampler(std::function<void (GLuint)> const& setup);
158 
160  void drawFullscreen();
161 
170  bool uiSelectPolygonMode(std::string const& label, enum polygon_mode_t& polygon_mode) noexcept;
171 
174  void changePolygonMode(enum polygon_mode_t const polygon_mode) noexcept;
175 }
bonobo::deinit
void deinit()
Deallocate objects allocated by the init() function.
Definition: helpers.cpp:41
bonobo::createFBO
GLuint createFBO(std::vector< GLuint > const &color_attachments, GLuint depth_attachment=0u)
Create an OpenGL FrameBuffer Object using the specified attachments.
Definition: helpers.cpp:409
bonobo::createProgram
GLuint createProgram(std::string const &vert_shader_source_path, std::string const &frag_shader_source_path)
Create an OpenGL program consisting of a vertex and a fragment shader.
Definition: helpers.cpp:361
bonobo::loadTexture2D
GLuint loadTexture2D(std::string const &filename, bool generate_mipmap=true)
Load an image into an OpenGL 2D-texture.
Definition: helpers.cpp:268
FPSCamera.h
bonobo::uiSelectPolygonMode
bool uiSelectPolygonMode(std::string const &label, enum polygon_mode_t &polygon_mode) noexcept
Add a combo box to the current ImGUI window, to choose a polygon mode.
Definition: helpers.cpp:450
bonobo::shader_bindings::tangents
@ tangents
= 3, value of the binding point for tangents
bonobo::shader_bindings
shader_bindings
Formalise mapping between an OpenGL VAO attribute binding, and the meaning of that attribute.
Definition: helpers.hpp:19
bonobo::mesh_data::indices_nb
size_t indices_nb
number of indices stored in ibo
Definition: helpers.hpp:37
bonobo::createTexture
GLuint createTexture(uint32_t width, uint32_t height, GLenum target=GL_TEXTURE_2D, GLint internal_format=GL_RGBA, GLenum format=GL_RGBA, GLenum type=GL_UNSIGNED_BYTE, GLvoid const *data=nullptr)
Creates an OpenGL texture without any content nor parameters.
Definition: helpers.cpp:242
bonobo::mesh_data::vertices_nb
size_t vertices_nb
number of vertices stored in bo
Definition: helpers.hpp:36
bonobo::shader_bindings::texcoords
@ texcoords
= 2, value of the binding point for texcoords
bonobo::mesh_data::vao
GLuint vao
OpenGL name of the Vertex Array Object.
Definition: helpers.hpp:33
bonobo::createSampler
GLuint createSampler(std::function< void(GLuint)> const &setup)
Create an OpenGL sampler and set it up.
Definition: helpers.cpp:432
bonobo::mesh_data::bo
GLuint bo
OpenGL name of the Buffer Object.
Definition: helpers.hpp:34
bonobo::drawFullscreen
void drawFullscreen()
Draw full screen.
Definition: helpers.cpp:442
bonobo::polygon_mode_t::fill
@ fill
bonobo::loadObjects
std::vector< mesh_data > loadObjects(std::string const &filename)
Load objects found in an object/scene file, using assimp.
Definition: helpers.cpp:69
bonobo::mesh_data::drawing_mode
GLenum drawing_mode
OpenGL drawing mode, i.e. GL_TRIANGLES, GL_LINES, etc.
Definition: helpers.hpp:39
bonobo::mesh_data
Contains the data for a mesh in OpenGL.
Definition: helpers.hpp:32
bonobo::texture_bindings
std::unordered_map< std::string, GLuint > texture_bindings
Association of a sampler name used in GLSL to a corresponding texture ID.
Definition: helpers.hpp:29
bonobo::shader_bindings::binormals
@ binormals
= 4, value of the binding point for binormals
bonobo::mesh_data::bindings
texture_bindings bindings
texture bindings for this mesh
Definition: helpers.hpp:38
bonobo::shader_bindings::normals
@ normals
= 1, value of the binding point for normals
bonobo
Namespace containing a few helpers for the LUGG computer graphics labs.
Definition: helpers.hpp:16
bonobo::polygon_mode_t::line
@ line
bonobo::polygon_mode_t::point
@ point
bonobo::mesh_data::name
std::string name
Name of the mesh; used for debugging purposes.
Definition: helpers.hpp:40
bonobo::changePolygonMode
void changePolygonMode(enum polygon_mode_t const polygon_mode) noexcept
Call glPolygonMode for both front and back faces, with the specified polygon mode.
Definition: helpers.cpp:461
bonobo::displayTexture
void displayTexture(glm::vec2 const &lower_left, glm::vec2 const &upper_right, GLuint texture, GLuint sampler, glm::ivec4 const &swizzle, glm::ivec2 const &window_size, bool linearise=false, float nearPlane=0.0f, float farPlane=0.0f)
Display the current texture in the specified rectangle.
Definition: helpers.cpp:380
bonobo::shader_bindings::vertices
@ vertices
= 0, value of the binding point for vertices
bonobo::init
void init()
Allocate some objects needed by some helper functions.
Definition: helpers.cpp:31
bonobo::mesh_data::ibo
GLuint ibo
OpenGL name of the Buffer Object for indices.
Definition: helpers.hpp:35
bonobo::polygon_mode_t
polygon_mode_t
Definition: helpers.hpp:43
bonobo::loadTextureCubeMap
GLuint loadTextureCubeMap(std::string const &posx, std::string const &negx, std::string const &posy, std::string const &negy, std::string const &posz, std::string const &negz, bool generate_mipmap=true)
Load six images into an OpenGL cubemap-texture.
Definition: helpers.cpp:287