CG_Labs  2021.2
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 
31  struct material_data {
32  glm::vec3 diffuse{ 0.0f };
33  glm::vec3 specular{ 0.0f };
34  glm::vec3 ambient{ 0.0f };
35  glm::vec3 emissive{ 0.0f };
36  float shininess{ 0.0f };
37  float indexOfRefraction{ 1.0f };
38  float opacity{ 1.0f };
39  };
40 
42  struct mesh_data {
43  GLuint vao{0u};
44  GLuint bo{0u};
45  GLuint ibo{0u};
46  GLsizei vertices_nb{0};
47  GLsizei indices_nb{0};
50  GLenum drawing_mode{GL_TRIANGLES};
51  std::string name{"un-named mesh"};
52  };
53 
54  enum class cull_mode_t : unsigned int {
55  disabled = 0u,
56  back_faces,
58  };
59 
60  enum class polygon_mode_t : unsigned int {
61  fill = 0u,
62  line,
63  point
64  };
65 
67  void init();
68 
70  void deinit();
71 
77  std::vector<mesh_data> loadObjects(std::string const& filename);
78 
91  GLuint createTexture(uint32_t width, uint32_t height,
92  GLenum target = GL_TEXTURE_2D,
93  GLint internal_format = GL_RGBA,
94  GLenum format = GL_RGBA,
95  GLenum type = GL_UNSIGNED_BYTE,
96  GLvoid const* data = nullptr);
97 
103  GLuint loadTexture2D(std::string const& filename,
104  bool generate_mipmap = true);
105 
116  GLuint loadTextureCubeMap(std::string const& posx, std::string const& negx,
117  std::string const& posy, std::string const& negy,
118  std::string const& posz, std::string const& negz,
119  bool generate_mipmap = true);
120 
129  GLuint createProgram(std::string const& vert_shader_source_path,
130  std::string const& frag_shader_source_path);
131 
153  void displayTexture(glm::vec2 const& lower_left,
154  glm::vec2 const& upper_right, GLuint texture,
155  GLuint sampler, glm::ivec4 const& swizzle,
156  glm::ivec2 const& window_size, bool linearise = false,
157  float nearPlane = 0.0f, float farPlane = 0.0f);
158 
167  GLuint createFBO(std::vector<GLuint> const& color_attachments,
168  GLuint depth_attachment = 0u);
169 
174  GLuint createSampler(std::function<void (GLuint)> const& setup);
175 
177  void drawFullscreen();
178 
180  GLuint getDebugTextureID();
181 
189  void renderBasis(float thickness_scale, float length_scale, glm::mat4 const& view_projection, glm::mat4 const& world = glm::mat4(1.0f));
190 
199  bool uiSelectCullMode(std::string const& label, enum cull_mode_t& cull_mode) noexcept;
200 
203  void changeCullMode(enum cull_mode_t const cull_mode) noexcept;
204 
213  bool uiSelectPolygonMode(std::string const& label, enum polygon_mode_t& polygon_mode) noexcept;
214 
217  void changePolygonMode(enum polygon_mode_t const polygon_mode) noexcept;
218 }
GLuint world
Definition: helpers.cpp:29
GLuint thickness_scale
Definition: helpers.cpp:31
GLuint length_scale
Definition: helpers.cpp:32
Namespace containing a few helpers for the LUGG computer graphics labs.
Definition: helpers.hpp:16
GLuint loadTexture2D(std::string const &filename, bool generate_mipmap=true)
Load an image into an OpenGL 2D-texture.
Definition: helpers.cpp:385
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:478
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:497
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:404
void drawFullscreen()
Draw full screen.
Definition: helpers.cpp:559
GLuint getDebugTextureID()
Retrieve the ID of a small placeholder texture.
Definition: helpers.cpp:567
void init()
Allocate some objects needed by some helper functions.
Definition: helpers.cpp:59
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:630
bool uiSelectCullMode(std::string const &label, enum cull_mode_t &cull_mode) noexcept
Add a combo box to the current ImGUI window, to choose a cull mode.
Definition: helpers.cpp:590
shader_bindings
Formalise mapping between an OpenGL VAO attribute binding, and the meaning of that attribute.
Definition: helpers.hpp:19
@ tangents
= 3, value of the binding point for tangents
@ binormals
= 4, value of the binding point for binormals
@ texcoords
= 2, value of the binding point for texcoords
@ normals
= 1, value of the binding point for normals
@ vertices
= 0, value of the binding point for vertices
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
polygon_mode_t
Definition: helpers.hpp:60
void deinit()
Deallocate objects allocated by the init() function.
Definition: helpers.cpp:72
cull_mode_t
Definition: helpers.hpp:54
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:359
void renderBasis(float thickness_scale, float length_scale, glm::mat4 const &view_projection, glm::mat4 const &world=glm::mat4(1.0f))
Render a right-hand orthonormal basis.
Definition: helpers.cpp:573
GLuint createSampler(std::function< void(GLuint)> const &setup)
Create an OpenGL sampler and set it up.
Definition: helpers.cpp:549
void changeCullMode(enum cull_mode_t const cull_mode) noexcept
Enable or disable culling, and call glCullFace with the specified cull mode.
Definition: helpers.cpp:601
std::vector< mesh_data > loadObjects(std::string const &filename)
Load objects found in an object/scene file, using assimp.
Definition: helpers.cpp:109
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:619
GLuint createFBO(std::vector< GLuint > const &color_attachments, GLuint depth_attachment=0u)
Create an OpenGL FrameBuffer Object using the specified attachments.
Definition: helpers.cpp:526
Definition: helpers.hpp:31
glm::vec3 diffuse
Definition: helpers.hpp:32
float indexOfRefraction
Definition: helpers.hpp:37
float opacity
Definition: helpers.hpp:38
float shininess
Definition: helpers.hpp:36
glm::vec3 specular
Definition: helpers.hpp:33
glm::vec3 ambient
Definition: helpers.hpp:34
glm::vec3 emissive
Definition: helpers.hpp:35
Contains the data for a mesh in OpenGL.
Definition: helpers.hpp:42
GLsizei vertices_nb
number of vertices stored in bo
Definition: helpers.hpp:46
material_data material
constant values for the material of this mesh
Definition: helpers.hpp:49
GLuint bo
OpenGL name of the Buffer Object.
Definition: helpers.hpp:44
GLsizei indices_nb
number of indices stored in ibo
Definition: helpers.hpp:47
std::string name
Name of the mesh; used for debugging purposes.
Definition: helpers.hpp:51
texture_bindings bindings
texture bindings for this mesh
Definition: helpers.hpp:48
GLuint vao
OpenGL name of the Vertex Array Object.
Definition: helpers.hpp:43
GLenum drawing_mode
OpenGL drawing mode, i.e. GL_TRIANGLES, GL_LINES, etc.
Definition: helpers.hpp:50
GLuint ibo
OpenGL name of the Buffer Object for indices.
Definition: helpers.hpp:45